Last active
February 6, 2018 21:00
-
-
Save Restuta/bc23a984e89ad33da84bb279c107af49 to your computer and use it in GitHub Desktop.
RxJS buffer function calls
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
console.clear() | |
async function main() { | |
const delay = time=>new Promise(resolve => setTimeout(()=>resolve(), time)) | |
const functionInvocationsObservable = new Rx.Subject() | |
.bufferTime(/* buffer time*/ 500, /* restart buffer, -1 means never */ -1, /* maxBufferSize*/ 3) | |
.filter(x => x.length > 0) | |
// .subscribe(console.log) | |
const batchify = func => { | |
functionInvocationsObservable.subscribe(x => { | |
console.log(x) | |
func(x) | |
}) | |
return function() { | |
functionInvocationsObservable.next(...arguments) | |
} | |
} | |
const foo = batchify(x=>x * 2) | |
for (let i = 0; i <= 10; i++) { | |
await delay(100) | |
console.info(`invoked with: ${i}`) | |
foo(i) | |
} | |
await delay(100) | |
console.info(`invoked with: ${28}`) | |
foo(28) | |
foo(30) | |
foo(42) | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment