Yex.DocServer behaviour (y_ex v0.8.0)

View Source

Define a Yex.DocServer process.

DocServer defines a module to handle document update messages by yjs.

Examples

defmodule MyDocServer do
  use Yex.DocServer

  def init(_arg, state) do
    {:ok, state}
  end

  def handle_update_v1(doc, update, origin, state) do
    # Handle document updates from Yjs
    # - doc: The current document state
    # - update: Binary encoded update
    # - origin: Source of the update
    {:noreply, state}
  end

  def handle_awareness_change(awareness, %{removed: removed, added: added, updated: updated}, origin, state) do
    # Handle presence/awareness changes
    # - awareness: Current awareness state
    # - removed/added/updated: Lists of changed client IDs
    {:noreply, state}
  end
end

Summary

Callbacks

Handle regular GenServer call messages.

Handle regular GenServer cast messages.

Handle regular Elixir process messages.

Handle document updates in v1 encoding format.

Initialize the doc process.

Interprets and processes v1 encode message.

Starts a DocServer process without linking (for use outside of a supervision tree).

Starts a DocServer process linked to the current process.

Invoked when the document server process is about to exit.

Types

option()

@type option() ::
  {:doc_option, Yex.Doc.Options.t()} | {:assigns, map()} | {term(), term()}

options()

@type options() :: [option()]

Callbacks

handle_awareness_change(awareness, update, origin, state)

(optional)
@callback handle_awareness_change(
  awareness :: Yex.Awareness.t(),
  update :: %{removed: list(), added: list(), updated: list()},
  origin :: binary(),
  state :: Yex.DocServer.State.t()
) ::
  {:noreply, Yex.DocServer.State.t()}
  | {:stop, reason :: term(), Yex.DocServer.State.t()}

Handle awareness change

handle_call(msg, from, state)

(optional)
@callback handle_call(
  msg :: term(),
  from :: {pid(), tag :: term()},
  state :: Yex.DocServer.State.t()
) ::
  {:reply, response :: term(), Yex.DocServer.State.t()}
  | {:noreply, Yex.DocServer.State.t()}
  | {:stop, reason :: term(), Yex.DocServer.State.t()}

Handle regular GenServer call messages.

See GenServer.handle_call/3.

handle_cast(msg, state)

(optional)
@callback handle_cast(msg :: term(), state :: Yex.DocServer.State.t()) ::
  {:noreply, Yex.DocServer.State.t()}
  | {:stop, reason :: term(), Yex.DocServer.State.t()}

Handle regular GenServer cast messages.

See GenServer.handle_cast/2.

handle_info(msg, state)

(optional)
@callback handle_info(msg :: term(), state :: Yex.DocServer.State.t()) ::
  {:noreply, Yex.DocServer.State.t()}
  | {:stop, reason :: term(), Yex.DocServer.State.t()}

Handle regular Elixir process messages.

See GenServer.handle_info/2.

handle_update_v1(doc, update, origin, state)

(optional)
@callback handle_update_v1(
  doc :: Yex.Doc.t(),
  update :: binary(),
  origin :: binary(),
  state :: Yex.DocServer.State.t()
) ::
  {:noreply, Yex.DocServer.State.t()}
  | {:stop, reason :: term(), Yex.DocServer.State.t()}

Handle document updates in v1 encoding format.

Parameters

  • doc: Current document state
  • update: Binary encoded update from Yjs
  • origin: Source of the update (can be nil for local updates)
  • state: Current server state

Returns

  • {:noreply, state} to continue with new state
  • {:stop, reason, state} to stop the server

init(arg, state)

(optional)
@callback init(arg :: term(), state :: Yex.DocServer.State.t()) ::
  {:ok, Yex.DocServer.State.t()} | {:stop, reason :: term()}

Initialize the doc process.

process_message_v1(server, message, origin)

@callback process_message_v1(
  server :: GenServer.server(),
  message :: binary(),
  origin :: binary()
) :: :ok | {:ok, replies :: [binary()]} | {:error, term()}

Interprets and processes v1 encode message.

start(arg, genserver_option)

@callback start(
  arg :: options(),
  genserver_option :: GenServer.options()
) :: {:ok, pid()} | {:error, term()}

Starts a DocServer process without linking (for use outside of a supervision tree).

Returns the same success and error responses as start_link/2.

start_link(arg, genserver_option)

@callback start_link(
  arg :: options(),
  genserver_option :: GenServer.options()
) :: {:ok, pid()} | {:error, term()}

Starts a DocServer process linked to the current process.

terminate(reason, state)

(optional)
@callback terminate(
  reason :: :normal | :shutdown | {:shutdown, :left | :closed | term()},
  state :: Yex.DocServer.State.t()
) :: term()

Invoked when the document server process is about to exit.

See GenServer.terminate/2.