Synchronizer
A Synchronizer
object lets you synchronize MergeableStore
data with another TinyBase client or system.
This is useful for sharing data between users, or between devices of a single user. This is especially valuable when there is the possibility that there has been a lack of immediate connectivity between clients and the synchronization requires some negotiation to orchestrate merging the MergeableStore
objects together.
Creating a Synchronizer
depends on the choice of underlying medium over which the synchronization will take place. Options include the createWsSynchronizer
function (for a Synchronizer
that will sync over web-sockets), and the createLocalSynchronizer
function (for a Synchronizer
that will sync two MergeableStore
objects in memory on one system). The createCustomSynchronizer
function can also be used to easily create a fully customized way to send and receive the messages of the synchronization protocol.
Note that, as an interface, it is an extension to the Persister
interface, since they share underlying implementations. Think of a Synchronizer
as 'persisting' your MergeableStore
to another client (and vice-versa).
Example
This example creates two empty MergeableStore
objects, creates a LocalSynchronizer
for each, and starts synchronizing them. A change in one becomes evident in the other.
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'}}});
// ...
console.log(store2.getTables());
// -> {pets: {fido: {species: 'dog'}}}
store2.setRow('pets', 'felix', {species: 'cat'});
// ...
console.log(store1.getTables());
// -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
synchronizer1.destroy();
synchronizer2.destroy();
Since
v5.0.0
Getter methods
This is the collection of getter methods within the Synchronizer
interface. There is only one method, getStore
.
Lifecycle methods
This is the collection of lifecycle methods within the Synchronizer
interface. There are only two lifecycle methods, destroy
and schedule
.
Load methods
This is the collection of load methods within the Synchronizer
interface. There are 4 load methods in total.
Save methods
This is the collection of save methods within the Synchronizer
interface. There are 4 save methods in total.
Synchronization methods
This is the collection of synchronization methods within the Synchronizer
interface. There are only three synchronization methods, getSynchronizerStats
, startSync
, and stopSync
.
Development methods
This is the collection of development methods within the Synchronizer
interface. There is only one method, getStats
.