[RxJS] Split an RxJS observable conditionally with windowToggle

Posted Answer1215

tags:

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

There are variants of the window operator that allow you to split RxJS observables in different ways. In this lesson we will explore the windowToggle variant and see one of its use cases in user interfaces.

 

Let‘s say we want to build a new functionality, it only receive the source when mousedown and stop receiving data when mouse up.

To do that we can use ‘windowToggle‘:

const clockObs = Rx.Observable.interval(1000);
const downObs = Rx.Observable.fromEvent(document, mousedown);
const upObs = Rx.Observable.fromEvent(document, mouseup);

const source = clockObs
  .windowToggle(downObs, () => upObs)
  .switch()
  .subscribe(console.log);

/*
--0--1--2--3--4--5--6--7--8--9--
          windowToggle
----------D--------U-------------
                    -3--4--5-|
          
          switch / mergeAll
          
-----------3--4--5---------------          
*/

 

以上是关于[RxJS] Split an RxJS observable conditionally with windowToggle的主要内容,如果未能解决你的问题,请参考以下文章

[RxJS] Subject: an Observable and Observer hybrid

[RxJS] RefCount: automatically starting and stopping an execution

Angular 6 / Rxjs - 如何基础:observables 成功,错误,最后

如何理解 RxJS?RxJS的中文API和使用教程

rxjs-tslint vs rxjs-tslint-rules 包

RxJS 6有哪些新变化?