TinyBase

addTableIdsListener

The addTableIdsListener method registers a listener function with the Store that will be called whenever the Table Ids in the Store change.

addTableIdsListener(
  listener: TableIdsListener,
  mutator?: boolean,
): string
TypeDescription
listenerTableIdsListener

The function that will be called whenever the Table Ids in the Store change.

mutator?boolean

An optional boolean that indicates that the listener mutates Store data.

returnsstring

A unique Id for the listener that can later be used to call it explicitly, or to remove it.

Such a listener is only called when a Table is added or removed. To listen to all changes in the Store, use the addTablesListener method.

The provided listener is a TableIdsListener function, and will be called with a reference to the Store.

Use the optional mutator parameter to indicate that there is code in the listener that will mutate Store data. If set to false (or omitted), such mutations will be silently ignored. All relevant mutator listeners (with this flag set to true) are called before any non-mutator listeners (since the latter may become relevant due to changes made in the former). The changes made by mutator listeners do not fire other mutating listeners, though they will fire non-mutator listeners.

Examples

This example registers a listener that responds to any change to the Table Ids.

const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
const listenerId = store.addTableIdsListener((store) => {
  console.log('Table Ids changed');
  console.log(store.getTableIds());
});

store.setTable('species', {dog: {price: 5}});
// -> 'Table Ids changed'
// -> ['pets', 'species']

store.delListener(listenerId);

This example registers a listener that responds to any change to the Table Ids, and which also mutates the Store itself.

const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
const listenerId = store.addTableIdsListener(
  (store) => store.setCell('meta', 'update', 'store', true),
  true,
);

store.setTable('species', {dog: {price: 5}});
console.log(store.getTable('meta'));
// -> {update: {store: true}}

store.delListener(listenerId);