# `Yex.Sync.SharedDoc`
[🔗](https://github.com/satoren/y_ex/blob/main/lib/protocols/shared_doc.ex#L1)

This process handles messages for yjs protocol sync and awareness.
https://github.com/yjs/y-protocols

Persistence is supported by passing persistence module.
see Yex.Sync.SharedDoc.PersistenceBehaviour

If the observer process does not exist, it will automatically terminate.

# `launch_param`

```elixir
@type launch_param() ::
  {:doc_name, String.t()}
  | {:persistence, {module() | {module(), init_arg :: term()}}}
  | {:auto_exit, boolean()}
  | {:auto_exit_timeout, timeout()}
  | {:doc_option, Yex.Doc.Options.t()}
```

Launch Parameters

  * `:doc_name` - The name of the document.
  * `:persistence` - Persistence module that implements PersistenceBehaviour.
  * `:auto_exit` - Automatically terminate the SharedDoc process when there is no longer a process to receive update notifications.
  * `:auto_exit_timeout` - With `:auto_exit`, how long (in milliseconds) to wait for the first observer after start before terminating. Defaults to 5000. `:infinity` disables this check.
  * `:doc_option` - Options for the document.

# `child_spec`

# `get_doc`

Get the current state of the document.

Returns the Doc struct that represents the current state of the shared document.
Note: If you manipulate the structure obtained with this function from a different Node (Erlang VM node), some features may not work (e.g., observe). Please be careful.

# `observe`

Receive doc update notifications in the calling process.

# `process_message_v1`

# `send_yjs_message`

Send a message to the SharedDoc process.
message mus be represented in the Yjs protocol default format.
type supports sync and awareness.

# `start`

# `start_link`

# `start_sync`

Start the initial state exchange.

# `unobserve`

Stop receiving doc update notifications in the calling process.

If auto_exit is started with true(default), the SharedDoc process will automatically stop when there is no longer a process to receive update notifications.

# `update_doc`

Update the document with the given function.

The function should take a Doc struct as its argument and modify it as needed.
The timeout parameter specifies how long to wait for the update to complete (defaults to 5000ms).

Returns :ok on success or {:error, reason} on failure.

If the function raises, throws, or exits, the SharedDoc process keeps running and
`{:error, reason}` is returned. The update is not atomic: changes the function made
to the document before failing are kept and synced to observers, so retrying the
same function may apply those changes twice.

## Examples
    iex> {:ok, shared_doc} = SharedDoc.start_link(doc_name: "document_name")
    iex> SharedDoc.update_doc(shared_doc, fn doc -> Yex.Doc.get_array(doc, "array") |> Array.insert(0, "updated_data") end) # update the document
    :ok
    iex> SharedDoc.get_doc(shared_doc) |> Yex.Doc.get_array("array") |> Yex.Array.to_json() # check the update
    ["updated_data"]

---

*Consult [api-reference.md](api-reference.md) for complete listing*
