# `Yex.DocServer`
[🔗](https://github.com/satoren/y_ex/blob/main/lib/server/doc_server.ex#L1)

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_update(awareness, %{removed: removed, added: added, updated: updated}, origin, state) do
        # Handle presence/awareness update
        # - awareness: Current awareness state
        # - removed/added/updated: Lists of changed client IDs
        {:noreply, state}
      end
    end

# `option`

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

# `options`

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

# `handle_awareness_change`
*optional* 

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

Handle awareness change

# `handle_awareness_update`
*optional* 

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

Handle awareness update

# `handle_call`
*optional* 

```elixir
@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 `c:GenServer.handle_call/3`.

# `handle_cast`
*optional* 

```elixir
@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 `c:GenServer.handle_cast/2`.

# `handle_info`
*optional* 

```elixir
@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 `c:GenServer.handle_info/2`.

# `handle_update_v1`
*optional* 

```elixir
@callback handle_update_v1(
  doc :: Yex.Doc.t(),
  update :: binary(),
  origin :: term(),
  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). An undo or
  redo arrives as the `%Yex.UndoManager{}` that ran it.
- state: Current server state

## Returns
- `{:noreply, state}` to continue with new state
- `{:stop, reason, state}` to stop the server

# `init`
*optional* 

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

Initialize the doc process.

# `process_message_v1`

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

Interprets and processes v1 encode message.

# `start`

```elixir
@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`

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

Starts a `DocServer` process linked to the current process.

# `terminate`
*optional* 

```elixir
@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 `c:GenServer.terminate/2`.

---

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