Skip to content

@statesync/core


@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:

  1. Subscribes to invalidation events via the provided InvalidationSubscriber.
  2. Fetches the initial snapshot via the provided SnapshotProvider.
  3. Applies the snapshot via the provided SnapshotApplier if it is newer.
  4. 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 ParameterDescription
TThe application-specific snapshot data type.

Parameters

ParameterTypeDescription
optionsRevisionSyncOptions<T>Configuration for the sync loop. See RevisionSyncOptions.

Returns

RevisionSyncHandle

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.

Released under the MIT License.