createCustomSynchronizer
The createCustomSynchronizer
function creates a Synchronizer
object that can persist one MergeableStore
to another.
createCustomSynchronizer(
store: MergeableStore,
send: Send,
registerReceive: (receive: Receive) => void,
destroy: () => void,
requestTimeoutSeconds: number,
onSend?: Send,
onReceive?: Receive,
onIgnoredError?: (error: any) => void,
): Synchronizer
Type | Description | |
---|---|---|
store | MergeableStore | The |
send | Send | A |
registerReceive | (receive: Receive) => void | A callback (called once when the |
destroy | () => void | A function called when destroying the |
requestTimeoutSeconds | number | An number of seconds before a request sent from this |
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 | Synchronizer | A reference to the new |
As well as providing a reference to the MergeableStore
to synchronize, you must provide parameters which identify how to send and receive changes to and from this MergeableStore
and its peers. This is entirely dependent upon the medium of communication used.
You must also provide a callback for when the Synchronizer
is destroyed (which is a good place to clean up resources and stop communication listeners), and indicate how long the Synchronizer
will wait for responses to message requests before timing out.
A final set of optional handlers can be provided to help debug sends, receives, and errors respectively.
Example
This example creates a function for creating custom Synchronizer
objects via a very naive pair of message buses (which are first-in, first-out). Each Synchronizer
can write to the other's bus, and they each poll to read from their own. The example then uses these Synchronizer
instances to sync two MergeableStore
objects together
import {createMergeableStore, getUniqueId} from 'tinybase';
import {createCustomSynchronizer} from 'tinybase/synchronizers';
const bus1 = [];
const bus2 = [];
const createBusSynchronizer = (store, localBus, remoteBus) => {
let timer;
const clientId = getUniqueId();
return createCustomSynchronizer(
store,
(toClientId, requestId, message, body) => {
// send
remoteBus.push([clientId, requestId, message, body]);
},
(receive) => {
// registerReceive
timer = setInterval(() => {
if (localBus.length > 0) {
receive(...localBus.shift());
}
}, 1);
},
() => clearInterval(timer), // destroy
1,
);
};
const store1 = createMergeableStore();
const store2 = createMergeableStore();
const synchronizer1 = createBusSynchronizer(store1, bus1, bus2);
const synchronizer2 = createBusSynchronizer(store2, bus2, bus1);
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