addParamValueListener
The addParamValueListener method registers a listener function with the Queries object that will be called whenever a specific param value of a query changes.
addParamValueListener(
queryId: IdOrNull,
paramId: IdOrNull,
listener: ParamValueListener,
): string| Type | Description | |
|---|---|---|
queryId | IdOrNull | The |
paramId | IdOrNull | The |
listener | ParamValueListener | The function that will be called whenever the specific param value of the query changes. |
| returns | string |
You can either listen to a single query (by specifying a query Id as the method's first parameter) or changes to any query (by providing a null wildcard). Additionally, you can either listen to a specific param (by specifying a param Id as the second parameter) or changes to any param (by providing a null wildcard).
The provided listener is a ParamValueListener function, and will be called with a reference to the Queries object, the Id of the query whose param value changed, the Id of the param whose value changed, and the new value of the param.
Example
This example registers two listeners that respond to changes to a specific param value of a specific query, or any param value of any query respectively.
import {createQueries, createStore} from 'tinybase';
const store = createStore().setTable('pets', {
// store data
});
const queries = createQueries(store).setQueryDefinition(
'petsByColor',
'pets',
() => {
// query definition
},
{color: 'white'},
);
const listenerId1 = queries.addParamValueListener(
'petsByColor',
'color',
(queries, queryId, paramId, value) => {
console.log(`Specific param for specific query changed: "${value}"`);
},
);
const listenerId2 = queries.addParamValueListener(
null,
null,
(queries, queryId, paramId, value) => {
console.log(`Param "${paramId}" for "${queryId}" changed: "${value}"`);
},
);
queries.setParamValue('petsByColor', 'color', 'black');
// -> 'Specific param for specific query changed: "black"'
// -> 'Param "color" for "petsByColor" changed: "black"'
queries.setParamValue('petsByColor', 'color', 'brown');
// -> 'Specific param for specific query changed: "brown"'
// -> 'Param "color" for "petsByColor" changed: "brown"'
queries.delListener(listenerId1);
queries.delListener(listenerId2);
Since
v7.2.0