In instance of Model class represents the underlying data structure, i.e. model, of the JSON CRDT document.

Type Parameters

Implements

  • Printable

Constructors

Properties

Clock that keeps track of logical timestamps of the current editing session and logical clocks of all known peers.

onbeforepatch?: ((patch) => void) = undefined

Callback called before every applyPatch call.

Type declaration

    • (patch): void
    • Parameters

      Returns void

onbeforereset?: (() => void) = undefined

Callback called before model isi reset using the .reset() method.

Type declaration

    • (): void
    • Returns void

onpatch?: ((patch) => void) = undefined

Callback called after every applyPatch call.

Type declaration

    • (patch): void
    • Parameters

      Returns void

onreset?: (() => void) = undefined

Callback called after model has been reset using the .reset() method.

Type declaration

    • (): void
    • Returns void

root: RootNode<N> = ...

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.

sid: (() => number) = randomSessionId

Generates 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.

Type declaration

    • (): number
    • Returns number

Accessors

  • get s(): JsonNodeToProxyNode<N>
  • Experimental node retrieval API using proxy objects. Returns a strictly typed proxy wrapper around the value of the root node.

    Returns JsonNodeToProxyNode<N>

    Todo

    consider renaming this to _.

Methods

  • Applies a batch of patches to the document.

    Parameters

    • patches: Patch[]

      A batch, i.e. an array of patches.

    Returns void

  • Applies a single patch to the document. All mutations to the model must go through this method.

    Parameters

    Returns void

  • Creates a copy of this model with a new session ID. If the session ID is not provided, a random session ID is generated.

    Parameters

    • sessionId: number = ...

      Session ID to use for the new model.

    Returns Model<N>

    A copy of this model with a new session ID.

  • Strictly types the model and sets the default value of the model, if the document is empty.

    Type Parameters

    Parameters

    • schema: S

      The schema to set for this model.

    • useGlobalSession: boolean = true

    Returns Model<SchemaToJsonNode<S>>

    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.

    Parameters

    • sid: number

      The new session ID to set for the model.

    Returns void

  • Serialize this model using "binary" structural encoding.

    Returns Uint8Array

    This model encoded in octets.

  • Create 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).

    Examples

    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);

    Type Parameters

    Parameters

    • Optional schema: S

      The schema (typing and default value) to set for this model.

    • sidOrClock: number | ClockVector = ...

      Session ID to use for local operations. Defaults to a random session ID generated by Model.sid.

    Returns Model<SchemaToJsonNode<S>>

    A strictly typed model.

  • Decodes 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.

    Parameters

    • data: Uint8Array

      Binary blob of a model encoded using "binary" structural encoding.

    Returns Model<JsonNode<any>>

    An instance of a model.

  • Instantiates 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.

    Parameters

    • patches: Patch[]

      A collection of initial patches to apply to the model.

    Returns Model<JsonNode<any>>

    A model with the patches applied.

  • Un-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.

    Type Parameters

    Parameters

    • data: Uint8Array

      Binary blob of a model encoded using "binary" structural encoding.

    • Optional sid: number

      Session ID to set for the model.

    • Optional schema: S

    Returns Model<SchemaToJsonNode<S>>

    An instance of a model.

  • Create 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.

    Parameters

    • Optional clockOrSessionId: number | ClockVector

      Logical clock to use.

    Returns Model<JsonNode<any>>

    CRDT model.

    Deprecated

    Use Model.create() instead.

  • Create 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.

    Parameters

    • time: number = 1

      Latest known server sequence number.

    Returns Model<JsonNode<any>>

    CRDT model.

    Deprecated

    Use Model.create() instead: Model.create(undefined, SESSION.SERVER).