@datafn/client
Sync Engine
Sync engine internals reference.
SyncEngine
The SyncEngine class manages background synchronization between the client's local storage and the remote server.
Lifecycle
- Initialization -- created internally by
createDatafnClientwhensyncconfig is provided. - Start -- begins the hydration plan (clone boot resources, then background resources). Starts the push interval if configured.
- Running -- processes push cycles, responds to WebSocket cursor notifications, and handles connectivity changes.
- Destroy -- stops all timers, closes WebSocket, and cleans up event listeners.
Configuration
The engine reads from DatafnSyncConfig:
pushInterval-- ms between automatic push cycles.pushBatchSize-- max mutations per push batch (default: 100).pushMaxRetries-- max retries per push round (default: 3).wsReconnect-- exponential backoff for WebSocket reconnection.
Push Engine
The push engine reads entries from the offline changelog and sends them to the server:
- Read up to
pushBatchSizeentries fromstorage.changelogList(). - Send mutations to the server via
remote.push(). - On success, acknowledge entries via
storage.changelogAck(). - On failure, increment retry count and apply exponential backoff.
- Emit
sync_failedorsync_retryevents as appropriate.
Pull Engine
Triggered by WebSocket cursor notifications or manual calls:
- Read the current cursor from
storage.getCursor(). - Send a pull request with the cursor to
remote.pull(). - Apply received changes to local storage.
- Update the cursor monotonically.
- Emit
sync_appliedevents for each affected resource.
SyncFacade API
The client exposes a sync object with these methods:
interface SyncFacade {
/** Clone a resource from the server. */
clone(resource: string): Promise<void>;
/** Pull changes since the current cursor. */
pull(): Promise<void>;
/** Push pending offline mutations. */
push(): Promise<void>;
/** Clone and upload: full bidirectional sync initialization. */
cloneUp(options: CloneUpOptions): Promise<CloneUpResult>;
}WebSocket Reconnection
When the WebSocket disconnects, the engine applies exponential backoff with jitter:
delay = min(baseDelayMs * multiplier^attempt + random(0, jitterMs), maxDelayMs)Default configuration:
| Parameter | Default |
|---|---|
baseDelayMs | 1000 |
maxDelayMs | 60000 |
multiplier | 2 |
jitterMs | 500 |
On successful reconnection, the attempt counter resets to 0.
Hydration Plan
During initial startup:
- Boot resources -- clone sequentially. The client blocks until all boot resources reach
"ready"hydration state. - Background resources -- clone concurrently after boot completes. The client does not block on these.
Clone requests are paginated using clonePageSize (default or per-resource).