emitter-in-stream

Attaches an event emitter to a stream, removing all listeners when stream ends

npm install emitter-in-stream
4 downloads in the last month

emitter-in-stream

Creates an event emitter attached to another event emitter, but bound to stream life.

When stream ends, all event listeners are removed.

Install

$ npm install emitter-in-stream

Require

var EIS = require('emitter-in-stream');

Wrap emitter and stream

var ee = new EventEmitter();
var stream = //...

var eis = EIS(ee, stream);

// or

var eis = EIS(ee);
eis.liveIn(stream);

When stream ends, all listeners are removed from original emitter

test('lives in stream and reacts to stream end', function(t) {
  t.plan(3);
  var s = stream();
  var eis = emitterInStream();
  eis.liveIn(s);
  eis.on('event', function(a, b) {
    t.equal(a, 'abc');
    t.equal(b, 'def');
    s.end();
  });
  eis.emit('event', 'abc', 'def');
  eis.emit('event', 'ghi', 'jkl');
  t.ok(true);
});

Licence

MIT

npm loves you