TinyBase logoTinyBase

addResultTableCellIdsListener

The addResultTableCellIdsListener method registers a listener function with the Queries object that will be called whenever the Cell Ids that appear anywhere in a ResultTable change.

addResultTableCellIdsListener(
  queryId: IdOrNull,
  listener: ResultTableCellIdsListener,
): string
TypeDescription
queryIdIdOrNull

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

listenerResultTableCellIdsListener

The function that will be called whenever the Cell Ids that appear anywhere in the ResultTable change.

returnsstring

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

The provided listener is a ResultTableCellIdsListener function, and will be called with a reference to the Queries object and the Id of the ResultTable that changed (which is also the query Id).

By default, such a listener is only called when a Cell Id is added to, or removed from, the ResultTable. To listen to all changes in the ResultTable, use the addResultTableListener method.

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

Examples

This example registers a listener that responds to any change to the Cell Ids of a specific ResultTable.

import {createQueries, createStore} from 'tinybase';

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

const queries = createQueries(store).setQueryDefinition(
  'dogColorsAndLegs',
  'pets',
  ({select, where}) => {
    select('color');
    select('legs');
    where('species', 'dog');
  },
);

const listenerId = queries.addResultTableCellIdsListener(
  'dogColorsAndLegs',
  (queries) => {
    console.log(`Cell Ids for dogColorsAndLegs result table changed`);
    console.log(queries.getResultTableCellIds('dogColorsAndLegs'));
  },
);

store.setCell('pets', 'cujo', 'legs', 4);
// -> 'Cell Ids for dogColorsAndLegs result table changed'
// -> ['color', 'legs']

store.delListener(listenerId);

This example registers a listener that responds to any change to the ResultCell Ids of any ResultTable.

import {createQueries, createStore} from 'tinybase';

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

const queries = createQueries(store)
  .setQueryDefinition('dogColorsAndLegs', 'pets', ({select, where}) => {
    select('color');
    select('legs');
    where('species', 'dog');
  })
  .setQueryDefinition('catColorsAndLegs', 'pets', ({select, where}) => {
    select('color');
    select('legs');
    where('species', 'cat');
  });

const listenerId = queries.addResultTableCellIdsListener(
  null,
  (queries, tableId) => {
    console.log(`Cell Ids for ${tableId} result table changed`);
  },
);

store.setCell('pets', 'cujo', 'legs', 4);
// -> 'Cell Ids for dogColorsAndLegs result table changed'
store.delCell('pets', 'felix', 'legs');
// -> 'Cell Ids for catColorsAndLegs result table changed'

store.delListener(listenerId);

Since

v2.0.0