View Source Yex.Sync.SharedDoc.PersistenceBehaviour behaviour (y_ex v0.6.3)

Persistence behavior for SharedDoc

Summary

Callbacks

Invoked to handle SharedDoc bind. Mainly used to read and set the initial values of SharedDoc

Invoked to handle SharedDoc terminate.

Invoked to handle all doc updates.

Callbacks

Link to this callback

bind(state, doc_name, doc)

View Source
@callback bind(state :: term(), doc_name :: String.t(), doc :: Yex.Doc.t()) :: term()

Invoked to handle SharedDoc bind. Mainly used to read and set the initial values of SharedDoc

Examples save the state to the filesystem

def bind(state, doc_name, doc) do

case File.read("path/to/" <> doc_name, [:read, :binary]) do
  {:ok, data} ->
    Yex.apply_update(doc, data)

  {:error, _} ->
    :ok
end

end

Link to this callback

unbind(state, doc_name, doc)

View Source (optional)
@callback unbind(state :: term(), doc_name :: String.t(), doc :: Yex.Doc.t()) :: :ok

Invoked to handle SharedDoc terminate.

This is only executed when SharedDoc exits successfully.

Examples save the state to the filesystem

def unbind(state, doc_name, doc) do
  case Yex.encode_state_as_update(doc) do
    {:ok, update} ->
      File.write!("path/to/" <> doc_name, update, [:write, :binary])
    error ->
      error
  end

  :ok
end
Link to this callback

update_v1(state, update, doc_name, doc)

View Source (optional)
@callback update_v1(
  state :: term(),
  update :: binary(),
  doc_name :: String.t(),
  doc :: Yex.Doc.t()
) :: term()

Invoked to handle all doc updates.