Created
April 20, 2019 09:12
-
-
Save kosich/344486194ac5eaee28cf09697e1684af to your computer and use it in GitHub Desktop.
RXJS - Only emit bigger values than the last value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { rxObserver } = require('api/v0.3'); | |
const { timer, from, of, EMPTY } = require('rxjs'); | |
const { zip, startWith, pairwise, switchMap } = require('rxjs/operators'); | |
// create a source using values | |
const values$ = from([ 5, 10, 20, 10, 5, 20, 0, 5 ]); | |
const source$ = timer(0, 10).pipe( | |
zip(values$, (_,x)=>x) | |
); | |
// compare values pairwise | |
const result$ = source$.pipe( | |
startWith(-1), | |
pairwise(), | |
switchMap(([a,b])=> | |
b > a | |
? of(b) | |
: EMPTY | |
) | |
); | |
source$.subscribe(rxObserver('source$')); | |
result$.subscribe(rxObserver('result$')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment