Skip to content

Commit

Permalink
Merge pull request #609 from briancaine/bugfix/remove_listener_destru…
Browse files Browse the repository at this point in the history
…ctor

Stream removes itself from EventEmitter source upon destruction.
  • Loading branch information
vqvu authored Mar 23, 2017
2 parents 89481f3 + 6343d11 commit 8d19fe1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,21 @@ function __(StreamCtor) {
var mapper = hintMapper(mappingHint);

s = new StreamCtor();
secondArg.on(xs, function () {

var callback_func = function () {
var ctx = mapper.apply(this, arguments);
s.write(ctx);
});
};

secondArg.on(xs, callback_func);
var removeMethod = secondArg.removeListener // EventEmitter
|| secondArg.unbind; // jQuery

if (removeMethod) {
s._destructors.push(function() {
removeMethod.call(secondArg, xs, callback_func);
});
}
}
else {
throw new Error(
Expand Down
15 changes: 15 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2864,6 +2864,21 @@ exports['wrap EventEmitter (or jQuery) on handler'] = function (test) {
});
};

exports['removing EventEmitter (or jQuery) listener on destruction (issue #500)'] = function(test) {
var ee = new EventEmitter();
var s = _('myevent', ee);

var removed = false;

ee.on('removeListener', function() {
removed = true;
});

s.destroy();
test.ok(removed);
test.done();
};

exports['wrap EventEmitter (or jQuery) on handler with args wrapping by function'] = function (test) {
var ee = {
on: function (name, f) {
Expand Down

0 comments on commit 8d19fe1

Please sign in to comment.