Skip to content

Commit

Permalink
Fixed .off() race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill89 committed Jul 12, 2016
1 parent 2ecfce2 commit d8d081a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ Wire.prototype.__emitOne = function (ch, params) {
var fn = wh.func;
var p = self.__p.resolve();

// Check if handler removed
if (!fn) return;

if (wh.once) { self.off(wh.channel, fn); }

if (wh.gen) {
Expand Down
18 changes: 18 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,24 @@ describe('Wire', function () {
});


it('async + `.off()` race condition', function (done) {
var w = ew();

function h2() {
}

function h1(callback) {
w.off('test', h2);
setTimeout(callback, 1);
}

w.on('test', h1);
w.on('test', h2);

w.emit('test', done);
});


describe('errors', function () {

it('bad priority', function () {
Expand Down

0 comments on commit d8d081a

Please sign in to comment.