diff --git a/lib/index.js b/lib/index.js index c445020..f127cee 100755 --- a/lib/index.js +++ b/lib/index.js @@ -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( diff --git a/test/test.js b/test/test.js index 90d3db7..64d13e2 100755 --- a/test/test.js +++ b/test/test.js @@ -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) {