Advertisement
fabiobiondi

Implement pause and resume feature correctly through RxJS

Apr 5th, 2018
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. const resume$ = new Rx.Subject();
  2.  
  3. const res$ = resume$
  4. .switchMap(resume =>
  5. resume ?
  6. Rx.Observable.interval(2000) :
  7. Rx.Observable.empty()
  8. )
  9. .do(x => console.log('request it! ' + x) || displayInPreview('request it! ' + x))
  10. .switchMap(ev => Rx.Observable.ajax({
  11. url: 'https://jsonplaceholder.typicode.com/users/1',
  12. method: 'GET',
  13. }));
  14.  
  15. res$.subscribe(function (data) {
  16. console.log(data.response)|| displayInPreview(data.response.company.bs);
  17. });
  18.  
  19. resume$.next(false);
  20. setTimeout(() => resume$.next(true), 500);
  21. setTimeout(() => resume$.next(false), 5000);
  22.  
  23.  
  24.  
  25. // display in plunker preview
  26. function displayInPreview(string) {
  27. var newDiv = document.createElement("div");
  28. var newContent = document.createTextNode(string);
  29. newDiv.appendChild(newContent);
  30. document.body.appendChild(newDiv)
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement