TinyBase logoTinyBase β

addStatusListener

The addStatusListener method registers a listener function with the Persister that will be called whenever it starts or stops loading or saving.

addStatusListener(listener: StatusListener<StoreOrMergeableStore>): string
TypeDescription
listenerStatusListener<StoreOrMergeableStore>

The function that will be called whenever the Persister starts or stops loading or saving.

returnsstring

A unique Id for the listener that can later be used to remove it.

The provided listener is a StatusListener function, and will be called with a reference to the Persister and the new Status: 0 means now idle, 1 means now loading, and 2 means now saving.

Example

This example registers a listener that responds to changes in the state of the Persister.

import {createSessionPersister} from 'tinybase/persisters/persister-browser';
import {createStore} from 'tinybase';

const persister = createSessionPersister(createStore(), 'pets');

const listenerId = persister.addStatusListener((persister, status) => {
  console.log(
    `${persister.getStorageName()} persister status changed to ${status}`,
  );
});

await persister.load();
// -> 'pets persister status changed to 1'
// -> 'pets persister status changed to 0'
await persister.save();
// -> 'pets persister status changed to 2'
// -> 'pets persister status changed to 0'

persister.delListener(listenerId);

Since

v5.3.0