Streams often need to work together to produce the values of you ' ll need. This lesson shows how to use a input stream and an interval stream together and push a object with both values through T He stream.
ConstObservable =rx.observable;ConstStartbutton = Document.queryselector (' #start ');ConstHalfbutton = Document.queryselector (' #half ');ConstQuarterbutton = Document.queryselector (' #quarter ');Constinput = Document.queryselector ("#input");ConstStopbutton = Document.queryselector (' #stop ');ConstResetbutton = Document.queryselector (' #reset ');Constdata = {count:0};Constinc = (ACC) = ({Count:acc.count + 1});Constreset = (ACC) = =data;Conststart$ = Observable.fromevent (Startbutton, ' click ');Consthalf$ = Observable.fromevent (Halfbutton, ' click ');Constquarter$ = Observable.fromevent (Quarterbutton, ' click ');Conststop$ = Observable.fromevent (Stopbutton, ' click ');Constreset$ = Observable.fromevent (Resetbutton, ' click ');Conststarters$ =Observable.merge (Start$.mapto (1000), Half$.mapto (500), Quarter$.mapto (250) );ConstIntervalactions = (time) = = { returnObservable.merge (Observable.interval (Time) Takeuntil (stop$). Mapto (inc), Reset$.mapto (R ESET))};Consttimer$ =starters$. Switchmap (intervalactions). Startwith (data). Scan (ACC, Curr)=Curr (ACC));Constinput$ = observable.fromevent (input, "input"). Map (EV=ev.target.value); Observable. combinelatest (timer$, input$)//We'll get an array combinelatest. Map ((array) ={ return{Count:array[0].count, input:array[1]}). Subscribe (x= Console.log (x))
They last param of combinelatest was a result function, which can parse the result in the fucntion:
observable.combinelatest ( timer$, input$, (timer, input)={ return {count:timer.count, input} = Console.log (x))
[RxJS] Combining Streams with Combinelatest