Skip to content

Instantly share code, notes, and snippets.

@kosich
Last active April 1, 2019 00:01
Show Gist options
  • Save kosich/c14c6642424e485de51ab1c8c2d979be to your computer and use it in GitHub Desktop.
Save kosich/c14c6642424e485de51ab1c8c2d979be to your computer and use it in GitHub Desktop.
Switch sources on timeout
const { chart } = require('rp-api');
const { from, timer } = require('rxjs');
const { delayWhen, share, map, timeout, catchError, repeat, take } = require('rxjs/operators');
const source$ = from([ 5, 20, 25, 30, 42, 45 ])
.pipe(
delayWhen(timer)
, share()
);
const timer$ = timer(0, 5)
.pipe(
map(x => 't:' + x)
, share()
);
const output$ = source$
.pipe(
timeout(2)
, catchError(()=>
timer$
.takeUntil(source$)
.merge(source$.take(1))
)
, repeat()
, take(10)
);
timer$.take(10).subscribe(chart.createRxObserver());
source$.subscribe(chart.createRxObserver());
output$.subscribe(chart.createRxObserver());
@kosich
Copy link
Author

kosich commented Apr 1, 2019

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