-
-
Notifications
You must be signed in to change notification settings - Fork 31
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
Conversation
index.js
Outdated
@@ -91,15 +84,16 @@ export default function pThrottle({limit, interval, strict, onDelay}) { | |||
}); | |||
}; | |||
|
|||
throttled.abort = () => { | |||
signal?.throwIfAborted(); | |||
signal?.addEventListener('abort', () => { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
#53 (comment) is not done |
index.js
Outdated
queue.get(timeout)(new AbortError()); | ||
} | ||
signal?.throwIfAborted(); | ||
if (signal && !signal.aborted) { |
There was a problem hiding this comment.
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.
I think you could use https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry to unsubscribe when the |
index.js
Outdated
const registry = new FinalizationRegistry(() => { | ||
signal.removeEventListener('abort', aborted); | ||
}); | ||
registry.register(signal, 'signal'); |
There was a problem hiding this comment.
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);
});
There was a problem hiding this comment.
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); | |||
}); |
There was a problem hiding this comment.
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.
You need to fix the merge conflict. |
signal
option and remove abort()
method
Fixes #51