Skip to content

Instantly share code, notes, and snippets.

@kosich
Created April 20, 2019 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kosich/344486194ac5eaee28cf09697e1684af to your computer and use it in GitHub Desktop.
Save kosich/344486194ac5eaee28cf09697e1684af to your computer and use it in GitHub Desktop.
RXJS - Only emit bigger values than the last value
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