Lightning.Collaboration.DocumentState (Lightning v2.15.0-pre5)

View Source

Schema for persisting Y.js collaborative document states.

Supports multiple record types for batched persistence:

  • "update": Individual or batched updates
  • "checkpoint": Full document state snapshot
  • "state_vector": Current state vector for efficient syncing

Summary

Functions

Applies persisted state (checkpoint + updates) to a Yex document.

Retrieves the latest checkpoint and all updates since that checkpoint for a given document.

Retrieves the latest checkpoint for a document, or nil if none exists.

Retrieves all updates for a document inserted after the given timestamp, ordered chronologically (oldest first).

Loads all persisted state for a document and applies it to a Yex document.

Types

t()

@type t() :: %Lightning.Collaboration.DocumentState{
  __meta__: term(),
  document_name: String.t() | nil,
  id: integer() | nil,
  inserted_at: DateTime.t() | nil,
  state_data: binary() | nil,
  state_vector: binary() | nil,
  updated_at: DateTime.t() | nil,
  version: :update | :checkpoint | :state_vector | nil
}

Functions

apply_to_doc(doc, checkpoint, updates)

@spec apply_to_doc(Yex.Doc.t(), t() | nil, [t()]) :: :ok

Applies persisted state (checkpoint + updates) to a Yex document.

Applies the checkpoint first (if present), then all updates in chronological order.

changeset(document_state, attrs)

get_checkpoint_and_updates(doc_name)

@spec get_checkpoint_and_updates(String.t()) ::
  {:ok, t() | nil, [t()]} | {:error, :not_found}

Retrieves the latest checkpoint and all updates since that checkpoint for a given document.

Returns {:ok, checkpoint, updates} where checkpoint may be nil, or {:error, :not_found} if no persisted state exists.

get_latest_checkpoint(doc_name)

@spec get_latest_checkpoint(String.t()) :: t() | nil

Retrieves the latest checkpoint for a document, or nil if none exists.

get_updates_since(doc_name, since)

@spec get_updates_since(String.t(), DateTime.t()) :: [t()]

Retrieves all updates for a document inserted after the given timestamp, ordered chronologically (oldest first).

load_into_doc(doc, doc_name)

@spec load_into_doc(Yex.Doc.t(), String.t()) :: :ok

Loads all persisted state for a document and applies it to a Yex document.

Convenience function that combines get_checkpoint_and_updates/1 and apply_to_doc/3.