[RxJS] Stopping a Stream with TakeUntil

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[RxJS] Stopping a Stream with TakeUntil相关的知识,希望对你有一定的参考价值。

Observables often need to be stopped before they are completed. This lesson shows how to use takeUntil to stop a running timer. Then we use the starting stream and the stopping stream together to create a simple stopwatch.

 

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$);

start$
    .switchMapTo(intervalThatStops$)
    .subscribe((x)=> console.log(x));

 

以上是关于[RxJS] Stopping a Stream with TakeUntil的主要内容,如果未能解决你的问题,请参考以下文章

[RxJS] RefCount: automatically starting and stopping an execution

[RxJS] Handling a Complete Stream with Reduce

[RxJS] Combining Streams with CombineLatest

[Recompose] Stream Props to React Children with RxJS

[RxJS] Sharing Streams with Share

RxJS的基础