addValueIdsListener
The addValueIdsListener
method registers a listener function with the Store
that will be called whenever the Value
Ids
in a Store
change.
addValueIdsListener(
listener: ValueIdsListener,
mutator?: boolean,
): string
Type | Description | |
---|---|---|
listener | ValueIdsListener | The function that will be called whenever the |
mutator? | boolean | An optional boolean that indicates that the listener mutates |
returns | string | A unique Id for the listener that can later be used to call it explicitly, or to remove it. |
The provided listener is a ValueIdsListener
function, and will be called with a reference to the Store
.
By default, such a listener is only called when a Value
is added or removed. To listen to all changes in the Values
, use the addValuesListener
method.
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 Value
Ids
.
const store = createStore().setValues({open: true});
const listenerId = store.addValueIdsListener((store) => {
console.log('Value Ids changed');
console.log(store.getValueIds());
});
store.setValue('employees', 3);
// -> 'Value Ids changed'
// -> ['open', 'employees']
store.delListener(listenerId);
This example registers a listener that responds to any change to the Value
Ids
, and which also mutates the Store
itself.
const store = createStore().setValues({open: true});
const listenerId = store.addValueIdsListener(
(store) => store.setValue('updated', true),
true, // mutator
);
store.setValue('employees', 3);
console.log(store.getValues());
// -> {open: true, employees: 3, updated: true}
store.delListener(listenerId);
Since
v3.0.0