You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Mutating objects in sharedb is not necessarily the most intuitive thing to do, which is a shame for a library whose purpose is to mutate objects.
It often requires in-depth understanding of the types being used (by default json0) and how to construct ops for that type.
For users of TypeScript, op construction like this also removes type safety, and autocompletion suggestions.
For example, consider a simple json0 document:
{"foo": "bar"}
To mutate this, we need to consult the json0 docs, and then manually assemble an op:
[
{
"p": ["foo"],
"od": "bar",
"oi": "baz",
}
]
As well as being cumbersome to write (and to read!), this can also be error-prone. If we're using TypeScript, we run in to these potentially avoidable errors:
accessing an invalid path
inserting a property with an incorrect type
deleting the incorrect value
Proposal
I'm proposing extension of the sharedb API to allow for a more fluent syntax, where consumers can (seemingly) mutate doc.data directly:
doc.data.foo='baz';
This update would be far easier for consumers to use, and would keep TypeScript checks.
It would also stop consumers from accidentally mutating the doc.data object (since this is now allowed!).
Implementation
For a fully-worked json0 example, please see below.
The basic concept is to use JavaScript Proxys to convert setter calls into ops.
Since conversion into ops is type-dependent, the implementation would belong in the types. We could potentially add an optional method to the type spec: accessor(), which returns a proxy object which may be "mutated" directly and instead submits ops.
If we want the type to be unaware of the Doc class (probably for the best), this may be done by having the accessor emit events that the Doc can subscribe to and then submit.
If we wanted to use super-modern JS features, we could potentially use for await...of and have the doc asynchronously iterate ops from the accessor.
Acknowledgement & error handling
Since consumers don't directly call submit() with this API, they also won't have an opportunity to register an error handler. For simple use cases, this may be "good enough" — ShareDB will still emit an error event if anything goes wrong, which can either be handled or leaved to let Node.js processes crash.
However, consumers may also want to wait for the op to be acknowledged by the server for some reason (eg UI updates; performing a dependent task; etc.). They may also want to handle errors on a per-op basis (especially if they want sensible stack traces).
Since setters aren't asynchronous, this would require addition of some mechanism to capture errors from these accessor updates.
Since ops will only be submitted in the next event loop tick, we can synchronously queue a number of changes, and then synchronously attach a callback:
Could create confusion when moving between types that don't implement accessor()
Hides type details from consumers, possibly obfuscating more appropriate op features (eg using lm instead of li+ld; using na instead of oi+od; etc.)
Increased CPU + memory usage (although could be made an opt-in feature)
It's always hard to tell with JS, but I think Proxy was only finalised in the ES6 spec (and we currently target ES3)
json0 example
Below is a basic example of what this might look like for json0.
The main complications come about from handling arrays (we manually override a number of array methods, taken from Vue's reactivity docs. There are also some corner cases around creating sparse arrays, which json0 doesn't support.
exportfunctionjson0Accessor(doc){constproxyCache=newWeakMap();constpathCache=newWeakMap();constARRAY_OVERRIDES={pop: (target)=>function(){const[popped]=splice(target)(-1,1);returnpopped;},push: (target)=>function(...items){constnewLength=target.length+items.length;splice(target)(target.length,0, ...items);returnnewLength;},unshift: (target)=>function(...items){constnewLength=target.length+items.length;splice(target)(0,0, ...items);returnnewLength;},shift: (target)=>function(){const[shifted]=splice(target)(0,1);returnshifted;},
splice,sort: ()=>()=>{// Left as an exercise to the reader to implement using `lm`thrownewError("Unsupported method 'sort'");},reverse: ()=>()=>{// Left as an exercise to the reader to implement using `lm`thrownewError("Unsupported method 'reverse'");},};returnnewProxy(doc,{get(target,key){if(key==='data')returndoc.data&&deepProxy(doc.data);returntarget[key];},});functiondeepProxy(obj){returnnewProxy(obj,{get(target,key){constvalue=target[key];if(Array.isArray(target)&&keyinARRAY_OVERRIDES){returnARRAY_OVERRIDES[key](target);}if(!value||typeofvalue!=='object')returnvalue;constparentPath=pathCache.get(target)||[];constpath=parentPath.concat(normalizedKey(target,key));pathCache.set(value,path);if(proxyCache.has(value))returnproxyCache.get(value);constproxy=deepProxy(value);proxyCache.set(value,proxy);returnproxy;},set(target,key,value){key=normalizedKey(target,key);constp=pathCache.get(target).concat(key);letop={p,od: target[key],oi: value};if(Array.isArray(target)){// Setting length directly can create a sparse Array, which we can't do with json0if(key==='length')thrownewError('Cannot set Array.length with writeableJSON');elseif(typeofkey!=='number')thrownewError('Cannot set non-numeric keys on Arrays');// Setting keys outside our length also creates a sparse indexif(key>=target.length)thrownewError('Cannot set index outside Array bounds');op={p,ld: target[key],li: value};}submitOp(op);returntrue;},deleteProperty(target,key: PropertyKey){// Using the delete keyword with an Array creates a sparse Array, which we can't do with json0if(Array.isArray(target))thrownewError('Cannot use delete with arrays');key=normalizedKey(target,key);constp=pathCache.get(target).concat(key);submitOp({p,od: target[key]});returntrue;},});}functionsubmitOp(op){if(doc.type.uri!=='http://sharejs.org/types/JSONv0')thrownewError(`Cannot with type '${doc.type.uri}'`);doc.submitOp(op);}functionnormalizedKey(target,key){if(!Array.isArray(target))returnkey;constnormalized=+key.toString();returnNumber.isInteger(normalized) ? normalized : key;}functionsplice(target){returnfunction(start,deleteCount, ...items){start=+start;deleteCount=arguments.length>1 ? +deleteCount : target.length;if(start<0)start=target.length+start;start=Math.min(start,target.length);start=Math.max(start,0);constpath=pathCache.get(target);constp=path.concat(start);constdeleted=target.slice(start,start+deleteCount);for(constldofdeleted)submitOp({p, ld});leti=start;for(constitemofitems){submitOp({p: path.concat(i),li: item});i++;}returndeleted;};}}
The text was updated successfully, but these errors were encountered:
submitBatch option could provide the mutable doc data for you, which prevents issues with forgetting to handle op submission errors that would cause unhandled errors / promise rejections
Doing it as a plugin should mean cleaner code since the plugin can be Doc-aware, and then it's not tied to core json0
I think for simple scenarios, proxy can avoid using json0 to construct op object, but for some cases, there will be problems. For example, if I have an array and I want to swap the position of 1,3, maybe use
tmp=data[3];data[3]=data[1];data[1]=tmp;
In this case we cannot identify whether to swap the order of the array or to assign values to two elements.
@Qquanwei yes I agree; I've stated that in the "Disadvantages" section. I think for things like this, consumers would be expected to use the "traditional" submitOp() machinery they want specific semantics.
Introduction
Mutating objects in
sharedb
is not necessarily the most intuitive thing to do, which is a shame for a library whose purpose is to mutate objects.It often requires in-depth understanding of the types being used (by default
json0
) and how to construct ops for that type.For users of TypeScript, op construction like this also removes type safety, and autocompletion suggestions.
For example, consider a simple
json0
document:To mutate this, we need to consult the
json0
docs, and then manually assemble an op:As well as being cumbersome to write (and to read!), this can also be error-prone. If we're using TypeScript, we run in to these potentially avoidable errors:
Proposal
I'm proposing extension of the
sharedb
API to allow for a more fluent syntax, where consumers can (seemingly) mutatedoc.data
directly:This update would be far easier for consumers to use, and would keep TypeScript checks.
It would also stop consumers from accidentally mutating the
doc.data
object (since this is now allowed!).Implementation
For a fully-worked
json0
example, please see below.The basic concept is to use JavaScript
Proxy
s to convert setter calls into ops.Since conversion into ops is type-dependent, the implementation would belong in the types. We could potentially add an optional method to the type spec:
accessor()
, which returns a proxy object which may be "mutated" directly and instead submits ops.If we want the type to be unaware of the
Doc
class (probably for the best), this may be done by having theaccessor
emit events that theDoc
can subscribe to and then submit.If we wanted to use super-modern JS features, we could potentially use
for await...of
and have the doc asynchronously iterate ops from theaccessor
.Acknowledgement & error handling
Since consumers don't directly call
submit()
with this API, they also won't have an opportunity to register an error handler. For simple use cases, this may be "good enough" — ShareDB will still emit anerror
event if anything goes wrong, which can either be handled or leaved to let Node.js processes crash.However, consumers may also want to wait for the op to be acknowledged by the server for some reason (eg UI updates; performing a dependent task; etc.). They may also want to handle errors on a per-op basis (especially if they want sensible stack traces).
Since setters aren't asynchronous, this would require addition of some mechanism to capture errors from these
accessor
updates.One possible way is to just add something like:
Since ops will only be submitted in the next event loop tick, we can synchronously queue a number of changes, and then synchronously attach a callback:
We could alternatively add some sort of helper method that will actively batch ops and attach a callback. Consumer code might look like:
Disadvantages
accessor()
lm
instead ofli
+ld
; usingna
instead ofoi
+od
; etc.)Proxy
was only finalised in the ES6 spec (and we currently target ES3)json0
exampleBelow is a basic example of what this might look like for
json0
.The main complications come about from handling arrays (we manually override a number of array methods, taken from Vue's reactivity docs. There are also some corner cases around creating sparse arrays, which
json0
doesn't support.The text was updated successfully, but these errors were encountered: