TinyBase logoTinyBase

addHasValueListener

The addHasValueListener method registers a listener function with the Store that will be called when a Value is added to or removed from the Store.

addHasValueListener(
  valueId: IdOrNull,
  listener: HasValueListener,
  mutator?: boolean,
): string
TypeDescription
valueIdIdOrNull

The Id of the Value to listen to, or null as a wildcard.

listenerHasValueListener

The function that will be called whenever the matching Value is added or removed.

mutator?boolean

An optional boolean that indicates that the listener mutates Store data.

returnsstring

A unique Id for the listener that can later be used to call it explicitly, or to remove it.

The provided listener is a HasValueListener function, and will be called with a reference to the Store and the Id of Value that changed. It is also given a flag to indicate whether the Value now exists (having not done previously), or does not (having done so previously).

You can either listen to a single Value being added or removed (by specifying the Value Id) or any Value being added or removed (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 a specific Value being added or removed.

const store = createStore().setValues({open: true, employees: 3});
const listenerId = store.addHasValueListener(
  'employees',
  (store, valueId, hasValue) => {
    console.log('employee value ' + (hasValue ? 'added' : 'removed'));
  },
);

store.delValue('employees');
// -> 'employee value removed'

store.setValue('employees', 4);
// -> 'employee value added'

store.delListener(listenerId);

This example registers a listener that responds to any Value being added or removed.

const store = createStore().setValues({open: true, employees: 3});
const listenerId = store.addHasValueListener(
  null,
  (store, valueId, hasValue) => {
    console.log(valueId + ' value ' + (hasValue ? 'added' : 'removed'));
  },
);

store.delValue('employees');
// -> 'employees value removed'
store.setValue('website', 'https://pets.com');
// -> 'website value added'

store.delListener(listenerId);

This example registers a listener that responds to a specific Value being added or removed, and which also mutates the Store itself.

const store = createStore().setValues({open: true, employees: 3});
const listenerId = store.addHasValueListener(
  'employees',
  (store, valueId) => store.setValue('updated', true),
  true,
);

store.delValue('employees');
console.log(store.getValues());
// -> {open: true, updated: true}

store.delListener(listenerId);

Since

v4.4.0