addSliceRowIdsListener
The addSliceRowIdsListener
method registers a listener function with the Indexes
object that will be called whenever the Row
Ids
in a Slice
change.
addSliceRowIdsListener(
indexId: IdOrNull,
sliceId: IdOrNull,
listener: SliceRowIdsListener,
): string
Type | Description | |
---|---|---|
indexId | IdOrNull | |
sliceId | IdOrNull | |
listener | SliceRowIdsListener | The function that will be called whenever the |
returns | string | A unique |
You can either listen to a single Slice
(by specifying the Index
Id
and Slice
Id
as the method's first two parameters), or changes to any Slice
(by providing null
wildcards).
Both, either, or neither of the indexId
and sliceId
parameters can be wildcarded with null
. You can listen to a specific Slice
in a specific Index
, any Slice
in a specific Index
, a specific Slice
in any Index
, or any Slice
in any Index
.
The provided listener is a SliceRowIdsListener
function, and will be called with a reference to the Indexes
object, the Id
of the Index
, and the Id
of the Slice
that changed.
Examples
This example creates a Store
, an Indexes
object, and then registers a listener that responds to any changes to a specific Slice
.
import {createIndexes, createStore} from 'tinybase';
const store = createStore().setTable('pets', {
fido: {species: 'dog'},
felix: {species: 'cat'},
cujo: {species: 'dog'},
});
const indexes = createIndexes(store);
indexes.setIndexDefinition('bySpecies', 'pets', 'species');
const listenerId = indexes.addSliceRowIdsListener(
'bySpecies',
'dog',
(indexes) => {
console.log('Row Ids for dog slice in bySpecies index changed');
console.log(indexes.getSliceRowIds('bySpecies', 'dog'));
},
);
store.setRow('pets', 'toto', {species: 'dog'});
// -> 'Row Ids for dog slice in bySpecies index changed'
// -> ['fido', 'cujo', 'toto']
indexes.delListener(listenerId);
This example creates a Store
, an Indexes
object, and then registers a listener that responds to any changes to any Slice
.
import {createIndexes, createStore} from 'tinybase';
const store = createStore().setTable('pets', {
fido: {species: 'dog', color: 'brown'},
felix: {species: 'cat', color: 'black'},
cujo: {species: 'dog', color: 'black'},
});
const indexes = createIndexes(store)
.setIndexDefinition('bySpecies', 'pets', 'species')
.setIndexDefinition('byColor', 'pets', 'color');
const listenerId = indexes.addSliceRowIdsListener(
null,
null,
(indexes, indexId, sliceId) => {
console.log(
`Row Ids for ${sliceId} slice in ${indexId} index changed`,
);
console.log(indexes.getSliceRowIds(indexId, sliceId));
},
);
store.setRow('pets', 'toto', {species: 'dog', color: 'brown'});
// -> 'Row Ids for dog slice in bySpecies index changed'
// -> ['fido', 'cujo', 'toto']
// -> 'Row Ids for brown slice in byColor index changed'
// -> ['fido', 'toto']
indexes.delListener(listenerId);
Since
v1.0.0