history is exposed to the user to manage undo/redo operations.
addOnlineClient adds the given clientID into the online client set.
applyChangePack applies the given change pack into this document.
applyDocEvent applies the given doc event into this document.
applyDocEventsForReplay applies the given events into this document.
applySnapshot applies the given snapshot into this document.
Optionalsnapshot: Uint8Array<ArrayBufferLike>applyWatchInit applies the watch initialization with the given client IDs.
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.
createChangePack create change pack of the local changes to send to the
remote server.
ensureClone make a clone of root.
garbageCollect purges elements that were removed before the given time.
getChangeID returns the change id of this document.
getCheckpoint returns the checkpoint of this document.
getClone returns this clone.
getCloneRoot returns clone object.
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).
getDocSize returns the size of this document.
getEpoch returns the document's last-known compaction epoch.
getGarbageLen returns the length of elements should be purged.
getGarbageLenFromClone returns the length of elements should be purged from clone.
getKey returns the key of this document.
getMaxSizePerDocument gets the maximum size of this document.
getMyPresence returns the presence of the current client.
getOthersForTest returns all the other clients in online, sorted by clientID.
getOthersPresences returns the presences of all other clients.
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.
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.
getPresence returns the presence of the given clientID.
getPresenceForTest returns the presence of the given clientID
regardless of whether the client is online or not.
getPresences returns the presences of online clients.
getRedoStackForTest returns the redo stack for test.
getRoot returns a new proxy of cloned root.
getRootCRDT returns the CRDTRoot for testing purposes.
This method is intended for internal testing only.
getRootObject returns root object.
getSchemaRules gets the schema rules of this document.
getSelfForTest returns the client that has attached this document.
getStats returns the statistics of this document.
getStatus returns the status of this document.
getUndoStackForTest returns the undo stack for test.
getValueByPath returns the JSONElement corresponding to the given path.
getVersionVector returns the version vector of document
hasLocalChanges returns whether this document has local changes or not.
hasPresence returns whether the given clientID has a presence or not.
isEnableDevtools returns whether devtools is enabled or not.
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.
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.
removeOnlineClient removes the clientID from the online client set.
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.
resetOnlineClients resets the online client set.
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.
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.
restoreMetaFromBytes applies the bytes produced by metaToBytes,
overwriting the checkpoint and changeID. Trailing blobs stay optional, the
same extension rule the toBytes envelope follows.
setActor sets actor into this document. This is also applied in the local
changes the document has.
setDisableGC records whether this document participates in GC. The
client calls this on attach so subsequent applyChange runs use the
lamport-only sync path.
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.
setDocID records the server-assigned document id so the next persisted
envelope carries it for the Tier-3 silent-purge guard.
setMaxSizePerDocument sets the maximum size of this document.
setOnlineClients sets the given online client set.
setSchemaRules sets the schema rules of this document.
subscribe registers a callback to subscribe to events on the document.
The callback will be called when the document is changed.
Optionalerror: ErrorFnOptionalcomplete: CompleteFnsubscribe registers a callback to subscribe to events on the document.
The callback will be called when the clients watching the document
establishe or update its presence.
Optionalerror: ErrorFnOptionalcomplete: CompleteFnsubscribe registers a callback to subscribe to events on the document.
The callback will be called when the current client establishes or updates its presence.
Optionalerror: ErrorFnOptionalcomplete: CompleteFnsubscribe registers a callback to subscribe to events on the document.
The callback will be called when the client establishes or terminates a connection,
or updates its presence.
Optionalerror: ErrorFnOptionalcomplete: CompleteFnsubscribe registers a callback to subscribe to events on the document.
The callback will be called when the stream connection status changes.
Optionalerror: ErrorFnOptionalcomplete: CompleteFnsubscribe registers a callback to subscribe to events on the document.
The callback will be called when the document status changes.
Optionalerror: ErrorFnOptionalcomplete: CompleteFnsubscribe registers a callback to subscribe to events on the document.
The callback will be called when the document is synced with the server.
Optionalerror: ErrorFnOptionalcomplete: CompleteFnsubscribe registers a callback to subscribe to events on the document.
The callback will be called when the targetPath or any of its nested values change.
Optionalerror: ErrorFnOptionalcomplete: CompleteFnsubscribe registers a callback to subscribe to events on the document.
The callback will be called when the authentification error occurs.
subscribe registers a callback to subscribe to events on the document.
The callback will be called when an epoch mismatch error occurs.
Optionalerror: ErrorFnsubscribe registers a callback to subscribe to events on the document.
The callback will be called when the offline-persistence layer had to
discard un-pushed local changes it could not reconcile (a data-loss event).
subscribe registers a callback to subscribe to events on the document.
The callback will be called when the offline-persistence layer stopped
persisting this document because writing it exceeds the configured budget.
Editing continues; durability does not.
subscribe registers a callback to subscribe to events on the document.
Optionalerror: ErrorFnOptionalcomplete: CompleteFntoBytes 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.
toJSForTest returns value with meta data for testing.
toJSON returns the JSON encoding of this document.
toSortedJSON returns the sorted JSON encoding of this document.
update executes the given updater to update this document.
Optionalmessage: stringStaticfrom
Documentis 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.