tc39 / proposal-signals

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

Polyfill calls `equals` function unbound #213

Closed prophile closed 2 weeks ago

prophile commented 2 weeks ago

Consider the following code:

const state = new Signal.State(0, {
  equals: (a, b) => {
    console.log(this);
    return true;
  }
});
state.set(1);

The spec says:

        // Custom comparison function between old and new value. Default: Object.is.
        // The signal is passed in as the this value for context.
        equals?: (this: Signal<T>, t: T, t2: T) => boolean;

and:

Various callbacks (e.g., equals, the computed callback) are called with the relevant Signal as the this value for context, so that a new closure isn't needed per Signal. Instead, context can be saved in extra properties of the signal itself.

The polyfill in f7c550b currently prints undefined however.

robbiespeed commented 2 weeks ago

@prophile do you get the same result with:

equals: function (a, b) {
  console.log(this);
  return true;
}

this within arrow functions use the this from the scope with which they were created rather than the caller.

prophile commented 2 weeks ago

Ach how embarrassing, it was a long day. Careless mistake on my part, sorry for the noise!