Skip to content

Instantly share code, notes, and snippets.

@kosich
Created March 23, 2019 21:33
Show Gist options
  • Select an option

  • Save kosich/73877d92ddc6ca8e4a53af1e8effb688 to your computer and use it in GitHub Desktop.

Select an option

Save kosich/73877d92ddc6ca8e4a53af1e8effb688 to your computer and use it in GitHub Desktop.
const { rxObserver } = require('api/v0.3');
const { Subject, interval, merge } = require('rxjs');
const { timeInterval, filter, distinctUntilChanged, map , pairwise } = require('rxjs/operators');
const START_EVENT = 'RUN';
const END_EVENT = 'END';
const events$ = new Subject();
const result$ =
events$
.pipe(
timeInterval(),
pairwise(),
filter(([first, second]) =>
first.value === START_EVENT
&& second.value === END_EVENT
),
map(([, second]) => second.interval)
);
events$.subscribe(rxObserver());
result$.subscribe(rxObserver());
// push events
setTimeout(()=>{
events$.next(START_EVENT);
}, 100);
setTimeout(()=>{
events$.next(START_EVENT);
}, 200);
setTimeout(()=>{
events$.next(END_EVENT);
}, 550);
@kosich
Copy link
Author

kosich commented Mar 23, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment