proxy-observable icon indicating copy to clipboard operation
proxy-observable copied to clipboard

observable.off doesn't unsubscribe observable.once callback

Open rnike opened this issue 4 years ago • 1 comments

According to the document, using observable.off can unsubscribe a callback of observable.on or observable.once.

The off method works fine with on, but it doesn't work properly with once, here is my example code:

var observable = require("proxy-observable")

const obs = observable({});
const callback = obs.once('sub', value => {
    console.log('sub changed to: '+ value);
});

const success = obs.off(callback);

obs.sub = '123';

console.log("is off success: "+ success);

// expect output:
// - is off success: true

// actual output:
// - sub changed to: 123
// - is off success: false

Expecting the callback should be unsubscribed but it doesn't

I'm working on version 4.0.4

rnike avatar Jan 14 '21 15:01 rnike

Take a quick look into the source code, I think the problem is here

  once(e, fn) {
    const method = (value, prev, prop) => {
      fn(value, prev, prop);
      this.off(method);
    };
    this.on(e, method);
-   return fn;
+   return method;
  }
}

it should be returning method not fn

rnike avatar Jan 14 '21 16:01 rnike