TinyBase logoTinyBase

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
TypeDescription
storeMergeableStore

The MergeableStore to synchronize.

sendSend

A Send function for sending a message.

registerReceive(receive: Receive) => void

A callback (called once when the Synchronizer is created) that is passed a Receive function that you need to ensure will receive messages addressed or broadcast to this client.

destroy() => void

A function called when destroying the Synchronizer which can be used to clean up underlying resources.

requestTimeoutSecondsnumber

An number of seconds before a request sent from this Synchronizer to another peer times out.

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.

returnsSynchronizer

A reference to the new Synchronizer object.

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