Skip to content

createSyncCoordinator ​

ts
import { createSyncCoordinator } from '@signaldb/sync'

const coordinator = createSyncCoordinator('my-app')

Creates the coordinator that lets the SyncManagers of several instances of an app — browser tabs, windows or workers — share one sync. Pass it to every instance's SyncManager as coordinator, and create the synced collections with coordinator.wrap(dataAdapter). The guide Multiple Tabs shows the complete setup.

The coordinator is built on the Web Locks API. Where it is missing — React Native, Node, or a browser without Web Locks — every instance is elected and nothing is coordinated, which is what a single instance needs.

Parameters ​

  • name (string): A name shared by all instances of the app. It prefixes the two locks the coordinator uses, so two apps on the same origin need different names.

Returns ​

A SyncCoordinator:

wrap(dataAdapter) ​

Returns a data adapter that forwards everything to dataAdapter. Its writes wait while the elected instance applies pulled data, and the elected instance waits until the change of a write is recorded before applying pulled data. Without this, pulled data could briefly overwrite a write of another instance whose change was not recorded yet. Create every synced collection with the wrapped adapter. Pass the unwrapped one to the SyncManager as dataAdapter.

  • dataAdapter (DataAdapter): The data adapter the collections would use otherwise.

elect(onChange), exclusive(callback), hold() ​

Called by the SyncManager; you don't call them yourself. They are part of the SyncCoordinator type so that an environment without Web Locks can provide its own implementation: elect reports whether this instance leads, exclusive keeps writes of the other instances from being applied while the elected one applies pulled data, and hold keeps the elected instance from applying pulled data while a write is being recorded.

Released under the MIT License.