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.
Use the method method .toStateful(initialState) of Evt. Example:
import { Evt } from"evt";constevtClickCount=Evt.from(document,"click").pipe([(...[,count])=>[count+1],0]).toStateful(0);//...user click 3 times on the pageconsole.log(evtClickCount.state); //Prints "3"
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
import { Evt } from"evt";constevtIsBlue=Evt.create(false);constevtIsBig=Evt.create(false);constevtIsBigAndBlue=Evt.merge([evtIsBlue.evtChange,evtIsBig.evtChange]).toStateful().pipe(()=> [ evtIsBlue.state &&evtIsBig.state ]) ;console.log(evtIsBigAndBlue.state); // Prints "false"evtIsBlue.state=true;console.log(evtIsBigAndBlue.state); // Prints "false"evtIsBig.state=true;console.log(evtIsBigAndBlue.state); // Prints "true"
To prevent a StatefulEvt to be posted by parts of the code that is not supposed to StatefulEvt can be exposed as StatefulReadonlyEvt.
import { StatefulEvt, StatefulReadonlyEvt } from"evt";//Return an event that post every second.functiongenerateEvtTick(delay:number):StatefulReadonlyEvt<number> {constevtTick=newStatefulEvt(0);setInterval(()=>evtTick.state++, delay); retrun evtTick;}constevtTick=generateTick(1000);evtTick.state++; // TS ERRORevtTick.post(2); // TS ERROR
.postFoceChange()
/** * Post and enforce that .evtChange and .evtChangeDiff * be posted even if the state has not changed. * * If no argument is passed the post is performed with the current state. * * Returns post count **/postForceChange(wData?: readonly [T]): number;
.toStateless([ctx])
Return a stateless copy of the Evt.
import { Evt } from"evt";constevtText=Evt.create("foo");//x is Evt<string>constx=evtText.toStateless();
evt.toStateless() is equivalent to Evt.prototype.pipe.call(evt)