tc39 / proposal-signals

A proposal to add signals to JavaScript.
MIT License
3.45k stars 59 forks source link

Watcher should be Disposable #117

Open DavidANeil opened 8 months ago

DavidANeil commented 8 months ago

A watcher should be easily disposable, without needing to track which signals it was previous watching. Using the (currently) stage 3 Symbol.dispose seems most appropriate.

Normative Spec Text

Method: Signal.subtle.Watcher.prototype[Symbol.dispose]()

  1. Invoke this.unwatch.apply(this, [[signals]])
EisenbergEffect commented 8 months ago

In the meantime, here's how you can do it with the current API:

watcher.unwatch(...Signal.subtle.introspectSources(watcher));
littledan commented 8 months ago

Right, we could add this as a very small convenience, but it definitely doesn’t require any extra tracking to unwatch all sources, due to introspection. And you can look at the git history to see that we very recently did have such a Symbol.dispose method—I have gone back and forth in my thoughts on whether this is important.