Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. const render$ = new Subject()
  2.  
  3. render$.subscribe(
  4. value => output.textContent = '' + value
  5. );
  6.  
  7. // Just a source that increments a number over time when
  8. // you click buttons.
  9. const source$ = render$.pipe(
  10. startWith(INITIAL_VALUE),
  11. switchMap(initialValue => (
  12. merge(
  13. // when we click "+ 1" it adds `1`
  14. add1Click$.pipe(map(() => 1)),
  15. // when we click "+ 2" it adds `2`
  16. add2Click$.pipe(map(() => 2)),
  17. )
  18. .pipe(
  19. // A reducer to accumlate our number
  20. scan((acc, n) => acc + n, initialValue || 0),
  21. )
  22. )),
  23. // Start with 0, so we have an initial value to display
  24. startWith(INITIAL_VALUE),
  25. );
  26.  
  27. // Here we're adding our custom `undo` and subscribing to it.
  28. source$.pipe(
  29. // "Undo" whenever we click the "Undo" button.
  30. undo(undoClick$, redoClick$),
  31. ).subscribe(render$);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement