@statesync/core / createRevisionSync
Function: createRevisionSync()
ts
function createRevisionSync<T>(options): RevisionSyncHandle;Defined in: engine.ts:190
Creates a revision-based state synchronization loop.
The returned RevisionSyncHandle is inert until start() is called. Once started, the engine:
- Subscribes to invalidation events via the provided InvalidationSubscriber.
- Fetches the initial snapshot via the provided SnapshotProvider.
- Applies the snapshot via the provided SnapshotApplier if it is newer.
- On each subsequent invalidation event whose revision exceeds the local revision, triggers a new fetch-and-apply cycle (subject to optional throttling).
Concurrent refreshes are coalesced: at most one fetch is in flight at a time, and a trailing refresh is automatically queued if events arrive mid-flight.
Type Parameters
| Type Parameter | Description |
|---|---|
T | The application-specific snapshot data type. |
Parameters
| Parameter | Type | Description |
|---|---|---|
options | RevisionSyncOptions<T> | Configuration for the sync loop. See RevisionSyncOptions. |
Returns
A RevisionSyncHandle to control the sync lifecycle.
Throws
If options.topic is not a non-empty string (fails synchronously).
Example
ts
const handle = createRevisionSync<UserProfile>({
topic: 'user-profile',
subscriber: webSocketSubscriber,
provider: httpSnapshotProvider,
applier: { apply: ({ data }) => store.setState(data) },
});
await handle.start();
// Sync is now running. Call handle.stop() to tear down.