Skip to content

Sync Controllers

Sync controllers are implemented in src/sync.ts and provide local snapshot projection from one initial snapshot plus event updates.

FunctionReturnNotes
createLegatoSync(options?)LegatoSyncControllerUses options.client or defaults to Legato.
createAudioPlayerSync(options?)LegatoSyncControllerAlias that calls createLegatoSync(options).
OptionTypeRequiredDefaultDescription
clientAudioPlayerApiLegatoAPI client used for getSnapshot() and event subscriptions.
onSnapshot(snapshot) => voidCalled whenever controller publishes a new current snapshot.
onEvent(eventName, payload) => voidCalled on each event received through subscriptions.
interface LegatoSyncController {
start(): Promise<PlaybackSnapshot>;
resync(): Promise<PlaybackSnapshot>;
getCurrent(): PlaybackSnapshot | null;
stop(): Promise<void>;
}

src/__tests__/sync-behavior-parity.test.ts verifies:

  • start() performs resync() before adding listeners.
  • Queue/state/progress/active-track events project into the current snapshot.
  • Non-projecting events (for example playback-error, remote commands) trigger onEvent but do not mutate the snapshot.
  • stop() removes all registered listener handles.
const sync = createLegatoSync({ onSnapshot: (s) => setSnapshot(s) });
await sync.start();