-
-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Batched function calls (debounce) (馃挌 completed) #136
Comments
function batchFn(function_) {
let queue = [];
return function addToQueue(value) {
queue.push(value);
if (queue.length === 1) {
setTimeout(() => {
function_(queue);
queue = [];
});
}
}
} |
Also consider: |
Pretty good! That format also allows to include a simple "ms" parameter to batchFn to pass it straight to setTimeout. Types are probably the next difficult part |
export default function batchFn<ValueType>(function_: (value: ValueType[]) => unknown): (value: ValueType) => void; |
Are you going to publish that? 馃檹 |
ok https://github.com/Richienb/batched-function @fregante would you mind if I added you as a collaborator? |
Sure. I'd probably like optionally replacing the setTimeout too, like with setImmediate or nextTick |
I added an option that allows the milliseconds between invokations to be changed, maybe another function could be used when no value is provided but it couldn't be either of those 2 because neither work in the browser. |
What I wanted was |
Perhaps it should be used if the timeout is |
SGTM. Also PR welcome to Refined GitHub to replace the local copy. |
Batched execution:
fun(1)
fun(2)
fun(4)
_fun([1, 2, 4])
Would be cool:
Some of the complexity here is in managing the types for these functions.
Real-life scenario: refined-github/refined-github#5991 (comment)
The text was updated successfully, but these errors were encountered: