TinyBase logoTinyBase

createLocalSynchronizer

The createLocalSynchronizer function creates a LocalSynchronizer object that can synchronize MergeableStore data to and from other MergeableStore instances on the same local machine.

createLocalSynchronizer(
  store: MergeableStore,
  onSend?: Send,
  onReceive?: Receive,
  onIgnoredError?: (error: any) => void,
): LocalSynchronizer
TypeDescription
storeMergeableStore

The MergeableStore to synchronize.

onSend?Send

An optional handler for the messages that this Synchronizer sends. This is suitable for debugging synchronization issues in a development environment, since v5.1.

onReceive?Receive

An optional handler for the messages that this Synchronizer receives. This is suitable for debugging synchronization issues in a development environment, since v5.1.

onIgnoredError?(error: any) => void

An optional handler for the errors that the Synchronizer would otherwise ignore when trying to synchronize data. This is suitable for debugging synchronization issues in a development environment.

returnsLocalSynchronizer

A reference to the new LocalSynchronizer object.

This is something of a showcase Synchronizer, rather than something you would use in a production environment. If you do need to synchronize two in-memory MergeableStore instances, you may prefer to use the merge function on either one of them instead of going to the effort of setting up this Synchronizer.

As well as providing a reference to the MergeableStore to persist, a final set of optional handlers can be provided to help debug sends, receives, and errors respectively.

Example

This example creates two LocalSynchronizer objects to synchronize one MergeableStore to another.

import {createLocalSynchronizer} from 'tinybase/synchronizers/synchronizer-local';
import {createMergeableStore} from 'tinybase';

const store1 = createMergeableStore();
const store2 = createMergeableStore();

const synchronizer1 = createLocalSynchronizer(store1);
const synchronizer2 = createLocalSynchronizer(store2);

await synchronizer1.startSync();
await synchronizer2.startSync();

store1.setTables({pets: {fido: {species: 'dog'}}});
store2.setTables({pets: {felix: {species: 'cat'}}});

// ...
console.log(store1.getTables());
// -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
console.log(store2.getTables());
// -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}

synchronizer1.destroy();
synchronizer2.destroy();

Since

v5.0.0