Skip to content

feat: Add Symbol.dispose and Symbol.asyncDispose support to DispatcherBase #4258

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 10 additions & 1 deletion lib/dispatcher/dispatcher-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ const {
InvalidArgumentError
} = require('../core/errors')
const { kDestroy, kClose, kClosed, kDestroyed, kDispatch } = require('../core/symbols')

const kOnDestroyed = Symbol('onDestroyed')
const kOnClosed = Symbol('onClosed')

function noop () {}

class DispatcherBase extends Dispatcher {
constructor () {
super()
Expand Down Expand Up @@ -156,6 +157,14 @@ class DispatcherBase extends Dispatcher {
return false
}
}

async [Symbol.asyncDispose] () {
await this.close()
}

[Symbol.dispose] () {
this.close(noop)
}
Comment on lines +164 to +167

Choose a reason for hiding this comment

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

Suggested change
[Symbol.dispose] () {
this.close(noop)
}

Queueing a callback does not equate to a synchronous task. The semantics of explicit resource management are such that by the time the disposer returns (or the async disposer's Promise is resolved), the resource disposal should be complete, to maintain the expected order of operations; this is not the case here.

Copy link
Member

Choose a reason for hiding this comment

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

I don’t believe there’s much else we can do for this; the close method is async by default, we cannot ensure sync guarantees.

Choose a reason for hiding this comment

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

Aye, hence only the asyncDispose method makes sense here.

}

module.exports = DispatcherBase
Loading