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,
): string
Type | Description | |
---|---|---|
queryId | IdOrNull | The |
rowId | IdOrNull | The |
listener | ResultCellIdsListener | The function that will be called whenever the |
returns | string | A unique |
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
.
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(
'dogColors',
'pets',
({select, where}) => {
select('color');
select('price');
where('species', 'dog');
},
);
const listenerId = queries.addResultCellIdsListener(
'dogColors',
'fido',
(queries) => {
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
.
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('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