addRowIdsListener
The addRowIdsListener
method registers a listener function with the Store
that will be called whenever the Row
Ids
in a Table
change.
addRowIdsListener(
tableId: IdOrNull,
listener: RowIdsListener<MergeableStore>,
mutator?: boolean,
): string
Type | Description | |
---|---|---|
tableId | IdOrNull | |
listener | RowIdsListener<MergeableStore> | The function that will be called whenever the |
mutator? | boolean | An optional boolean that indicates that the listener mutates |
returns | string | A unique |
The provided listener is a RowIdsListener
function, and will be called with a reference to the Store
and the Id
of the Table
that changed.
By default, such a listener is only called when a Row
is added or removed. To listen to all changes in the Table
, use the addTableListener
method.
You can either listen to a single Table
(by specifying its Id
as the method's first parameter) or changes to any Table
(by providing a null
wildcard).
Use the optional mutator parameter to indicate that there is code in the listener that will mutate Store
data. If set to false
(or omitted), such mutations will be silently ignored. All relevant mutator listeners (with this flag set to true
) are called before any non-mutator listeners (since the latter may become relevant due to changes made in the former). The changes made by mutator listeners do not fire other mutating listeners, though they will fire non-mutator listeners.
Examples
This example registers a listener that responds to any change to the Row
Ids
of a specific Table
.
import {createStore} from 'tinybase';
const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
const listenerId = store.addRowIdsListener('pets', (store) => {
console.log('Row Ids for pets table changed');
console.log(store.getRowIds('pets'));
});
store.setRow('pets', 'felix', {species: 'cat'});
// -> 'Row Ids for pets table changed'
// -> ['fido', 'felix']
store.delListener(listenerId);
This example registers a listener that responds to any change to the Row
Ids
of any Table
.
import {createStore} from 'tinybase';
const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
const listenerId = store.addRowIdsListener(null, (store, tableId) => {
console.log(`Row Ids for ${tableId} table changed`);
console.log(store.getRowIds(tableId));
});
store.setRow('pets', 'felix', {species: 'cat'});
// -> 'Row Ids for pets table changed'
// -> ['fido', 'felix']
store.setRow('species', 'dog', {price: 5});
// -> 'Row Ids for species table changed'
// -> ['dog']
store.delListener(listenerId);
This example registers a listener that responds to any change to the Row
Ids
of a specific Table
, and which also mutates the Store
itself.
import {createStore} from 'tinybase';
const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
const listenerId = store.addRowIdsListener(
'pets',
(store, tableId) => store.setCell('meta', 'update', tableId, true),
true, // mutator
);
store.setRow('pets', 'felix', {species: 'cat'});
console.log(store.getTable('meta'));
// -> {update: {pets: true}}
store.delListener(listenerId);
Since
v1.0.0