API reference › @evolu/common › local‑first/Evolu › SyncStateDep

Defined in: packages/common/src/local-first/Evolu.ts:1126

Dependency wrapper for the shared SyncState store.

Properties

syncState

readonly syncState: ReadonlyStore<
  | SyncState
| null>;

Defined in: packages/common/src/local-first/Evolu.ts:1182

ReadonlyStore of the latest SyncState shared by all Evolu instances, or null before the shared worker sends its first snapshot. It lists every database the shared worker serves, including other tabs', so an app finds its own by Evolu.name. Apps show users an owner's OwnerSyncStatus, from syncStateToOwnerSyncStatus or a framework binding's useOwnerSyncStatus; views of each relay use syncStateToRelaySyncStates.

Each snapshot keeps the previous snapshot's object for every part that did not change, and a snapshot equal to the previous one leaves the store unchanged, so parts and derived statuses can be compared with ===.

Example

import {
  assertEqual,
  createId,
  createStore,
  Millis,
  testCreateDeps,
} from "@evolu/common";
import type { SyncState, SyncStateDep } from "@evolu/common/local-first";

const openRelayLabels = (deps: SyncStateDep): ReadonlyArray<string> =>
  (deps.syncState.get()?.transports ?? [])
    .filter(({ connection }) => connection.type === "Open")
    .map(({ label }) => label);

using syncState = createStore<SyncState | null>(null);
assertEqual(openRelayLabels({ syncState }), []);

const deps = testCreateDeps();
syncState.set({
  transports: [
    {
      type: "WebSocket",
      id: createId<"SyncTransport">(deps),
      label: "wss://relay.example",
      connection: {
        type: "Open",
        openedAt: Millis.orThrow(1000),
        error: null,
      },
    },
  ],
  tenants: [],
});
assertEqual(openRelayLabels({ syncState }), ["wss://relay.example"]);