Skip to content
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

Closed
fregante opened this issue Sep 17, 2022 · 11 comments
Closed

Batched function calls (debounce) (馃挌 completed) #136

fregante opened this issue Sep 17, 2022 · 11 comments

Comments

@fregante
Copy link

fregante commented Sep 17, 2022

Batched execution:

  1. fun(1)
  2. fun(2)
  3. fun(4)
  4. Then the original _fun is called as _fun([1, 2, 4])

Would be cool:

  • original function accepts array
  • batched function accepts single item
  • option to call batched functions in the next tick or after a timeout (probably defaulting to setTimeout(0) is good enough)

Some of the complexity here is in managing the types for these functions.

Real-life scenario: refined-github/refined-github#5991 (comment)

@Richienb
Copy link

Richienb commented Sep 17, 2022

function batchFn(function_) {
  let queue = [];

  return function addToQueue(value) {
    queue.push(value);

    if (queue.length === 1) {
      setTimeout(() => {
        function_(queue);
        queue = [];
      });
    }
  }
}

@Richienb
Copy link

Richienb commented Sep 17, 2022

Also consider: queueMicrotask()

@fregante
Copy link
Author

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

@Richienb
Copy link

export default function batchFn<ValueType>(function_: (value: ValueType[]) => unknown): (value: ValueType) => void;

fregante added a commit to refined-github/refined-github that referenced this issue Sep 19, 2022
@fregante
Copy link
Author

fregante commented Feb 3, 2023

Are you going to publish that? 馃檹

@Richienb
Copy link

Richienb commented Feb 4, 2023

ok https://github.com/Richienb/batched-function

@fregante would you mind if I added you as a collaborator?

@fregante
Copy link
Author

fregante commented Feb 4, 2023

Sure. I'd probably like optionally replacing the setTimeout too, like with setImmediate or nextTick

@fregante fregante changed the title Batched function calls (debounce) Batched function calls (debounce) (馃挌 completed) Feb 4, 2023
@Richienb
Copy link

Richienb commented Feb 4, 2023

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.

@fregante
Copy link
Author

fregante commented Feb 4, 2023

What I wanted was Promise.resolved().then() to attempt calling them "synchronously" before the browser paints something on the screen. I think automatically switching to that when the timeout is 0 is a good compromise.

@Richienb
Copy link

Richienb commented Feb 4, 2023

Perhaps it should be used if the timeout is undefined, and setTimeout is used when timeout is any number.

@fregante
Copy link
Author

fregante commented Feb 4, 2023

SGTM. Also PR welcome to Refined GitHub to replace the local copy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants