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

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

Dependency wrapper for the shared SyncState store.

Properties

syncState

readonly syncState: ReadonlyStore<
  | SyncState
| null>;

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

ReadonlyStore of the latest SyncState from these dependencies' workers, or null before any of them sends a snapshot. It lists every database the shared worker serves, other tabs' included. On the web, memory databases run in workers of their own, so it also lists the memory databases of instances these dependencies created. On the web, dependencies start the stored worker as soon as their initial storage check succeeds, even before an instance exists; when the browser refuses storage, the store includes only the memory databases of instances these dependencies created. Each worker reports its own transports, so one relay can appear once per worker. An app finds its own database 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 {
  RelayUrl,
  type SyncState,
  type SyncStateDep,
} from "@evolu/common/local-first";

const openRelayLabels = (deps: SyncStateDep): ReadonlyArray<string> => [
  ...new Set(
    (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: RelayUrl.orThrow("wss://relay.example/"),
      connection: {
        type: "Open",
        openedAt: Millis.orThrow(1000),
        error: null,
      },
    },
    {
      type: "WebSocket",
      id: createId<"SyncTransport">(deps),
      label: RelayUrl.orThrow("wss://relay.example/"),
      connection: {
        type: "Open",
        openedAt: Millis.orThrow(1000),
        error: null,
      },
    },
  ],
  tenants: [],
});
assertEqual(openRelayLabels({ syncState }), ["wss://relay.example/"]);