Skip to content

Instantly share code, notes, and snippets.

@kosich
Last active May 29, 2019 16:51
Show Gist options
  • Save kosich/25dfe51a29deb6c27300bd6ad09b0ced to your computer and use it in GitHub Desktop.
Save kosich/25dfe51a29deb6c27300bd6ad09b0ced to your computer and use it in GitHub Desktop.
expand with a limit
const { rxObserver } = require('api/v0.3');
const { of, timer, EMPTY } = require('rxjs');
const { expand, delay, flatMap, take } = require('rxjs/operators');
// PLEASE, OPEN THE CONSOLE
const items = [
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
[21, 22, 23, 24, 25, 26, 27, 28, 29, 30],
];
const call = () => {
console.log('#call');
const batch = items.shift();
if (batch) {
return of(batch).pipe(delay(100));
}
return EMPTY;
};
const o$ = call().pipe(
expand((values) => {
console.log('expansion');
return timer(0).pipe(map(call));
})
, flatMap((val) => val)
);
const log = (prefix) => (...args) => console.log(prefix, ...args);
o$.pipe(take(9))
.subscribe(rxObserver());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment