码迷,mamicode.com
首页 > Web开发 > 详细

[RxJS] Implement pause and resume feature correctly through RxJS

时间:2017-05-30 18:13:41      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:ons   empty   using   jsonp   cas   run   ssi   res   interval   

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

 

[RxJS] Implement pause and resume feature correctly through RxJS

标签:ons   empty   using   jsonp   cas   run   ssi   res   interval   

原文地址:http://www.cnblogs.com/Answer1215/p/6920445.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!