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

Yex is wrapper library for the Yjs CRDT library.

# `any_type`

```elixir
@type any_type() :: binary() | map() | list() | integer() | float() | boolean() | nil
```

# `input_type`

```elixir
@type input_type() :: any_type() | Yex.PrelimType.t()
```

# `apply_update`

```elixir
@spec apply_update(Yex.Doc.t(), binary()) :: :ok | {:error, term()}
```

Apply a document update on the shared document.

## Examples syncs two clients by exchanging the complete document structure
    iex> doc1 = Yex.Doc.new()
    iex> doc2 = Yex.Doc.new()
    iex> {:ok, state1} = Yex.encode_state_as_update(doc1)
    iex> {:ok, state2} = Yex.encode_state_as_update(doc2)
    iex> Yex.apply_update(doc1, state2)
    :ok
    iex> Yex.apply_update(doc2, state1)
    :ok

# `apply_update_v1`

```elixir
@spec apply_update_v1(Yex.Doc.t(), binary()) :: :ok | {:error, term()}
```

# `apply_update_v2`

```elixir
@spec apply_update_v2(Yex.Doc.t(), binary()) :: :ok | {:error, term()}
```

# `encode_state_as_update`

```elixir
@spec encode_state_as_update(Yex.Doc.t(), binary() | nil) ::
  {:ok, binary()} | {:error, term()}
```

Encode the document state as a single update message that can be applied on the remote document. Optionally, specify the target state vector to only write the missing differences to the update message.

## Examples
    iex> doc = Yex.Doc.new()
    iex> {:ok, _binary} = Yex.encode_state_as_update(doc)

# `encode_state_as_update!`

```elixir
@spec encode_state_as_update!(Yex.Doc.t(), binary() | nil) :: binary()
```

@see encode_state_as_update/2

# `encode_state_as_update_v1`

# `encode_state_as_update_v2`

# `encode_state_vector`

```elixir
@spec encode_state_vector(Yex.Doc.t()) :: {:ok, binary()} | {:error, term()}
```

Computes the state vector and encodes it into an Binary.
A state vector describes the state of the local client.
The remote client can use this to exchange only the missing differences.

## Examples
    iex> doc = Yex.Doc.new()
    iex> {:ok, _binary} = Yex.encode_state_vector(doc)

# `encode_state_vector!`

```elixir
@spec encode_state_vector!(Yex.Doc.t()) :: binary()
```

@see encode_state_vector/1

# `encode_state_vector_v1`

# `encode_state_vector_v2`

# `get_pending_ds`

```elixir
@spec get_pending_ds(Yex.Doc.t()) :: {:ok, binary() | nil} | {:error, term()}
```

Returns the pending delete set (v1 encoded) for the document, if any.

A pending delete set holds deletions that arrived out of order and could
not yet be applied. Returns `{:ok, binary()}` when a pending delete set
exists, `{:ok, nil}` otherwise.

# `get_pending_update`

```elixir
@spec get_pending_update(Yex.Doc.t()) :: {:ok, binary() | nil} | {:error, term()}
```

Returns the pending update (v1 encoded) for the document, if any.

A pending update accumulates operations that arrived out of order and
could not yet be applied because their causal predecessors are missing.
Returns `{:ok, binary()}` when a pending update exists, `{:ok, nil}` otherwise.

# `merge_updates`

```elixir
@spec merge_updates([binary()]) :: {:ok, binary()} | {:error, term()}
```

# `merge_updates_v1`

```elixir
@spec merge_updates_v1([binary()]) :: {:ok, binary()} | {:error, term()}
```

# `merge_updates_v2`

```elixir
@spec merge_updates_v2([binary()]) :: {:ok, binary()} | {:error, term()}
```

# `normalize`

Normalize a number to a format that can be used in Yjs.

# `snapshot`

```elixir
@spec snapshot(Yex.Doc.t()) :: {:ok, Yex.Snapshot.t()} | {:error, term()}
```

Creates a snapshot of the current document state.

The returned snapshot can later be used with
`Yex.Snapshot.encode_state_v1/1` or `Yex.Snapshot.encode_state_v2/1`
to produce a time-travel update for that point-in-time state.

The source document must have garbage collection disabled
(`Yex.Doc.Options.skip_gc == true`) for snapshot-based time-travel encoding.

# `update_debug_v1`

```elixir
@spec update_debug_v1(binary()) :: {:ok, String.t()} | {:error, term()}
```

Parses an update payload encoded in lib0 v1 format and returns a human-readable debug string.

# `update_debug_v2`

```elixir
@spec update_debug_v2(binary()) :: {:ok, String.t()} | {:error, term()}
```

Parses an update payload encoded in lib0 v2 format and returns a human-readable debug string.

---

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