tc39 / proposal-signals

A proposal to add signals to JavaScript.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Should "unwatch" be a returned function or object?

dead-claudia opened this issue · comments

Just thinking that watcher.watch(signal) could simply return a function that removes that watcher reference. It'd allow for duplicate watchers, and it'd simplify managing watchers a bit.

Maybe... I was hoping that we could avoid allocations like this. I'm not sure if we especially need duplicate watchers.

Why is a function a worse allocation than an object containing one?

Right now, .watch() returns undefined and doesn't have to allocate anything. This idea would allocate a function or object to represent the subscription.

Worth noting that .watch() still needs to store the watcher so it can return it as part of .getPending(). So potentially some allocation (an internal resizable array) is still going to happen regardless.

I do separately have a suggestion of removing APIs like watcher.getPending() in favor of a boolean flag providing that functionality in signals in #166, so that memory overhead could get recovered in the common case of just one watched signal.