TinyBase

callListener

The callListener method provides a way for you to manually provoke a listener to be called, even if the underlying data hasn't changed.

callListener(listenerId: string): Store
TypeDescription
listenerIdstring

The Id of the listener to call.

returnsStore

A reference to the Store.

This is useful when you are using mutator listeners to guarantee that data conforms to programmatic conditions, and those conditions change such that you need to update the Store in bulk.

Example

This example registers a listener that ensures a Cell has one of list of a valid values. After that list changes, the listener is called to apply the condition to the existing data.

const validColors = ['walnut', 'brown', 'black'];
const store = createStore();
const listenerId = store.addCellListener(
  'pets',
  null,
  'color',
  (store, tableId, rowId, cellId, color) => {
    if (!validColors.includes(color)) {
      store.setCell(tableId, rowId, cellId, validColors[0]);
    }
  },
  true,
);

store.setRow('pets', 'fido', {species: 'dog', color: 'honey'});
console.log(store.getRow('pets', 'fido'));
// -> {species: 'dog', color: 'walnut'}

validColors.shift();
console.log(validColors);
// -> ['brown', 'black']

store.callListener(listenerId);
console.log(store.getRow('pets', 'fido'));
// -> {species: 'dog', color: 'brown'}

store.delListener(listenerId);