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 AbortController #53

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Conversation

liuhanqu
Copy link

@liuhanqu liuhanqu commented May 21, 2024

Fixes #51

readme.md Outdated Show resolved Hide resolved
readme.md Show resolved Hide resolved
index.d.ts Outdated Show resolved Hide resolved
index.d.ts Show resolved Hide resolved
index.d.ts Outdated Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
@@ -91,15 +84,16 @@ export default function pThrottle({limit, interval, strict, onDelay}) {
});
};

throttled.abort = () => {
signal?.throwIfAborted();
signal?.addEventListener('abort', () => {
Copy link
Owner

Choose a reason for hiding this comment

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

You need to remove this listener when the function is done, so not to create event listener leaks.

Copy link
Author

Choose a reason for hiding this comment

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

It seem to have no way to know that the function is done?

@sindresorhus
Copy link
Owner

#53 (comment) is not done

index.js Outdated
queue.get(timeout)(new AbortError());
}
signal?.throwIfAborted();
if (signal && !signal.aborted) {
Copy link
Owner

Choose a reason for hiding this comment

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

The aborted check is moot as throwIfAborted already takes care of that.

@sindresorhus
Copy link
Owner

I think you could use https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry to unsubscribe when the throttle function is garbage-collected.

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.

Support AbortController
2 participants