Skip to content

Instantly share code, notes, and snippets.

@EmilienD
Created May 6, 2021 13:38
Show Gist options
  • Select an option

  • Save EmilienD/377bf818e23b005243355b0794c057e5 to your computer and use it in GitHub Desktop.

Select an option

Save EmilienD/377bf818e23b005243355b0794c057e5 to your computer and use it in GitHub Desktop.
"Accumulate" all the calls to a function in a each iteration of the event loop so that they can be treated together ("insert all" instead of "insert one" for every call, or actually applying only the last "update" of a model for example)
function accumulator(callback){
let acc = []
let timeout = null
return (val) =>{
acc.push(val)
clearTimeout(timeout)
timeout = setTimeout(() => {
callback(acc)
acc = []
}, 0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment