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

feat(debounce): Support AbortSignal to debounce for improved cancellation #45

Merged
merged 7 commits into from
Jun 13, 2024

Conversation

Hanna922
Copy link
Contributor

@Hanna922 Hanna922 commented Jun 13, 2024

Hi👋🏻 I have reviewed issue #37 and thought it was a good feature, so I have added it.

The debounce function already supports cancellation through its return value, but its usability would improve if it also supported cancellation via AbortSignal, which is implemented in all major web browsers and JavaScript runtimes.

If this change is acceptable, I'll add docs and extend this functionality to the delay function.

Before

function debounce<F extends (...args: any[]) => void>(func: F, debounceMs: number): F & { cancel: () => void }

After

function debounce<F extends (...args: any[]) => void>(
  func: F,
  debounceMs: number,
  abortSignal?: AbortSignal
): F & { cancel: () => void }

Copy link

vercel bot commented Jun 13, 2024

@Hanna922 is attempting to deploy a commit to the Toss Team on Vercel.

A member of the Team first needs to authorize it.

@raon0211
Copy link
Collaborator

Hello @Hanna922, Thanks for your contribution!

export function debounce<F extends (...args: any[]) => void>(
func: F,
debounceMs: number,
abortSignal?: AbortSignal
Copy link
Collaborator

@raon0211 raon0211 Jun 13, 2024

Choose a reason for hiding this comment

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

To receive signals, what about we follow the fetch API's option? It would be helpful when we add new options to our debounce function.

interface DebounceOptions {
  signal?: AbortSignal;
}

export function debounce<F extends (...args: any[]) => void>(
  func: F,
  debounceMs: number,
  { signal }: DebounceOptions = {}
) { /* ... */ }

Copy link
Contributor Author

@Hanna922 Hanna922 Jun 13, 2024

Choose a reason for hiding this comment

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

It was applied in b6ed097

Comment on lines 64 to 66
if (abortSignal) {
abortSignal.removeEventListener('abort', onAbort);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if (abortSignal) {
abortSignal.removeEventListener('abort', onAbort);
}
abortSignal?.removeEventListener('abort', onAbort);

What about using optional chaining here?

Comment on lines 69 to 71
if (abortSignal) {
abortSignal.addEventListener('abort', onAbort);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if (abortSignal) {
abortSignal.addEventListener('abort', onAbort);
}
abortSignal?.addEventListener('abort', onAbort);

Here too

Copy link
Contributor Author

@Hanna922 Hanna922 Jun 13, 2024

Choose a reason for hiding this comment

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

It was applied in 8cf4772

@Hanna922
Copy link
Contributor Author

Hanna922 commented Jun 13, 2024

@raon0211 I have applied the comments and added docs! Thank you 🙇🏻‍♀️

Copy link
Collaborator

@raon0211 raon0211 left a comment

Choose a reason for hiding this comment

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

Awesome work!

@raon0211 raon0211 merged commit a707c06 into toss:main Jun 13, 2024
5 of 6 checks passed
@Hanna922 Hanna922 deleted the feat/abortSignal branch June 13, 2024 23:36
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

Successfully merging this pull request may close these issues.

2 participants