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| Type | Description | |
|---|---|---|
store | MergeableStore | The |
onSend? | Send | An optional handler for the messages that this |
onReceive? | Receive | An optional handler for the messages that this |
onIgnoredError? | (error: any) => void | An optional handler for the errors that the |
| returns | LocalSynchronizer | A reference to the new |
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 {createMergeableStore} from 'tinybase';
import {createLocalSynchronizer} from 'tinybase/synchronizers/synchronizer-local';
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'}}}
await synchronizer1.destroy();
await synchronizer2.destroy();
Since
v5.0.0