API reference › @evolu/common › local‑first/Shared › syncStateToRelaySyncStates

function syncStateToRelaySyncStates(
  state: SyncState | null,
  name: string & Brand<"UrlSafeString"> & Brand<"Name">,
  ownerId: string & Brand<"Id"> & Brand<"OwnerId">,
): readonly RelaySyncState[];

Defined in: packages/common/src/local-first/Shared.ts:1152

Pairs each route of an owner in a database with its transport, in route order. Returns none for a null snapshot, a missing or refused database, and a missing or readonly owner.

Example

import {
  assertEqual,
  createId,
  Millis,
  testCreateDeps,
  testName,
} from "@evolu/common";
import {
  relaySyncStateToStatus,
  syncStateToRelaySyncStates,
  testAppOwner,
  type SyncRoute,
  type SyncState,
  type SyncTransport,
} from "@evolu/common/local-first";

const deps = testCreateDeps();
const transport: SyncTransport = {
  type: "WebSocket",
  id: createId<"SyncTransport">(deps),
  label: "wss://relay.example",
  connection: {
    type: "Open",
    openedAt: Millis.orThrow(800),
    error: null,
  },
};
const route: SyncRoute = {
  type: "Complete",
  transportId: transport.id,
  completeAt: Millis.orThrow(1000),
  lastSentAt: Millis.orThrow(900),
  lastReceivedAt: Millis.orThrow(1000),
};
const state: SyncState = {
  transports: [transport],
  tenants: [
    {
      type: "Active",
      name: testName,
      owners: [{ type: "Writable", ownerId: testAppOwner.id, routes: [route] }],
    },
  ],
};

const relays = syncStateToRelaySyncStates(state, testName, testAppOwner.id);
assertEqual(relays, [{ transport, route }]);
assertEqual(relays.map(relaySyncStateToStatus), [{ type: "Synced" }]);
assertEqual(syncStateToRelaySyncStates(null, testName, testAppOwner.id), []);