json-joy
    Preparing search index...

    Class ModelApi<N>

    Local changes API for a JSON CRDT model. This class is the main entry point for executing local user actions on a JSON CRDT document.

    Type Parameters

    Hierarchy (View Summary)

    Implements

    Index

    Constructors

    Properties

    api: ModelApi<any>
    builder: PatchBuilder

    Patch builder for the local changes.

    model: Model<N>

    Model instance on which the API operates.

    node: RootNode
    onBeforeLocalChange: FanOut<number> = ...

    Emitted before local changes through model.api are applied.

    onBeforePatch: FanOut<Patch> = ...

    Emitted before a patch is applied using model.applyPatch().

    onBeforeReset: FanOut<void> = ...

    Emitted before the model is reset, using the .reset() method.

    onBeforeTransaction: FanOut<void> = ...

    Emitted before a transaction is started.

    onChange: MergeFanOut<ChangeEvent> = ...

    Emitted when the model changes. Combines onReset, onPatch and onLocalChange.

    onChanges: MicrotaskBufferFanOut<unknown> = ...

    Emitted when the model changes. Same as .onChange, but this event is emitted once per microtask.

    onFlush: FanOut<Patch> = ...

    Emitted when the model.api builder change buffer is flushed.

    onLocalChange: FanOut<number> = ...

    Emitted after local changes through model.api are applied.

    onLocalChanges: MicrotaskBufferFanOut<number> = ...

    Emitted after local changes through model.api are applied. Same as .onLocalChange, but this event buffered withing a microtask.

    onPatch: FanOut<Patch> = ...

    Emitted after a patch is applied using model.applyPatch().

    onReset: FanOut<Set<JsonNode<unknown>>> = ...

    Emitted after the model is reset, using the .reset() method.

    onTransaction: FanOut<void> = ...

    Emitted after transaction completes.

    stopAutoFlush?: () => void = undefined

    Accessors

    Methods

    • Returns the API object of the extension if the node is an extension node. When the ext parameter is provided, it checks if the node is an instance of the given extension and returns the object's TypeScript type. Otherwise, it returns the API object of the extension, but without any type checking.

      Returns undefined | ExtApi<any>

      API of the extension

    • Returns the API object of the extension if the node is an extension node. When the ext parameter is provided, it checks if the node is an instance of the given extension and returns the object's TypeScript type. Otherwise, it returns the API object of the extension, but without any type checking.

      Type Parameters

      • EN extends ExtNode<any, any>
      • EApi extends ExtApi<EN>

      Parameters

      • ext: Extension<any, any, EN, EApi, any, any>

        Extension of the node

      Returns EApi

      API of the extension

    • Begins to automatically flush buffered operations into patches, grouping operations by microtasks or by transactions. To capture the patch, listen to the .onFlush event.

      Parameters

      • drainNow: boolean = false

      Returns () => void

      Callback to stop auto flushing.

    • Attaches a listener which executes on every change that is applied to this node's children. Hence, this listener will trigger only for container nodes - nodes that can have child nodes, such as "obj", "arr", "vec", and "val" nodes. It will not execute on changes made directly to this node.

      If you want to listen to changes on this node as well as its children, use onSubtreeChange() method. If you want to listen to changes on this node only, use onSelfChange() method.

      Parameters

      • listener: (event: ChangeEvent) => void

        Callback called on every change that is applied to children of this node.

      • OptionalonReset: boolean

        Optional parameter, if set to true, the listener will also be called when the model is reset using the .reset() method.

      Returns FanOutUnsubscribe

      Returns an unsubscribe function to stop listening to the events.

      • onSelfChange()
      • onSubtreeChange()
    • Attaches a listener which executes on every change that is executed directly on this node. For example, if this is a "str" string node and you insert or delete text, the listener will be executed. Or if this is an "obj" object node and keys of this object are changed, this listener will be executed.

      It does not trigger when child nodes are edit, to include those changes, use onSubtreeChange() or onChildChange() methods.

      Parameters

      • listener: (event: ChangeEvent) => void

        Callback called on every change that is executed directly on this node.

      • OptionalonReset: boolean

        Optional parameter, if set to true, the listener will also be called when the model is reset using the .reset() method.

      Returns FanOutUnsubscribe

      Returns an unsubscribe function to stop listening to the events.

      • onChildChange()
      • onSubtreeChange()
    • Attaches a listener which executes on every change that is applied to this node or any of its child nodes (recursively). This is equivalent to combining both onSelfChange() and onChildChange() methods.

      Parameters

      • listener: (event: ChangeEvent) => void

        Callback called on every change that is applied to this node or any of its child nodes.

      • OptionalonReset: boolean

        Optional parameter, if set to true, the listener will also be called when the model is reset using the .reset() method.

      Returns FanOutUnsubscribe

      Returns an unsubscribe function to stop listening to the events.

      • onSelfChange()
      • onChildChange()
    • Given a JSON/CBOR value, constructs CRDT nodes recursively out of it and sets the root node of the model to the constructed nodes.

      Parameters

      • json: unknown

        JSON/CBOR value to set as the view of the model.

      Returns this

      Reference to itself.

      Use .set() instead.