Socket
Socket
Sign inDemoInstall

EventEmitter

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

EventEmitter

Javascript Event Emitter


Version published
Maintainers
1
Created
Source

EventEmitter in JavaScript

Synopsis

EventEmitter is an implementation of the Event-based architecture in JavaScript.

The code is written using the ES2015 approaches, including creation of private property through WeakMap that allows you to not to check for clearing memory, and let it do to the garbage collector.

The module contains the most basic and necessary things: subscription to an event, unsubscribing from the events, running event only once, setting the maximum number of listeners.

The focus is on code readability, speed of execution, getting rid of all the excess.

You can use this library in browser either at the server as within the node.js.

Dependencies

There are no dependencies. You need only npm installed and just run npm install to grab the development dependencies.

Examples

  let EM = new EventEmitter();

  EM.on('foo', () => {
    // some code...
  });

  EM.emit('foo');
  let EM = new EventEmitter();

  EM.once('foo', () => {
    // some code...
  });

  EM.emit('foo');
  let EM = new EventEmitter();

  EM.once('foo', (bar, baz) => {
    // some code...
  });

  EM.emit('foo', 'var 1 for bar', 'var 2 for baz');
  let EM = new EventEmitter();

  EM.on('foo', () => {
    // some code...
  });

  // Note: you can use chaining.
  EM
    .emit('foo')
    .emit('foo')
    .off('foo');
  // You can set maxNumberOfListeners as a parameter when creating new object.
  let EM = new EventEmitter(1);

  EM.on('foo', () => {
    // some code...
  });
  // Note: it will show notification in console.
  EM.on('foo', () => {
    // some other code...
  });

Testing

Tests are performed using mocha and expect library.

Building the documentation

You can use JSDoc comments found within the source code.

Minifying

You can grab minified versions of EventEmitter from /dist path after running gulp build.

Todo

  1. Add event's namespace:
  EM.on('foo.*', () => {
    // some code...
  });
  1. Add events through comma:
  EM.on('foo,bar,baz', () => {
    // some code...
  });
  1. Add method "onAny" for listening each event:
  EM.onAny(() => {
    // some code...
  });

Keywords

FAQs

Package last updated on 04 Apr 2016

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