Introducing a new way to review your PRs.

Read the announcement

crmne / zapfast

ZapFast: a fast, native WhatsApp client in Rust and egui

View on GitHub

19 documents · Updated Nov 18, 2026

docs / background-data-services

Background data services

Background data services are the backend workers that connect the client to WhatsApp and the local archive. They accept client commands, process phone events, run slow work away from the interface, and publish only the state that the client needs. Their main goal is continuity: local data remains useful across reconnects, delayed replies, account changes, and process restarts.

Worker runtime

Event loop

The worker runtime in src/…/worker.rs is the central coordinator for account data. It receives client commands and WhatsApp events through one loop, then uses timed work to retry, expire, or advance background tasks. This keeps network work and local state changes in one place while allowing long operations to finish in separate tasks.

The worker sends results back as client events. A task does not update the interface directly; it returns a tagged result to the worker, which checks that the result still belongs to the current account and operation before it changes local state.

This diagram shows how client actions and phone events meet in the worker before producing durable state and UI events.

flowchart LR
    C[Client commands] --> W[Background worker]
    P[Phone events] --> W
    T[Timer work] --> W
    W --> A[Local archive]
    W --> X[Network tasks]
    X --> W
    W --> U[Client events]

References: src/…/worker.rs• L855-1237 src/…/worker.rs• L1430-1508

Account boundaries

The worker treats each linked account as a separate session. It clears account-specific queues and in-memory state when an account logs out, and it tags asynchronous work so a late result from the old account cannot change the next account's archive. This boundary protects both local data and the meaning of delayed network replies.

The worker also opens the archive before it connects. If the archive cannot be unlocked, it does not connect with a temporary archive because a one-time history replay could then be stored in the wrong place or lost.

References: src/…/worker.rs• L862-903 src/…/worker.rs• L4654-4719 src/…/worker.rs• L1542-1548

Local state services

Archive and derived state

The local archive is the durable source for chats, messages, settings, and file references. The worker writes raw phone data and then derives the forms used by the client, such as chat lists, message content, unread state, contact names, and media paths. Startup repair and backfill work keep these derived views aligned when data rules change.

The worker emits a client event after a durable change. This order lets the interface rebuild from the archive after a restart instead of depending on an event that may have been missed.

References: src/…/worker.rs• L2520-2547 src/…/worker.rs• L2602-2773 src/…/worker.rs• L5078-5100

Event ordering

Phone events can arrive before the message or state row that they name. The worker holds a small amount of early state for reads and reactions, then applies it when the target message is filed. It replaces older reactions from the same sender and removes entries that wait too long or exceed the bound.

This prevents an event race from losing read state or reactions. It also avoids keeping unbounded data when the phone never sends the referenced message.

References: src/…/early_events.rs• L1-16 src/…/early_events.rs• L59-105 src/…/early_events.rs• L108-182 src/…/worker.rs• L6000-6058

Confirmed changes

Changes that affect both WhatsApp and local state use the phone's confirmation as the point of truth. The worker keeps a pending operation until the server accepts it, then updates the archive and notifies the client. This applies to actions such as chat archive state, read state, favorites, stars, pins, and disappearing-message settings.

The same rule prevents a send result from an old request from overwriting a newer choice. Operation generations, session markers, and one-at-a-time queues provide the checks needed when replies arrive late or out of order.

References: src/…/archive_chats.rs• L17-97 src/…/read_sync.rs• L1-77 src/…/favorite_chats.rs• L1-15 src/…/disappearing.rs• L1-12 src/…/worker.rs• L7413-7454

Background task coordination

History prefetch

History prefetch fills the archive with older messages without moving the reader's view. It builds a queue from chats already known locally, gives the focused chat priority, and sends only one background history request at a time. A delay between requests and a per-chat page limit reduce load on the phone.

The worker keeps background history separate from a reader's request. If the reader asks for the same chat, that request takes over and its result becomes visible. A timed-out background request can still be matched to a late reply, so it cannot be mistaken for a newer request.

The related page Event sync and recovery describes the wider synchronization and recovery flow.

References: src/…/prefetch.rs• L1-16 src/…/prefetch.rs• L24-45 src/…/prefetch.rs• L151-214 src/…/worker.rs• L6720-6781 src/…/worker.rs• L6850-7102

