Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

async-stream-emitter

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-stream-emitter

An alternantive to EventEmitter using consumable streams.

  • 3.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
26K
decreased by-7.5%
Maintainers
1
Weekly downloads
 
Created
Source

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

Keywords

FAQs

Package last updated on 24 Feb 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc