@yorkie-js/sdk
    Preparing search index...

    Class Document<R, P>

    Document is a CRDT-based data type. We can represent the model of the application and edit it even while offline. It implements Attachable interface to be managed by Attachment.

    Type Parameters

    Implements

    • Attachable
    Index

    Constructors

    Properties

    history: {
        canRedo: () => boolean;
        canUndo: () => boolean;
        redo: () => void;
        undo: () => void;
    }

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

    Methods

    • addOnlineClient adds the given clientID into the online client set.

      Parameters

      • clientID: string

      Returns void

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

      Returns void

    • applyDocEvent applies the given doc event into this document.

      Parameters

      • type: DocEventType
      • publisher: string

      Returns void

    • applySnapshot applies the given snapshot into this document.

      Parameters

      • serverSeq: bigint
      • snapshotVector: VersionVector
      • Optionalsnapshot: Uint8Array<ArrayBufferLike>
      • clientSeq: number = -1

      Returns void

    • applyWatchInit applies the watch initialization with the given client IDs.

      Parameters

      • clientIDs: string[]

      Returns void

    • clearHistory flushes both undo and redo stacks. This is used after applying a snapshot or initialRoot so that setup operations are not reachable via undo.

      Returns void

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

      Returns ChangePack<P>

    • getChangeID returns the change id of this document.

      Returns ChangeID

    • getCheckpoint returns the checkpoint of this document.

      Returns Checkpoint

    • getClone returns this clone.

      Returns { presences: Map<string, P>; root: CRDTRoot } | undefined

    • getCloneRoot returns clone object.

      Returns CRDTObject | undefined

    • getDocID returns the server-assigned document id recorded on attach, or an empty string before the first attach (or for a legacy envelope that predates docID persistence).

      Returns string

    • getEpoch returns the document's last-known compaction epoch.

      Returns bigint

    • getGarbageLen returns the length of elements should be purged.

      Returns number

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

      Returns number

    • getKey returns the key of this document.

      Returns string

    • getMaxSizePerDocument gets the maximum size of this document.

      Returns number

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

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

    • getOthersPresences returns the presences of all other clients.

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

    • getPendingChangesAfter returns the un-pushed local changes whose clientSeq is above the given one, each paired with that sequence.

      The pairing is the point: a ChangeStruct carries its clientSeq encoded inside the hex changeID, so a caller working from structs alone cannot tell which changes it has already seen without decoding them. The offline-persistence layer needs exactly that to append only what is new.

      Parameters

      • clientSeq: number

      Returns { clientSeq: number; struct: ChangeStruct<P> }[]

    • getPendingChangeStructs returns the serialized structs of the current un-pushed local changes. Used by the offline-persistence layer to carry the dropped changes in a LocalChangesDropped data-loss event so the app can surface (and optionally re-apply) edits that could not be reconciled.

      Returns ChangeStruct<P>[]

    • getPresence returns the presence of the given clientID.

      Parameters

      • clientID: string

      Returns P | undefined

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

      Parameters

      • clientID: string

      Returns P | undefined

    • getPresences returns the presences of online clients.

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

    • getRedoStackForTest returns the redo stack for test.

      Returns HistoryOperation<P>[][]

    • getRootCRDT returns the CRDTRoot for testing purposes. This method is intended for internal testing only.

      Returns CRDTRoot

    • getSchemaRules gets the schema rules of this document.

      Returns Rule[]

    • getSelfForTest returns the client that has attached this document.

      Returns { 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

    • 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

    • isPresenceDisabled returns the current presence-disabled state of this document. Reflects the server-fixated value once attached; before attach it reflects the local construction option.

      Returns boolean

    • metaToBytes serializes just the checkpoint and changeID — the client's position against the server — without touching the root.

      This is what the offline-persistence layer writes after a sync. A sync advances the checkpoint while leaving the document unchanged, so re-serializing the whole document to record it would cost time proportional to the document for information that is a few dozen bytes. Without it a restore would resume from whatever checkpoint the last snapshot happened to carry.

      Returns Uint8Array

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

      Parameters

      Returns void

    • removeOnlineClient removes the clientID from the online client set.

      Parameters

      • clientID: string

      Returns void

    • resetForReanchor drops all local state that was seeded from a stale persisted envelope so the document can be re-attached fresh. The server then re-anchors the client from the current snapshot. Used only on the store-backed attach path when the resume is rejected with ErrEpochMismatch: the persisted checkpoint/epoch/changeID and any un-pushed local changes are stale relative to the compacted document, so presenting them again would just be rejected. This mirrors constructing a brand-new Document instance without forcing the caller to swap the object reference it already holds.

      Returns void

    • restoreAppendedChanges replays changes that were recorded after the snapshot this document was restored from, as the offline-persistence layer's change log holds them.

      These are the opposite case to the pending changes carried inside a toBytes envelope. Those are already reflected in the snapshot's root — toBytes serializes the live root — so fromBytes queues them without applying. A change from the log was written after that root was captured, so it must be both applied, to bring the root forward, and queued, so it is still pushed. Doing only the first loses the edit on reconnect; doing only the second leaves the user looking at stale content.

      The log must be contiguous and ascending by clientSeq. A caller that cannot satisfy that should restore from the snapshot alone and report the loss rather than replaying a broken run.

      Parameters

      • structs: ChangeStruct<P>[]
      • ackedClientSeq: number = 0

      Returns void

    • restoreFromBytes rehydrates this document in place from the bytes produced by toBytes, overwriting the root, presences, checkpoint, changeID, and pending local changes. Unlike the static fromBytes, it mutates the existing instance so a document the caller already holds (and is about to attach) recovers its persisted, un-pushed state.

      Ordering: the caller must have stamped the actor (setActor) before calling this. The rehydrated changes and changeID carry the actor they were persisted under, and the snapshot's element actors are restored as persisted, so this does not rely on setActor rewriting existing element actors (a documented limitation of setActor).

      Actor guard: the persisted changeID must carry the same actor the caller just stamped. If a store is reused under a different clientKey the current stable actor differs from the persisted one; restoring anyway would stamp subsequent edits with the current actor while the restored root/changes keep the persisted actor, silently diverging the CRDT against a server that keys on the current actor. On mismatch this throws ErrClientNotActivated- free ErrInvalidArgument so the store-backed attach path can surface a data-loss event and re-anchor instead of corrupting state.

      Parameters

      • bytes: Uint8Array

      Returns void

    • restoreMetaFromBytes applies the bytes produced by metaToBytes, overwriting the checkpoint and changeID. Trailing blobs stay optional, the same extension rule the toBytes envelope follows.

      Parameters

      • bytes: Uint8Array

      Returns void

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

      Parameters

      • actorID: string

      Returns void

    • setDisableGC records whether this document participates in GC. The client calls this on attach so subsequent applyChange runs use the lamport-only sync path.

      Parameters

      • disableGC: boolean

      Returns void

    • setDisablePresence records the server-fixated presence-disabled state of this document. The client calls this on attach (before applyChangePack) so any subsequent Document.update invocation sees the gating state already settled. Flipping the flag at runtime is supported: the next update honours the new value.

      Parameters

      • disablePresence: boolean

      Returns void

    • setDocID records the server-assigned document id so the next persisted envelope carries it for the Tier-3 silent-purge guard.

      Parameters

      • docID: string

      Returns void

    • setMaxSizePerDocument sets the maximum size of this document.

      Parameters

      • size: number

      Returns void

    • setOnlineClients sets the given online client set.

      Parameters

      • onlineClients: Set<string>

      Returns void

    • setSchemaRules sets the schema rules of this document.

      Parameters

      • rules: Rule[]

      Returns void

    • toBytes serializes the full restorable state of this document into a self-contained byte envelope: the root and presences (as a snapshot), the checkpoint, the changeID (lamport, version vector, actor), and the pending local changes. fromBytes reverses it, so a document survives a reload with its un-pushed edits intact. The envelope is a length-prefixed concatenation of existing byte encodings rather than a new protobuf message.

      Returns Uint8Array

    • toSortedJSON returns the sorted JSON encoding of this document.

      Returns string

    • fromBytes reconstructs a document from the bytes produced by toBytes, restoring the root, presences, checkpoint, changeID, and pending local changes.

      Type Parameters

      Parameters

      • key: string
      • bytes: Uint8Array
      • Optionalopts: DocumentOptions

      Returns Document<R, P>