addResultRowCountListener
The addResultRowCountListener
method registers a listener function with the Queries
object that will be called whenever the count of ResultRow
objects in a ResultTable
changes.
addResultRowCountListener(
queryId: IdOrNull,
listener: ResultRowCountListener,
): string
Type | Description | |
---|---|---|
queryId | IdOrNull | The |
listener | ResultRowCountListener | The function that will be called whenever the number of |
returns | string | A unique |
The provided listener is a ResultRowCountListener
function, and will be called with a reference to the Queries
object, the Id
of the ResultTable
that changed (which is also the query Id
), and the number of ResultRow
objects in th ResultTable
.
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 a change in the number of ResultRow
objects in 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(
'dogColors',
'pets',
({select, where}) => {
select('color');
where('species', 'dog');
},
);
const listenerId = queries.addResultRowCountListener(
'dogColors',
(queries, tableId, count) => {
console.log(
'Row count for dogColors result table changed to ' + count,
);
},
);
store.setRow('pets', 'rex', {species: 'dog', color: 'tan'});
// -> 'Row count for dogColors result table changed to 3'
store.delListener(listenerId);
This example registers a listener that responds to a change in the number of ResultRow
objects any 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('dogColors', 'pets', ({select, where}) => {
select('color');
where('species', 'dog');
})
.setQueryDefinition('catColors', 'pets', ({select, where}) => {
select('color');
where('species', 'cat');
});
const listenerId = queries.addResultRowCountListener(
null,
(queries, tableId, count) => {
console.log(
`Row count for ${tableId} result table changed to ${count}`,
);
},
);
store.setRow('pets', 'rex', {species: 'dog', color: 'tan'});
// -> 'Row count for dogColors result table changed to 3'
store.setRow('pets', 'tom', {species: 'cat', color: 'gray'});
// -> 'Row count for catColors result table changed to 2'
store.delListener(listenerId);
Since
v4.1.0