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

Property type: T

reading the property gives the last event data posted. Setting the property (evt.state = data) is equivalent to calling .post(data).

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

Property 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

Property type: NonPostableEvt<{prevState:T; newState: T}>

Posted every time the Evt is posted. Used to compare the previous state with the new state.

.evtChangeDiff

Property type: NonPostableEvt<{prevState:T; newState: T}>

Same than .evtDiff but post only when .evtChang post.

.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

Use the method method .toStateful(initialState) of Evt. Example:

Merging multiple StatefulEvts

****Run the example****

Make a StatefulEvt readonly

To prevent a StatefulEvt to be posted by parts of the code that is not supposed to StatefulEvt can be exposed as StatefulReadonlyEvt.

.postFoceChange()

.toStateless([ctx])

Return a stateless copy of the Evt.

evt.toStateless() is equivalent to Evt.prototype.pipe.call(evt)

Last updated