Optional
options: { queue events to be dispatched in a microtask.
If the state changes multiple times in the same microtask, only the last change will be dispatched.
If the most recent value is the same as the previous value, no event will be dispatched.
Set this to false
to dispatch events immediately upon state changes.
Optional
lastValue: TStatic
createCreate a listener that listens to multiple states and calls the callback when any of the states change.
For example,
import {State} from "lifecycle-utils";
const valueState1 = new State<number>(6);
const valueState2 = new State<string>("hello");
const valueState3 = new State<boolean>(true);
const eventHandle = State.createCombinedChangeListener([valueState1, valueState2, valueState3], (newValues, previousValues) => {
console.log("new values:", newValues);
console.log("previous values:", previousValues);
});
valueState1.state = 7;
valueState2.state = "world";
valueState3.state = false;
// after a microtask, the listener will be called
// to make event fire immediately upon change, disable the `queueEvents` option on the constructor
await new Promise(resolve => setTimeout(resolve, 0));
// will print:
// new values: [7, "world", false]
// previous values: [6, "hello", true]
eventHandle.dispose();
Optional
options: { Optional
callOptional
queuequeue events to be dispatched in a microtask.
If the state changes multiple times in the same microtask, only the last change will be dispatched.
If the most recent value is the same as the previous value, no event will be dispatched.
Set this to false
to dispatch events immediately upon state changes.
State
is a utility class that allows you to hold a value and notify listeners when the value changes.