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

fix: cleanup signal listener #1113

Merged
merged 2 commits into from
Dec 13, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions lib/fetch/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ let TransformStream

const kInit = Symbol('init')

const requestFinalizer = new FinalizationRegistry(({ signal, abort }) => {
signal.removeEventListener('abort', abort)
})

// https://fetch.spec.whatwg.org/#request-class
class Request {
// https://fetch.spec.whatwg.org/#dom-request
Expand Down Expand Up @@ -328,14 +332,9 @@ class Request {
if (signal.aborted) {
ac.abort()
} else {
// TODO: Remove this listener on failure/success.
signal.addEventListener(
'abort',
function () {
ac.abort()
},
{ once: true }
)
const abort = () => ac.abort()
signal.addEventListener('abort', abort, { once: true })
requestFinalizer.register(this, { signal, abort })
Copy link
Member

Choose a reason for hiding this comment

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

We should be unregistering this after successful completion. Always passing through the registry is expensive.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yea, but if we could do that we wouldn't need this hack at all.

}
}

Expand Down