Scheduled helpers

The worker uses the same coordination model for other slow or ordered work. A timer tick advances queues for read-state writes, poll history, group information, sticker work, profile data, presence, and cleanup. Each helper can stop, retry, or wait without blocking unrelated client commands.

These services preserve the right order for data that has one shared value. For example, a favorite list is learned from the phone before local changes are sent back, and a failed read-state write pauses the shared queue rather than falsely marking the archive as synchronized.

References: src/…/worker.rs• L1213-1231 src/…/favorite_chats.rs• L20-49 src/…/favorite_chats.rs• L142-180 src/…/read_sync.rs• L10-75

Recovery work

Some background tasks repair continuity rather than fetch new user actions. At startup the worker re-derives archived rows, repairs media paths, removes abandoned staging files, and restores state that older versions did not record. When a phone setting or contact field is missing, it requests a one-time snapshot and records completion only after the snapshot succeeds.

This work is deliberately cautious. An incomplete recovery stays incomplete and retries after a later connection instead of treating missing data as a valid empty value.

References: src/…/worker.rs• L1086-1104 src/…/worker.rs• L2602-2662 src/…/contact_names.rs• L13-63 src/…/link_preview_setting.rs• L29-95

Data continuity boundaries

Synchronization boundary

Synchronization joins three states: the phone's current state, the local archive, and the client view. The worker accepts phone events into the archive, merges local pending changes only when their order is safe, and emits refreshed client state after the merge. Reconnects clear session-only assumptions but keep durable data and retryable work.

Read the child page Event sync and recovery for the detailed rules for history, app-state recovery, identity mapping, and delayed events.

References: src/…/worker.rs• L2400-2440 src/…/worker.rs• L4649-4705 src/…/worker.rs• L6720-6835

Download boundary

Downloads cross a second boundary between the archive and the file system. The worker treats a file as ready only after its archive reference is accepted, and it removes only unfinished staging files during cleanup. Stable account and chat folders keep downloaded data tied to the right account even when names or account identifiers change.

Read the child page Downloads, previews, and device state for download ownership, previews, cache behavior, and device-store continuity.

References: src/…/worker.rs• L116-146 src/…/worker.rs• L2549-2662 src/…/download_folders.rs• L1-18 src/…/download_folders.rs• L43-117 src/…/device_store.rs• L1-41

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the worker's session and generation checks aligned with every spawned task that can finish after logout or reconnect.
  • Preserve the distinction between a reader-visible request and a silent background request when changing history ownership or completion events.
  • Keep archive writes ahead of client events when changing derived chat, message, read, or media state.
  • Update the durable completion marker when changing a one-time snapshot recovery, or the worker may repeat or skip the recovery.
  • Keep download folder identity and archive media paths consistent when changing account identity or file cleanup rules.

Important failure modes

TriggerConsequenceGuard
The archive cannot be unlockedThe worker refuses to connect, so a one-time phone replay is not written to a disposable archive.Archive opening happens before connection setup.
A history reply arrives after its request times outThe reply could be applied to a newer request or make the reader display the wrong page.Request ownership uses request ids when available and keeps bounded late-request records.
A phone event names a message that is not filed yetThe read or reaction could disappear.Early events wait for the message and expire after a bounded age and count.
A network write failsLocal state could claim that the phone accepted a change.The archive changes only after confirmation, with backoff for shared queues.
An old account task finishes after logoutData from the old account could be written into the new account.Account sessions and operation generations reject stale results.
A download is interruptedA partial file could be treated as complete or cleanup could touch another account.Archive confirmation gates publication, and cleanup uses the fixed account/chat layout.

Key files

FileWhy to read it
src/…/worker.rsCentral worker lifecycle, event loop, account session boundary, archive writes, and history request ownership.
src/…/prefetch.rsBackground history queue, pacing, limits, and retry behavior.
src/…/read_sync.rsShared ordering and backoff for read and unread state writes.
src/…/early_events.rsBounded handling for phone events that arrive before their messages.
src/…/favorite_chats.rsExample of snapshot-first synchronization for one shared phone value.
src/…/download_folders.rsStable file-system ownership for account and chat downloads.
src/…/device_store.rsCompatibility boundary that keeps device data usable across library versions.