Class Document<T, P>

Document is a CRDT-based data type. We can represent the model of the application and edit it even while offline.

Type Parameters

Constructors

Properties

changeID: ChangeID
checkpoint: Checkpoint
clone?: {
    presences: Map<string, P>;
    root: CRDTRoot;
}

Type declaration

  • presences: Map<string, P>
  • root: CRDTRoot
eventStreamObserver: Observer<TransactionEvent<P>>
history: {
    canRedo: (() => boolean);
    canUndo: (() => boolean);
    redo: (() => void);
    undo: (() => void);
}

history is exposed to the user to manage undo/redo operations.

Type declaration

  • canRedo: (() => boolean)
      • (): boolean
      • canRedo returns whether there are any operations to redo.

        Returns boolean

  • canUndo: (() => boolean)
      • (): boolean
      • canUndo returns whether there are any operations to undo.

        Returns boolean

  • redo: (() => void)
      • (): void
      • redo redoes the last operation executed by the current client. It does not impact operations made by other clients.

        Returns void

  • undo: (() => void)
      • (): void
      • undo undoes the last operation executed by the current client. It does not impact operations made by other clients.

        Returns void

internalHistory: History<P>

internalHistory is used to manage undo/redo operations internally.

isUpdating: boolean

isUpdating is whether the document is updating by updater or not. It is used to prevent the updater from calling undo/redo.

key: string
localChanges: Change<P>[]
onlineClients: Set<string>

onlineClients is a set of client IDs that are currently online.

opts: DocumentOptions
presences: Map<string, P>

presences is a map of client IDs to their presence information.

root: CRDTRoot

Methods

  • Internal

    addOnlineClient adds the given clientID into the online client set.

    Parameters

    • clientID: string

    Returns void

  • Internal

    applyChangePack applies the given change pack into this document.

    1. Remove local changes applied to server.
    2. Update the checkpoint.
    3. Do Garbage collection.

    Parameters

    • pack: ChangePack<P>

      change pack

    Returns void

  • applySnapshot applies the given snapshot into this document.

    Parameters

    • serverSeq: Long
    • Optional snapshot: Uint8Array

    Returns void

  • applyWatchStream applies the given watch stream response into this document.

    Parameters

    • resp: WatchDocumentResponse

    Returns void

  • broadcast the payload to the given topic.

    Parameters

    • topic: string
    • payload: Json
    • Optional options: BroadcastOptions

    Returns void

  • canRedo returns whether there are any operations to redo.

    Returns boolean

  • canUndo returns whether there are any operations to undo.

    Returns boolean

  • Internal

    createChangePack create change pack of the local changes to send to the remote server.

    Returns ChangePack<P>

  • Internal

    garbageCollect purges elements that were removed before the given time.

    Parameters

    Returns number

  • Internal

    getChangeID returns the change id of this document.

    Returns ChangeID

  • Internal

    getCheckpoint returns the checkpoint of this document.

    Returns Checkpoint

  • Internal

    getClone return clone object.

    Returns undefined | CRDTObject

  • Internal

    getGarbageLen returns the length of elements should be purged.

    Returns number

  • getGarbageLenFromClone returns the length of elements should be purged from clone.

    Returns number

  • Internal

    getOthersForTest returns all the other clients in online, sorted by clientID.

    Returns {
        clientID: string;
        presence: P;
    }[]

  • getPresence returns the presence of the given clientID.

    Parameters

    • clientID: string

    Returns undefined | P

  • Internal

    getPresenceForTest returns the presence of the given clientID regardless of whether the client is online or not.

    Parameters

    • clientID: string

    Returns undefined | P

  • getPresences returns the presences of online clients.

    Returns {
        clientID: string;
        presence: P;
    }[]

  • getRedoStackForTest returns the redo stack for test.

    Returns HistoryOperation<P>[][]

  • Internal

    getRootObject returns root object.

    Returns CRDTObject

  • Internal

    getSelfForTest returns the client that has attached this document.

    Returns {
        clientID: string;
        presence: P;
    }

    • clientID: string
    • presence: P
  • getUndoStackForTest returns the undo stack for test.

    Returns HistoryOperation<P>[][]

  • hasLocalChanges returns whether this document has local changes or not.

    Returns boolean

  • Internal

    hasPresence returns whether the given clientID has a presence or not.

    Parameters

    • clientID: string

    Returns boolean

  • isEnableDevtools returns whether devtools is enabled or not.

    Returns boolean

  • Parameters

    • elem: string
    • parent: string

    Returns boolean

  • publish triggers an event in this document, which can be received by callback functions from document.subscribe().

    Parameters

    Returns void

  • redo redoes the last operation executed by the current client. It does not impact operations made by other clients.

    Returns void

  • Internal

    removeOnlineClient removes the clientID from the online client set.

    Parameters

    • clientID: string

    Returns void

  • Internal

    resetOnlineClients resets the online client set.

    Returns void

  • Internal

    setActor sets actor into this document. This is also applied in the local changes the document has.

    Parameters

    • actorID: string

    Returns void

  • Internal

    setOnlineClients sets the given online client set.

    Parameters

    • onlineClients: Set<string>

    Returns void

  • toSortedJSON returns the sorted JSON encoding of this document.

    Returns string

  • undo undoes the last operation executed by the current client. It does not impact operations made by other clients.

    Returns void

  • update executes the given updater to update this document.

    Parameters

    • updater: ((root, presence) => void)
        • (root, presence): void
        • Parameters

          Returns void

    • Optional message: string

    Returns void