SyncManager
import { SyncManager } from '@signaldb/sync'
const syncManager = new SyncManager({
pull: async () => {
// your pull logic
},
push: async () => {
// your push logic
},
})
syncManager.addCollection(someCollection, {
name: 'someCollection',
})
The SyncManager
class in SignalDB is designed to handle the synchronization of collections between your local storage and a remote source. It manages both pushing local changes to the server and pulling updates from the server, ensuring that your data remains consistent and up-to-date.
Constructor
new SyncManager(options)
Creates a new instance of SyncManager
.
Parameters
options
(Options
): Configuration options for the sync manager.pull
: Function to fetch data from the remote source.push
: Function to send changes to the remote source.registerRemoteChange
: Optional function to register a callback for remote changes. The callback can also return a cleanup function to remove the listener.id
: Optional unique identifier for the sync manager.persistenceAdapter
: Optional function to create a persistence adapter. Takes 2 arguments:name
andregisterErrorHandler
. Defaults tocreateLocalStorageAdapter
).reactivity
: Optional reactivity adapter for handling reactivity.onError
: Optional function to handle errors.autostart
: Optional flag to start syncing automatically after adding collections (default:true
).
Methods
addCollection(collection, options)
Adds a collection to the sync manager.
Parameters
collection
(Collection
): The collection to add.options
(SyncOptions
): Configuration options for the collection.
getCollection(name)
(deprecated)
Retrieves a collection and its options by name.
getCollectionProperties(name)
Retrieves the collection options by name.
Parameters
name
(string
): The name of the collection.
Returns
Tuple<Collection, SyncOptions>
: The collection and its options.
syncAll()
Starts the sync process for all collections managed by the sync manager.
isSyncing(name?)
Checks if a specific collection or any collection is currently being synced.
Parameters
name
(string
, optional): The name of the collection. If omitted, checks all collections.
Returns
boolean
:true
if syncing,false
otherwise.
sync(name, options)
Starts the sync process for a specific collection.
Parameters
name
(string
): The name of the collection.options
(object
, optional): Sync options.force
(boolean
, optional): Iftrue
, forces the sync process even if no changes are detected.onlyWithChanges
(boolean
, optional): Iftrue
, syncs only if there are changes.
startSync(name)
Setup a collection to be synced with remote changes and enable automatic pushing changes to the remote source.
Parameters
name
(string
): The name of the collection.
pauseSync(name)
Pauses the sync process for a collection. This means that the collection will not be synced with remote changes and changes will not automatically be pushed to the remote source.
Parameters
name
(string
): The name of the collection.
startAll()
Setup all collections to be synced with remote changes and enable automatic pushing changes to the remote source.
pauseAll()
Pauses the sync process for all collections. This means that the collections will not be synced with remote changes and changes will not automatically be pushed to the remote source.
pushChanges(name)
Initiates the push process for a collection, syncing only if there are changes.
Parameters
name
(string
): The name of the collection.
dispose()
Disposes all internal collections and other data structures.