@socket-mesh/async-stream-emitter
TypeScript icon, indicating that this package has built-in type declarations

7.1.2 • Public • Published

async-stream-emitter

EventEmitter using ConsumableStream.

Main methods:

  • emit(eventName, data)
  • listener(eventName)
  • closeListener(eventName)
  • closeAllListeners()
  • killListener(eventName)
  • killAllListeners()
  • getListenerBackpressure(eventName)
  • getAllListenersBackpressure()

Usage examples

let emitter = new AsyncStreamEmitter();

(async () => {
  await wait(10);
  emitter.emit('foo', 'hello');

  // This will cause all for-await-of loops for that event to exit.
  // Note that you can also use the 'break' statement inside
  // individual for-await-of loops.
  emitter.closeListener('foo');
})();

(async () => {
  for await (let data of emitter.listener('foo')) {
    // data is 'hello'
  }
  console.log('The listener was closed.');
})();

// Utility function.
function wait(duration) {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve();
    }, duration);
  });
}

Package Sidebar

Install

npm i @socket-mesh/async-stream-emitter

Weekly Downloads

5

Version

7.1.2

License

MIT

Unpacked Size

7.91 kB

Total Files

5

Last publish

Collaborators

  • gak10100