# `ExWapp.AppState.Engine.Sync`
[🔗](https://github.com/elchemista/ex_wapp/blob/v0.1.5/lib/ex_wapp/appstate/engine/sync.ex#L1)

Synchronization and transport helpers for app-state collections.

This module owns the IQ request/response flow with WhatsApp, including
collection fetches, mutation sends, external blob downloads, and the
sequencing required to apply snapshots and patch chains into the local store.

# `hash_state`

```elixir
@type hash_state() :: %{version: non_neg_integer(), hash: binary()}
```

# `patch_info`

```elixir
@type patch_info() :: ExWapp.AppState.Engine.Codec.patch_info()
```

# `apply_collection_response`

```elixir
@spec apply_collection_response(struct(), String.t(), ExWapp.Binary.Node.t()) ::
  {:ok, non_neg_integer()} | {:error, term()}
```

Applies a collection response payload to the local store.

# `apply_collection_response`

```elixir
@spec apply_collection_response(
  struct(),
  String.t(),
  ExWapp.Binary.Node.t(),
  pid() | nil
) ::
  {:ok, non_neg_integer()} | {:error, term()}
```

Applies a collection response payload and optionally uses a live session when
external blobs must be fetched while decoding snapshots or patch chains.

# `apply_collection_response`

```elixir
@spec apply_collection_response(
  struct(),
  String.t(),
  ExWapp.Binary.Node.t(),
  pid() | nil,
  keyword()
) :: {:ok, non_neg_integer()} | {:error, term()}
```

Applies a collection response using feature-specific persistence options.

The `:max_records` option is forwarded to call-log mutations.

# `fetch_collection`

```elixir
@spec fetch_collection(GenServer.server(), struct(), String.t()) ::
  :ok | {:error, term()}
```

Fetches a single app-state collection and applies its snapshot and patches.

# `fetch_collection`

```elixir
@spec fetch_collection(GenServer.server(), struct(), String.t(), keyword()) ::
  :ok | {:error, term()}
```

# `send_mutation`

```elixir
@spec send_mutation(GenServer.server(), patch_info(), struct()) ::
  {:ok, hash_state(), map()} | {:error, term()}
```

Synchronizes the affected collection, encodes and sends a mutation patch,
then applies the acknowledged change locally and schedules a background
refresh. Operations for the same store collection are serialized.

# `send_mutation`

```elixir
@spec send_mutation(GenServer.server(), patch_info(), struct(), keyword()) ::
  {:ok, hash_state(), map()} | {:error, term()}
```

---

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