TinyBase logoTinyBase

addIndexIdsListener

The addIndexIdsListener method registers a listener function with the Indexes object that will be called whenever an Index definition is added or removed.

addIndexIdsListener(listener: IndexIdsListener): string
TypeDescription
listenerIndexIdsListener

The function that will be called whenever an Index definition is added or removed.

returnsstring

The provided listener is an IndexIdsListener function, and will be called with a reference to the Indexes object.

Example

This example creates a Store, an Indexes object, and then registers a listener that responds to the addition and the removal of an Index definition.

const store = createStore().setTable('pets', {
  fido: {species: 'dog'},
  felix: {species: 'cat'},
  cujo: {species: 'dog'},
});

const indexes = createIndexes(store);
const listenerId = indexes.addIndexIdsListener((indexes) => {
  console.log(indexes.getIndexIds());
});

indexes.setIndexDefinition('bySpecies', 'pets', 'species');
// -> ['bySpecies']
indexes.delIndexDefinition('bySpecies');
// -> []

indexes.delListener(listenerId);

Since

v4.1.0