scottcorgan / tiny-emitter

A tiny (less than 1k) event emitter library
MIT License
935 stars 67 forks source link

off doesn't unbind once #10

Closed Prinzhorn closed 9 years ago

Prinzhorn commented 9 years ago

I'm surprised this hasn't been discovered before as it's a major bug.

var assert = require('assert');
var Emitter = require('tiny-emitter');
var emitter = new Emitter();

var pleaseNeverCallMe = assert.bind(assert, false, 'You called me, didn\'t you?');
var pleaseNeverCallMeEvenOnce = assert.bind(assert, false, 'You called me, didn\'t you?');

emitter.on('some-event', pleaseNeverCallMe);
emitter.once('some-event', pleaseNeverCallMeEvenOnce);

emitter.off('some-event', pleaseNeverCallMe);
emitter.off('some-event', pleaseNeverCallMeEvenOnce);

emitter.emit('some-event');

The cause is simple: you use on internally with a wrapper function, not the original one.

I just randomly chose another module and checked how they do it. E.g. little-emitter. They add a property to the wrapper function which references the original one.

https://github.com/Alex1990/little-emitter/blob/master/emitter.js#L58 https://github.com/Alex1990/little-emitter/blob/master/emitter.js#L78

scottcorgan commented 9 years ago

That's very interesting! Any way you can submit a PR with a failing test?

Prinzhorn commented 9 years ago

11

Prinzhorn commented 9 years ago

thanks!