TinyBase logoTinyBase

addHasTableListener

The addHasTableListener method registers a listener function with the Store that will be called when a Table is added to or removed from the Store.

addHasTableListener(
  tableId: IdOrNull,
  listener: HasTableListener<Store>,
  mutator?: boolean,
): string
TypeDescription
tableIdIdOrNull

The Id of the Table to listen to, or null as a wildcard.

listenerHasTableListener<Store>

The function that will be called whenever the matching Table is added or removed.

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.

The provided listener is a HasTableListener function, and will be called with a reference to the Store and the Id of the Table that changed. It is also given a flag to indicate whether the Table now exists (having not done previously), or does not (having done so previously).

You can either listen to a single Table being added or removed (by specifying the Table Id as the method's first parameter) or changes to any Table (by providing a null wildcard).

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 a specific Table being added or removed.

import {createStore} from 'tinybase';

const store = createStore().setTables({
  pets: {fido: {species: 'dog', color: 'brown'}},
});
const listenerId = store.addHasTableListener(
  'pets',
  (store, tableId, hasTable) => {
    console.log('pets table ' + (hasTable ? 'added' : 'removed'));
  },
);

store.delTable('pets');
// -> 'pets table removed'

store.setTable('pets', {fido: {species: 'dog', color: 'brown'}});
// -> 'pets table added'

store.delListener(listenerId);

This example registers a listener that responds to any Table being added or removed.

import {createStore} from 'tinybase';

const store = createStore().setTables({
  pets: {fido: {species: 'dog', color: 'brown'}},
});
const listenerId = store.addHasTableListener(
  null,
  (store, tableId, hasTable) => {
    console.log(`${tableId} table ` + (hasTable ? 'added' : 'removed'));
  },
);

store.delTable('pets');
// -> 'pets table removed'
store.setTable('species', {dog: {price: 5}});
// -> 'species table added'

store.delListener(listenerId);

This example registers a listener that responds to a specific Table being added or removed, and which also mutates the Store itself.

import {createStore} from 'tinybase';

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

store.delTable('pets', 'fido');
console.log(store.getTable('meta'));
// -> {update: {pets: true}}

store.delListener(listenerId);

Since

v4.4.0