proposal-signals icon indicating copy to clipboard operation
proposal-signals copied to clipboard

fix(polyfill): missing self argument for Watcher notify callback type

Open yyx990803 opened this issue 1 year ago • 2 comments

new Signal.subtle.Watcher(self => {
                       // ^ this currently lacks type
});

yyx990803 avatar Apr 01 '24 07:04 yyx990803

Thanks for this correction, but I think I should just remove that parameter from the code sample instead; passing it as the this value is enough.

littledan avatar Apr 01 '24 08:04 littledan

Addtionally to typing, this and self seem to be passed wrong.

const w = new Signal.subtle.Watcher(function (self) {
  console.log(this === w);
  console.log(this instanceof Signal.subtle.Watcher); // both false;  same with self-Argument

  console.log(this.wrapper instanceof Signal.subtle.Watcher); // true;  wrapper is the actual watcher instance

  if (needsEnqueue) {
    needsEnqueue = false;
    queueMicrotask(processPending);
  }
});

Changing the assignment of node.consumerMarkedDirty in the constructor like this or similar fixes it.

class Watcher {
  constructor(notify: (this: Watcher, self: Watcher) => void) {
    let node = Object.create(REACTIVE_NODE);
    node.wrapper = this;
    node.consumerMarkedDirty = () => {    // change to bind and pass this
      notify.call(this, this);
    };
    node.consumerIsAlwaysLive = true;
    node.consumerAllowSignalWrites = false;
    node.producerNode = [];
    this[NODE] = node;
  }
}

raven-dever avatar Apr 04 '24 19:04 raven-dever