Skip to content

Instantly share code, notes, and snippets.

@Restuta
Last active February 6, 2018 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Restuta/bc23a984e89ad33da84bb279c107af49 to your computer and use it in GitHub Desktop.
Save Restuta/bc23a984e89ad33da84bb279c107af49 to your computer and use it in GitHub Desktop.
RxJS buffer function calls
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