Last active
May 29, 2019 16:51
-
-
Save kosich/25dfe51a29deb6c27300bd6ad09b0ced to your computer and use it in GitHub Desktop.
expand with a limit
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 { 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