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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support arguments for rate-limited functions #316

Merged
merged 7 commits into from
Jul 21, 2022

Conversation

afshin
Copy link
Member

@afshin afshin commented Jul 20, 2022

This PR changes Debouncer#invoke and Throttler#invoke to support passing arguments into the underlying function being debounced or throttled.

Problem

@trungleduc points out a debouncing use case that uses arguments, which is not possible with Debouncer and required writing a debounce function:

function debounce<Params extends any[]>(
  func: (...args: Params) => any,
  timeout: number = 500
): (...args: Params) => void {
  let timer: NodeJS.Timeout;
  return (...args: Params) => {
    clearTimeout(timer);
    timer = setTimeout(() => {
      func(...args);
    }, timeout);
  };
}

The debounce function allows for passing a debounced function passed via props in a component:

<PropertyFrom
  key={hash}
  hash={hash}
  property={property}
  removeProperty={removeProperty}
  setProperty={debounce(setProperty)}
/>

The debounced function is invoked via props:

props.setProperty(props.hash, newState);

Solution

With this PR, a Debouncer instance supports arguments. It can be passed via props:

<PropertyFrom
  key={hash}
  hash={hash}
  property={property}
  removeProperty={removeProperty}
  setProperty={new Debouncer(setProperty)}
/>

And it is invoked via props:

props.setProperty.invoke(props.hash, newState);

Examples

This snippet will log Hello, Louie to the console.

const debouncer = new Debouncer((name?: string) => {
  console.log(`Hello, ${name || 'world'}`);
});
void debouncer.invoke('Huey');
void debouncer.invoke('Dewey');
void debouncer.invoke('Louie');

This snippet will log Hello, Huey to the console.

const throttler = new Throttler((name?: string) => {
  console.log(`Hello, ${name || 'world'}`);
});
void throttler.invoke('Huey');
void throttler.invoke('Dewey');
void throttler.invoke('Louie');

This snippet will log Hello, world to the console.

const throttler = new Throttler((name?: string) => {
  console.log(`Hello, ${name || 'world'}`);
});
void throttler.invoke();
void throttler.invoke('Dewey');
void throttler.invoke('Louie');

Backward-compatibility

This change is fully backward-compatible. However, sub-classes of RateLimiter should note that if they plan to use protected args they must set this.args inside their invoke method.

Semantic versioning

This PR changes the API in a backward-compatible way and adds a new feature. It is a minor version bump.

@afshin afshin added the enhancement New feature or request label Jul 20, 2022
@afshin afshin force-pushed the ratelimiter-arguments branch from 2ebd989 to 54fe387 Compare July 20, 2022 16:52
@afshin afshin changed the title Support arguments to invoke a rate limited function with Support arguments for rate-limited functions Jul 20, 2022
@afshin afshin marked this pull request as ready for review July 20, 2022 18:34
@afshin afshin self-assigned this Jul 20, 2022
@afshin afshin force-pushed the ratelimiter-arguments branch 3 times, most recently from 02503bc to a0f194a Compare July 21, 2022 02:41
@afshin afshin marked this pull request as draft July 21, 2022 02:57
@afshin afshin marked this pull request as ready for review July 21, 2022 02:58
@afshin afshin force-pushed the ratelimiter-arguments branch from a0f194a to a9c2ce0 Compare July 21, 2022 03:05
@afshin afshin force-pushed the ratelimiter-arguments branch from a9c2ce0 to e1782ff Compare July 21, 2022 03:11
Copy link
Member

@fcollonval fcollonval left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor optional suggestion otherwise it looks good to me.

packages/polling/src/ratelimiter.ts Show resolved Hide resolved
@afshin afshin merged commit 58fc58d into jupyterlab:main Jul 21, 2022
@afshin afshin deleted the ratelimiter-arguments branch July 21, 2022 15:36
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants