[RxJS] Displaying Initial Data with Startwith

Source: Internet
Author: User

You often need-to-render out data before your stream begins from a click or another user interaction. This lessons shows how to use to startWith set the initial output before you trigger your stream.

Const OBSERVABLE =Rx.observable;const Startbutton= Document.queryselector (' #start '); Const Stopbutton= Document.queryselector (' #stop '); Const start$= Observable.fromevent (Startbutton, ' click ')); Const interval$= Observable.interval (1000); Const stop$= Observable.fromevent (Stopbutton, ' click ')); Const intervalthatstops$=interval$. Takeuntil (stop$); const data= {count:0};start$. Switchmapto (intervalthatstops$). Startwith (data). Scan ((ACC)= {        returnObject.assign (ACC, {count:acc.count + 1}). Subscribe ((x)= Console.log (x));

What Startwith would do be, before you click the Start button, it'll set the initial value for scan (), and logout 0 on th E screen.

Then if you click the Start button, it'll increase from 1 to ....

So it means Startwith actually would fire Subscrie once.

[RxJS] Displaying Initial Data with Startwith

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.