# `BaileysEx.Feature.AppState`
[🔗](https://github.com/jeffhuen/baileys_ex/blob/main/lib/baileys_ex/feature/app_state.ex#L1)

App state sync (Syncd) — runtime orchestration for cross-device state
synchronization.

Provides patch construction, outbound push (`app_patch/3`), full resync
(`resync_app_state/4`), and the high-level `chat_modify/4` entry point.
Delegates encoding/decoding to `Syncd.Codec` and action mapping to
`Syncd.ActionMapper`.

Ports `appPatch`, `resyncAppState`, `chatModify` from Baileys `chats.ts:465-902`.

# `operation`

```elixir
@type operation() :: :set | :remove
```

# `patch`

```elixir
@type patch() :: %{
  sync_action: map(),
  index: [String.t()],
  type: patch_type(),
  api_version: pos_integer(),
  operation: operation()
}
```

# `patch_type`

```elixir
@type patch_type() ::
  :critical_block
  | :critical_unblock_low
  | :regular_high
  | :regular_low
  | :regular
```

# `app_patch`

```elixir
@spec app_patch(
  term(),
  GenServer.server() | BaileysEx.Connection.Store.Ref.t(),
  patch(),
  keyword()
) :: :ok | {:error, term()}
```

Send an outbound app state patch to the server.

Acquires the current sync version, encodes the patch, sends it via IQ,
and persists the updated state.

Ports `appPatch` from `chats.ts:779-853`.

## Parameters

  * `queryable` — function or `{module, pid}` for sending IQ queries
  * `store` — Store server or Ref
  * `patch_create` — `%{type:, index:, sync_action:, api_version:, operation:}`
  * `opts` — options:
    - `:emit_own_events` — boolean (default `true`)
    - `:event_emitter` — pid
    - `:me` — current user contact info

# `build_patch`

```elixir
@spec build_patch(atom(), String.t(), term(), keyword()) :: patch()
```

Construct the app-state patch structure without sending it.

# `chat_modify`

```elixir
@spec chat_modify(
  term(),
  GenServer.server() | BaileysEx.Connection.Store.Ref.t(),
  atom(),
  String.t(),
  term(),
  keyword()
) :: :ok | {:error, term()}
```

High-level chat modification — builds a patch and sends it.

Ports `chatModify` from `chats.ts:899-902`.

# `push_patch`

```elixir
@spec push_patch(term(), atom(), String.t(), term(), keyword()) ::
  {:ok, patch()} | {:error, term()}
```

Build a Baileys-style app-state patch for the given action and pass it to the
provided transport callback when one is supplied.

# `resync_app_state`

```elixir
@spec resync_app_state(term(), GenServer.server(), [patch_type()], keyword()) ::
  :ok | {:error, term()}
```

Full resync of app state collections from the server.

Fetches snapshots and patches for each collection, decodes them, persists
the updated LTHash state, and emits sync action events.

Ports `resyncAppState` from `chats.ts:479-632`.

## Parameters

  * `queryable` — function or `{module, pid}` for sending IQ queries
  * `store` — Store server or Ref for key/version persistence
  * `collections` — list of collection names to sync
  * `opts` — options:
    - `:is_initial_sync` — boolean (default `false`)
    - `:event_emitter` — pid to receive events
    - `:me` — current user contact info
    - `:validate_snapshot_macs` — boolean (default `false`)
    - `:validate_patch_macs` — boolean (default `false`)

---

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