ProsemirrorEx.Authority.Server (prosemirror_ex v0.3.0)

Copy Markdown View Source

GenServer wrapper around ProsemirrorEx.Authority.

Handles JSON serialization/deserialization at the boundary so callers work with plain maps. Suitable for integration with Phoenix channels, LiveView, or any Elixir web framework.

Supports process subscriptions for new-step notifications (the Elixir equivalent of the guide's onNewSteps callbacks / the collab demo's waiting clients).

Usage

{:ok, pid} = Authority.Server.start_link(schema: schema, doc: doc_node)

# Optionally subscribe this process to updates
:ok = Authority.Server.subscribe(pid)

# Receive steps from a client (steps as JSON maps)
{:ok, new_version} = Authority.Server.receive_steps(pid, "client1", 0, [step_json])

# Subscribers receive:
# {:authority_update, %{version: v, steps: steps_json, client_ids: ids}}

# Get the current document as JSON
{doc_json, version} = Authority.Server.get_doc(pid)

# Get step history since a version
{:ok, steps_json, client_ids} = Authority.Server.steps_since(pid, 0)

Summary

Functions

Returns a specification to start this module under a supervisor.

Get the current document as JSON and the current version.

Get the current authority version.

Start a linked Authority.Server process.

Get all steps since the given version as JSON maps.

Subscribe the calling process to authority updates.

Unsubscribe a process from authority updates.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

get_doc(server)

Get the current document as JSON and the current version.

Returns {doc_json, version}.

get_version(server)

Get the current authority version.

receive_steps(server, client_id, version, steps_json)

Submit steps from a client.

steps_json is a list of step JSON maps (as would arrive from a client). Steps are deserialized, validated, and applied.

On success, subscribed processes are notified with {:authority_update, %{version, steps, client_ids}}.

Returns {:ok, new_version} on success, or {:error, reason} / {:error, reason, message}.

start_link(opts)

Start a linked Authority.Server process.

Options

  • :schema (required) - the ProseMirror schema
  • :doc (optional) - initial document node
  • :max_history (optional) - max retained steps (see Authority.new/3)
  • :name (optional) - process name for registration

steps_since(server, version)

Get all steps since the given version as JSON maps.

Returns {:ok, steps_json, client_ids} or {:error, reason} (:invalid_version or :history_unavailable).

subscribe(server, subscriber \\ self())

Subscribe the calling process to authority updates.

The subscriber receives {:authority_update, payload} messages whenever new steps are accepted. The subscription is removed automatically when the subscriber exits.

unsubscribe(server, subscriber \\ self())

Unsubscribe a process from authority updates.