tc39 / proposal-signals

A proposal to add signals to JavaScript.
MIT License
2.87k stars 54 forks source link

`watched` callback calling `.unwatch` #206

Open prophile opened 2 weeks ago

prophile commented 2 weeks ago

Consider the following code:

let watcher
const a = new Signal.State(0, {
  [Signal.subtle.watched]: () => {
    watcher.unwatch(a)
  },
})

watcher = new Signal.subtle.Watcher(() => {
  console.log("notified")
});
watcher.watch(a);
a.set(1);

It's not clear from the specification what should happen here and it should probably be spelled out. In the event, the polyfill at f7c550b throws a RangeError from internal code and then leaves an inconsistent state - the watcher has a as a source but a does not have the watcher as a sync, is permissible in how the spec is currently worded but I suspect is not an invariant we want to break here.