StatefulEvt<T>
A StatefulEvt is an Evt stat keep a reference to the last value posted.
You can think of it as way to observe when a value is changed.
.state
.stateProperty type: T
reading the property gives the last event data posted. Setting the property (evt.state = data) is equivalent to calling .post(data).
In v2 evt.state = data will only trigger the call of .post(data) if data !== evt.state
Consult v2 roadmap here
import { Evt } from "evt";
const evtCount = Evt.create(0); // Equivalent wit new StatefulEvt<number>(0)
evtCount.attach(console.log);
console.log(evtCount.state); //Pints "state: 0"
evtIsConnected.post(1); //Pints "1"
console.log(evtCount.state); //Prints "1";
evtCount.state++; //Prints "2"
console.log(evtCount.state); //Pints "2";.evtChange
.evtChangeProperty type: ReadonlyStatefulEvt<T>
The .evtChange property is an Evt that post only when the .state has changed. ( or when post is made via .postForceChange() )
.evtDiff
.evtDiffProperty type: NonPostableEvt<{prevState:T; newState: T}>
Posted every time the Evt is posted. Used to compare the previous state with the new state.
.evtChangeDiff
.evtChangeDiffProperty type: NonPostableEvt<{prevState:T; newState: T}>
Same than .evtDiff but post only when .evtChang post.
.pipe(...)
.pipe(...)Same as evt.pipe(...) but return a StatefulEvt. Be aware that the current state of the StatefulEvt must be matched by the operator ( if any ) when invoking .pipe(), elst an exception will be thrown.
Converting an Evt into a StatefulEvt
Evt into a StatefulEvtUse the method method .toStateful(initialState) of Evt. Example:
You do not need to pass an initialization value to .toStateful(), if you don't the state will be initialized with undefined and the returned StatefulEvt will be of type<T | undefined>. This is usefull when using .toStateful after Evt.merge(). See next example.
Merging multiple StatefulEvts
StatefulEvts****Run the example****
Make a StatefulEvt readonly
StatefulEvt readonlyTo prevent a StatefulEvt to be posted by parts of the code that is not supposed to StatefulEvt can be exposed as StatefulReadonlyEvt.
.postFoceChange()
.postFoceChange().toStateless([ctx])
.toStateless([ctx])Return a stateless copy of the Evt.
evt.toStateless() is equivalent to Evt.prototype.pipe.call(evt)
Last updated