[RxJS] Combining Streams with Combinelatest

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.