[RxJS] Implement pause and resume feature correctly through RxJS

Eventually you will feel the need for pausing the observation of an Observable and resuming it later. In this lesson we will learn about use cases where pausing is possible, and what to do when pausing is impossible.

const resume$ = new Rx.Subject();

const res$ = resume$
  .switchMap(resume =>
    resume ?
      Rx.Observable.interval(2000) :
      Rx.Observable.empty()
  )
  .do(x => console.log(‘request it! ‘ + x))
  .switchMap(ev => Rx.Observable.ajax({
    url: ‘https://jsonplaceholder.typicode.com/users/1‘,
    method: ‘GET‘,
  }));

res$.subscribe(function (data) {
  console.log(data.response);
});

resume$.next(false);
setTimeout(() => resume$.next(true), 500);
setTimeout(() => resume$.next(false), 5000);

here use

Rx.Observable.empty()

inside switchMap(), it means if code goes to empty(), then the rest of code:

 .do().switchMap()

won‘t run.

If just subscribe, it trigger complete function:

var source = Rx.Observable.empty();

var subscription = source.subscribe(
  function (x) {
    console.log(‘Next: %s‘, x);
  },
  function (err) {
    console.log(‘Error: %s‘, err);
  },
  function () {
    console.log(‘Completed‘);
  });

// => Completed
时间: 2024-10-27 05:38:49

[RxJS] Implement pause and resume feature correctly through RxJS的相关文章

[RxJS] Implement RxJS `switchMap` by Canceling Inner Subscriptions as Values are Passed Through

switchMap is mergeMap that checks for an "inner" subscription. If the "inner" subscription exists, switchMap unsubscribes from that "inner" subscription which effectively "cancels" any pending pushes. import { fromE

[RxJS] Implement RxJS `mergeMap` through inner Observables to Subscribe and Pass Values Through

Understanding sources and subscribers makes it much easier to understand what's going on with mergeMap under the hood. Where a typical operator invokes destination.next directly, mergeMap wraps destination.next inside of a new source/subscriber combo

Leverage rabbitmq reliablity feature correctly

Producer: Broker can send to producer a confirmation after receiving message and successfully persisted it. The producer needs to register a confirmation listener to process the confirmation message. In confirmation mode, each produced message for a

[RxJS] Aggregating Streams With Reduce And Scan using RxJS

What is the RxJS equivalent of Array reduce? What if I want to emit my reduced or aggregated value at each event? This brief tutorial covers Observable operators reduce() and scan(), their differences and gotchas. In ES5, the Array's reduce function

[RxJS] Reactive Programming - Using cached network data with RxJS -- withLatestFrom()

So now we want to replace one user when we click the 'x' button. To do that, we want: 1. Get the cached network data for generating the userList. 2. Then get a random user from the cached data. 3. Showing the user in the list. We have the function to

构建流式应用—RxJS详解

讲之前先说一点哈,插入的图片,不能正常显示,图片一半被遮盖了,如果想看,鼠标右击然后图片地址,图片另存为去看.如果有知道怎么解决的,可以在下面给我评论,我会及时的修改. 好啦,开始: 目录 常规方式实现搜索功能 RxJS · 流 Stream RxJS 实现原理简析 观察者模式 迭代器模式 RxJS 的观察者 + 迭代器模式 RxJS 基础实现 Observable Observer RxJS · Operators Operators ·入门 一系列的 Operators 操作 使用 RxJS

[RxJS] Learn How To Use RxJS 5.5 Beta 2

The main changes is about how you import rxjs opreators from now on. And introduce lettable opreator. import { range } from 'rxjs/observable/range'; import { map, filter, scan } from 'rxjs/operators'; const source$ = range(0, 10); source$.pipe( filte

rxjs全局命名空间

通常我们学习某个js库的功能时,我们会直接在html中用script引入该js库来写一些demo.笔者在学习rxjs时,用script标签引入时,就掉到了坑里. 从6.0.0-alpha.4版本起,(目前版本为6.3.0)全局命名空间为rxjs,使用方法如下: <script src="https://cdn.bootcss.com/rxjs/6.0.0-alpha.4/rxjs.umd.js"></script> <script> /* * htt

一起来看 rxjs

更新日志 2018-05-26 校正 2016-12-03 第一版翻译 过去你错过的 Reactive Programming 的简介 你好奇于这名为Reactive Programming(反应式编程)的新事物, 更确切地说,你想了解它各种不同的实现(比如 [Rx*], [Bacon.js], RAC 以及其它各种各样的框架或库) 学习它比较困难, 因为比较缺好的学习材料(译者注: 原文写就时, RxJs 还在 v4 版本, 彼时社区对 RxJs 的探索还不够完善). 我在开始学习的时候, 试