JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 60650
  • Score
    100M100P100Q151728F
  • License MIT

An alternantive to EventEmitter using consumable streams.

Package Exports

  • async-stream-emitter

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (async-stream-emitter) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

async-stream-emitter

EventEmitter using ConsumableStream.

Methods:

  • emit(eventName, data)
  • listener(eventName)
  • closeListener(eventName)
  • closeAllListeners()

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);
  });
}

Note that unlike with EventEmitter, you cannot get the count for the number of active listeners at any given time. This is intentional as it encourages code to be written in a more declarative style and lowers the risk of memory leaks.

If you want to track listeners, you should do it yourself. The new ECMAScript Symbol type should make tracking object references easier: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol