首页 > 代码库 > [RxJS] Implement pause and resume feature correctly through RxJS
[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
[RxJS] Implement pause and resume feature correctly through RxJS
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。