Document

class Document(key: String, options: Document.Options = Options()) : Closeable, Attachable

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

A single-threaded, Closeable is used as default. Therefore you need to close the document, when the document is no longer needed. If you provide your own dispatcher, it is up to you to decide close is needed or not.

Constructors

Link copied to clipboard
constructor(key: String, options: Document.Options = Options())

Types

Link copied to clipboard
data class BroadcastOptions(val maxRetries: Int = Int.MAX_VALUE)

BroadcastOptions are the options to create a new broadcast.

Link copied to clipboard
object Companion
Link copied to clipboard
interface DocumentHistory

Interface for undo/redo operations on a document.

Link copied to clipboard
sealed interface Event : ResourceEvent
Link copied to clipboard
data class Options @JvmOverloads constructor(val disableGC: Boolean = false, val disablePresence: Boolean = false, val maxUndoDepth: Int = MaxUndoRedoStackDepth)

Properties

Link copied to clipboard
Link copied to clipboard
val events: SharedFlow<Document.Event>
Link copied to clipboard
Link copied to clipboard

Provides undo/redo operations for local user changes in this document. The resulting document changes are synchronized normally.

Link copied to clipboard
Link copied to clipboard
val presences: StateFlow<Presences>

Functions

Link copied to clipboard
open override fun applyStatus(status: ResourceStatus)
Link copied to clipboard
open override fun close()

Releases this document's coroutine scope and dispatcher.

Link copied to clipboard
fun events(targetPath: String): Flow<Document.Event>

Subscribes to events on the document with the specific targetPath.

Link copied to clipboard
fun garbageCollect(minSyncedVersionVector: VersionVector): Int

Deletes elements that were removed before the given time.

Link copied to clipboard

getDocSize returns the size of this document.

Link copied to clipboard
open override fun getKey(): String

getKey returns the key of this document.

Link copied to clipboard

getMaxSizePerDocument gets the maximum size of this document.

Link copied to clipboard
suspend fun getRoot(): JsonObject

Returns a new proxy of cloned root.

Link copied to clipboard

getSchemaRules gets the schema rules of this document.

Link copied to clipboard
open override fun getStatus(): ResourceStatus

getStatus returns the status of this document.

Link copied to clipboard
suspend fun getValueByPath(path: String): JsonElement?

Returns the JsonElement corresponding to the path.

Link copied to clipboard

getVersionVector returns the version vector of document

Link copied to clipboard
open override fun hasLocalChanges(): Boolean

hasLocalChanges returns whether this document has local changes or not.

Link copied to clipboard

True once this document has queued a change carrying at least one operation -- local, undo/redo, or replayed from a store. Read by dev.yorkie.core.Client to decide whether this document's key takes the per-client pre-attach re-issue claim on attach or on detach. Ports yorkie-js-sdk document.ts's hasMintedOperations() (ed25355e, #1449, yorkie#2111).

Link copied to clipboard

True unless a resync has suspended minting on this document (see mintingSuspended) -- i.e. no resync is currently between its discard and its re-anchor. DocumentHistory.canUndo/DocumentHistory.canRedo gate on this in addition to their own stack checks, and updateAsync/undo-redo refuse with YorkieException.Code.ErrRefused via ensureMintableAtEntry while it is false.

Link copied to clipboard
suspend fun metaToBytes(): ByteArray

Serializes just the checkpoint and changeID — plus the compaction epoch and docId — the client's position against the server, without touching the root. Mirrors JS metaToBytes.

Link copied to clipboard
open override fun publish(event: ResourceEvent)

publish publishes an event to notify observers about changes in this resource.

Link copied to clipboard
suspend fun publishEvent(event: Document.Event)
Link copied to clipboard
open override fun setActor(actorID: String)

Sets actorID into this document. This is also applied in the localChanges the document has.

Link copied to clipboard

setMaxSizePerDocument sets the maximum size of this document.

Link copied to clipboard
fun setSchemaRules(rules: List<Rule>)

setSchemaRules sets the schema rules of this document.

Link copied to clipboard
suspend fun toBytes(): ByteArray

Serializes this document's full restorable state — root, presences, checkpoint, changeID, pending changes, compaction epoch, and docId — into a self-contained byte envelope. The reverse of Companion.fromBytes.

Link copied to clipboard
fun toJson(): String
Link copied to clipboard
fun updateAsync(message: String? = null, updater: suspend (root: JsonObject, presence: DocPresence) -> Unit): Deferred<OperationResult>
fun updateAsync(message: String? = null, skipHistory: Boolean, updater: suspend (root: JsonObject, presence: DocPresence) -> Unit): Deferred<OperationResult>

Executes the given updater to update this document.