# `Emerge.Engine`
[🔗](https://github.com/emerge-elixir/emerge/blob/v0.2.1/lib/emerge/engine.ex#L1)

Retained tree diffing, encoding, and event routing helpers.

# `diff_state`

```elixir
@type diff_state() :: Emerge.Engine.DiffState.t()
```

Incremental diff state used between tree uploads and patches.

# `diff_state_new`

```elixir
@spec diff_state_new(Emerge.tree() | nil) :: diff_state()
```

Initialize a diff state for incremental updates.

# `diff_state_update`

```elixir
@spec diff_state_update(diff_state(), Emerge.tree()) ::
  {binary(), diff_state(), Emerge.tree()}
```

Compute patches for a new tree and return {patch_binary, next_state, assigned_tree}.

# `dispatch_click`

```elixir
@spec dispatch_click(diff_state(), binary()) :: :ok
```

Dispatch a click event to the handler registered for an element id.

# `dispatch_event`

```elixir
@spec dispatch_event(diff_state(), binary(), term()) :: :ok
```

Dispatch an element event to the handler registered for an element id.

# `dispatch_event`

```elixir
@spec dispatch_event(diff_state(), binary(), term(), term()) :: :ok
```

Dispatch an element event with payload to the registered handler.

# `encode_full`

```elixir
@spec encode_full(diff_state(), Emerge.tree()) ::
  {binary(), diff_state(), Emerge.tree()}
```

Encode a full tree with ids and return {binary, next_state, assigned_tree}.

# `encode_full_with_empty_patch`

```elixir
@spec encode_full_with_empty_patch(diff_state(), Emerge.tree()) ::
  {binary(), binary(), diff_state(), Emerge.tree()}
```

Encode a full tree and return {full_binary, patch_binary, next_state, assigned_tree}.

The patch binary is always empty for initial uploads.

# `lookup_event`

```elixir
@spec lookup_event(diff_state(), binary(), term()) :: {:ok, {pid(), term()}} | :error
```

Lookup the handler payload for an element event.

---

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