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

Add signal option and remove abort() method #53

Merged
merged 10 commits into from
Nov 30, 2024

Conversation

liuhanqu
Copy link
Contributor

@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
index.js Outdated
@@ -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
Contributor 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.

index.js Outdated
const registry = new FinalizationRegistry(() => {
signal.removeEventListener('abort', aborted);
});
registry.register(signal, 'signal');
Copy link
Owner

Choose a reason for hiding this comment

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

This is incorrect. It's the throttled function that needs to be the target.

And the registry should be defined at the top level:

const registry = new FinalizationRegistry(({signal, aborted}) => {
    signal.removeEventListener('abort', aborted);
});

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please review the latest commit, it's ok ?

index.js Outdated
@@ -67,6 +60,10 @@ export default function pThrottle({limit, interval, strict, onDelay}) {

const getDelay = strict ? strictDelay : windowedDelay;

const registry = new FinalizationRegistry(({signal, aborted}) => {
signal?.removeEventListener('abort', aborted);
});
Copy link
Owner

Choose a reason for hiding this comment

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

This is not the top-level.

@sindresorhus
Copy link
Owner

You need to fix the merge conflict.

@sindresorhus sindresorhus merged commit b0eadea into sindresorhus:main Nov 30, 2024
2 checks passed
@sindresorhus sindresorhus changed the title Support AbortController Add signal option and remove abort() method Nov 30, 2024
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