Skip to content

Conversation

rbuckton
Copy link
Collaborator

Per the September 2022 plenary, the overloaded use method should be split into multiple, distinct methods. These methods are as follows:

  • use(value) - Adds a disposable resource to the stack.
    • If the argument is null or undefined, it is returned as-is.
    • If the argument is not an Object, an error is thrown.
    • If the argument does not have a [Symbol.dispose]() method, an error is thrown.
  • adopt(value, onDispose) - Adds a non-disposable resource to the stack with a dispose callback.
    • If onDispose is not callable, an error is thrown.
    • value can be any value, including null or undefined. It will always be tracked.
    • When the stack is disposed, the onDispose callback will be invoked with value as its argument (i.e., onDispose(value)).
  • defer(onDispose) - Adds a callback to the stack.
    • If onDispose is not callable, an error is thrown.
    • When the stack is disposed, the onDispose callback will be invoked with no argument (i.e., onDispose()).

Fixes #102

@rbuckton rbuckton added this to the Stage 3 milestone Sep 16, 2022
@github-actions
Copy link

A preview of this PR can be found at https://tc39.es/proposal-explicit-resource-management/pr/103.

README.md Outdated
@@ -860,11 +873,84 @@ class PluginHost {
}

[Symbol.dispose]() {
this.#socket = undefined;
Copy link

@waldemarhorwat waldemarhorwat Sep 22, 2022

Choose a reason for hiding this comment

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

Do these assignments do anything necessary for disposal, or are they just stylistic to keep something (what?) from referring to the private fields after Symbol.dispose is run?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's not necessary for the Disposable protocol, but good housekeeping in general. Any resources held by #socket would be freed when #disposables is disposed. Removing the reference to #socket would allow the object in #socket to be GC'd (since it's no longer usable).

In addition, a disposable object should try to ensure that any access is consistent with its "disposed" state, and clearing #socket (and #channel) might be one way to ensure that any attempt to use PluginHost after it has been disposed will fail due to a ReferenceError being thrown from any attempt to use the undefined #socket.

@rbuckton
Copy link
Collaborator Author

Unless there is any additional feedback, I'm hoping to merge this later today.

@rbuckton rbuckton merged commit 2df6c9e into main Nov 15, 2022
@rbuckton rbuckton deleted the split-use-overloads branch November 15, 2022 21:25
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.

Split overloaded 'use' method into multiple methods.
2 participants