TinyBase logoTinyBase

addResultCellIdsListener

The addResultCellIdsListener method registers a listener function with the Queries object that will be called whenever the ResultCell Ids in a ResultRow change.

addResultCellIdsListener(
  queryId: IdOrNull,
  rowId: IdOrNull,
  listener: ResultCellIdsListener,
): Id
TypeDescription
queryIdIdOrNull

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

rowIdIdOrNull

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

listenerResultCellIdsListener

The function that will be called whenever the ResultCell Ids in the ResultRow change.

returnsId

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

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

Such a listener is only called when a ResultCell is added to, or removed from, the ResultRow. To listen to all changes in the ResultRow, use the addResultRowListener method.

You can either listen to a single ResultRow (by specifying the query Id and ResultRow Id as the method's first two parameters) or changes to any ResultRow (by providing null wildcards).

Both, either, or neither of the queryId and rowId parameters can be wildcarded with null. You can listen to a specific ResultRow in a specific query, any ResultRow in a specific query, a specific ResultRow in any query, or any ResultRow in any query.

Examples

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

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(
  'dogColors',
  'pets',
  ({select, where}) => {
    select('color');
    select('price');
    where('species', 'dog');
  },
);

const listenerId = queries.addResultCellIdsListener(
  'dogColors',
  'fido',
  (store, tableId, rowId) => {
    console.log(`Cell Ids for fido row in dogColors result table changed`);
    console.log(queries.getResultCellIds('dogColors', 'fido'));
  },
);

store.setCell('pets', 'fido', 'price', 5);
// -> 'Cell Ids for fido row in dogColors result table changed'
// -> ['color', 'price']

store.delListener(listenerId);

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

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('dogColors', 'pets', ({select, where}) => {
    select('color');
    select('price');
    where('species', 'dog');
  })
  .setQueryDefinition('catColors', 'pets', ({select, where}) => {
    select('color');
    select('purrs');
    where('species', 'cat');
  });

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

store.setCell('pets', 'fido', 'price', 5);
// -> 'Cell Ids for fido row in dogColors result table changed'
store.setCell('pets', 'felix', 'purrs', true);
// -> 'Cell Ids for felix row in catColors result table changed'

store.delListener(listenerId);

Since

v2.0.0