Created
May 6, 2021 13:38
-
-
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)
This file contains hidden or 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
| 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