Clock that keeps track of logical timestamps of the current editing session and logical clocks of all known peers.
Optional
onbeforepatchCallback called before every applyPatch
call.
Optional
onbeforeresetCallback called before model isi reset using the .reset()
method.
Optional
onpatchCallback called after every applyPatch
call.
Optional
onresetCallback called after model has been reset using the .reset()
method.
Root of the JSON document is implemented as Last Write Wins Register, so that the JSON document does not necessarily need to be an object. The JSON document can be any JSON value.
Static
Readonly
sidGenerates a random session ID. Use this method to generate a session ID for a new user. Store the session ID in the user's browser or device once and reuse it for all editing sessions of that user.
Generating a new session ID for each editing session will work, however, that is not recommended. If a user generates a new session ID for each editing session, the session clock table will grow indefinitely.
Applies a batch of patches to the document.
A batch, i.e. an array of patches.
Applies a single patch to the document. All mutations to the model must go through this method.
Strictly types the model and sets the default value of the model, if the document is empty.
The schema to set for this model.
Strictly typed model.
Changes the session ID of the model. By modifying the attached clock vector of the model. Be careful when changing the session ID of the model, as this is an advanced operation.
Use the Model.load method to load a model with the the right session ID, instead of changing the session ID of the model. When in doubt, use the Model.fork method to create a new model with the right session ID.
The new session ID to set for the model.
Returns the view of the model.
JSON/CBOR of the model.
Static
Readonly
createCreate a new JSON CRDT model. If a schema is provided, the model is strictly typed and the default value of the model is set to the default value of the schema.
By default, the model is created with a random session ID and is using
a logical clock. It is also possible to create a model which uses a server
clock by providing the session ID SESSION.SERVER
(1).
Create a basic model, without schema and default value:
const model = Model.create();
Create a strictly typed model with a schema and default value:
const schema = s.obj({
ticker: s.con<string>('BODEN'),
name: s.str('Jeo Boden'),
tags: s.arr(
s.str('token'),
),
});
const model = Model.create(schema);
Create a model with a custom session ID for your logical clock:
const schema = s.str('');
const sid = 123456789;
const model = Model.create(schema, sid);
The session ID must be at least 65,536 or higher, see JSON CRDT Patch specification.
To create a model with a server clock, use the SESSION.SERVER
, which is
equal to 1:
const model = Model.create(undefined, SESSION.SERVER);
// or
const model = Model.create(undefined, 1);
Finally, you can create a model with your clock vector:
const clock = new ClockVector(123456789, 1);
const model = Model.create(undefined, clock);
Optional
schema: SThe schema (typing and default value) to set for this model.
Session ID to use for local operations. Defaults to a random session ID generated by Model.sid.
A strictly typed model.
Static
Readonly
fromDecodes a model from a "binary" structural encoding.
Use Model.load instead, if you want to set the session ID of the model and the right schema for the model, during the de-serialization.
Binary blob of a model encoded using "binary" structural encoding.
An instance of a model.
Static
fromInstantiates a model from a collection of patches. The patches are applied to the model in the order they are provided. The session ID of the model is set to the session ID of the first patch.
A collection of initial patches to apply to the model.
A model with the patches applied.
Static
Readonly
loadUn-serializes a model from "binary" structural encoding. The session ID of
the model is set to the provided session ID sid
, or the default session
ID of the un-serialized model is used.
Binary blob of a model encoded using "binary" structural encoding.
Optional
sid: numberSession ID to set for the model.
Optional
schema: SAn instance of a model.
Static
Readonly
withCreate a CRDT model which uses logical clock. Logical clock assigns a logical timestamp to every node and operation. Logical timestamp consists of a session ID and sequence number 2-tuple. Logical clocks allow to sync peer-to-peer.
Optional
clockOrSessionId: number | ClockVectorLogical clock to use.
CRDT model.
Use Model.create()
instead.
Static
Readonly
withCreate a CRDT model which uses server clock. In this model a central server timestamps each operation with a sequence number. Each timestamp consists simply of a sequence number, which was assigned by a server. In this model all operations are approved, persisted and re-distributed to all clients by a central server.
Latest known server sequence number.
CRDT model.
Use Model.create()
instead: Model.create(undefined, SESSION.SERVER)
.
In instance of Model class represents the underlying data structure, i.e. model, of the JSON CRDT document.