callListener
The callListener
method provides a way for you to manually provoke a listener to be called, even if the underlying data hasn't changed.
callListener(listenerId: string): this
This is useful when you are using mutator listeners to guarantee that data conforms to programmatic conditions, and those conditions change such that you need to update the Store
in bulk.
Examples
This example registers a listener that ensures a Cell
has one of list of a valid values. After that list changes, the listener is called to apply the condition to the existing data.
import {createStore} from 'tinybase';
const validColors = ['walnut', 'brown', 'black'];
const store = createStore();
const listenerId = store.addCellListener(
'pets',
null,
'color',
(store, tableId, rowId, cellId, color) => {
if (!validColors.includes(color)) {
store.setCell(tableId, rowId, cellId, validColors[0]);
}
},
true,
);
store.setRow('pets', 'fido', {species: 'dog', color: 'honey'});
console.log(store.getRow('pets', 'fido'));
// -> {species: 'dog', color: 'walnut'}
validColors.shift();
console.log(validColors);
// -> ['brown', 'black']
store.callListener(listenerId);
console.log(store.getRow('pets', 'fido'));
// -> {species: 'dog', color: 'brown'}
store.delListener(listenerId);
This example registers a listener to Row
Id
changes. It is explicitly called and fires for two Tables
in the Store
.
import {createStore} from 'tinybase';
const store = createStore().setTables({
pets: {fido: {species: 'dog'}},
species: {dog: {price: 5}},
});
const listenerId = store.addRowIdsListener(null, (store, tableId) => {
console.log(`Row Ids listener called for ${tableId} table`);
});
store.callListener(listenerId);
// -> 'Row Ids listener called for pets table'
// -> 'Row Ids listener called for species table'
store.delListener(listenerId);
This example registers a listener Value
changes. It is explicitly called and fires for two Values
in the Store
.
import {createStore} from 'tinybase';
const store = createStore().setValues({open: true, employees: 3});
const listenerId = store.addValueListener(
null,
(store, valueId, value) => {
console.log(`Value listener called for ${valueId} value, ${value}`);
},
);
store.callListener(listenerId);
// -> 'Value listener called for open value, true'
// -> 'Value listener called for employees value, 3'
store.delListener(listenerId);
This example registers listeners for the end of transactions, and for invalid Cells. They are explicitly called, meaninglessly. The former receives empty arguments. The latter is not called at all.
import {createStore} from 'tinybase';
const store = createStore();
const listenerId = store.addWillFinishTransactionListener(
(store, cellsTouched, valuesTouched) => {
console.log(`Transaction finish: ${cellsTouched}/${valuesTouched}`);
},
);
store.callListener(listenerId);
// -> 'Transaction finish: undefined/undefined'
store.delListener(listenerId);
const listenerId2 = store.addInvalidCellListener(
null,
null,
null,
(store, tableId, rowId, cellId) => {
console.log('Invalid cell', tableId, rowId, cellId);
},
);
store.callListener(listenerId2);
// -> undefined
store.delListener(listenerId2);
Since
v1.0.0