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

smelly-event-emitter

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

smelly-event-emitter

A minimal and fast event-emitter for the browser.

  • 0.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
25
decreased by-44.44%
Maintainers
1
Weekly downloads
 
Created
Source

Smelly Event Emitter

A minimal and fast event-emitter for the browser. This can be used as a standalone class or attached to an existing objects prototype. See usage example below.

Installation

npm

npm install smelly-event-emitter --save

browser

<script src="/source/to/event-emitter.min.js"></script>

Usage

commonjs environment with inheritance (ES6)

import EventEmitter from 'event-emitter';

class Foo extends EventEmitter {
  constructor() {
    super();
    
    this.on('foo', this.bar);
    this.emit('foo');
  }
  
  bar() {
    console.log('foo event emitted!');
  }
}

commonjs environment with inheritance (ES5)

var EventEmitter = require('smelly-event-emitter');

function Foo() {
  EventEmitter.call(this); // super
  
  this.on('foo', this.bar);
}

Foo.prototype.bar = function() {
  console.log('foo event emitted');
}

Foo.prototype = Object.create(EventEmitter.prototype);
Foo.prototype.constructor = Foo;

var test = new Foo();
test.emit('foo');

browser environment with inheritance

function Foo() {
  EventEmitter.call(this); // super
  
  this.on('foo', this.bar);
}

Foo.prototype.bar = function() {
  console.log('foo event emitted');
}

Foo.prototype = Object.create(EventEmitter.prototype);
Foo.prototype.constructor = Foo;

var test = new Foo();
foo.emit('foo');

standalone (commonjs ES6)

import EventEmitter from 'event-emitter';

const ee = new EventEmitter();

class Foo {
  constructor() {
    ee.on('foo', this.bar);
    ee.emit('foo');
  }
  
  bar() {
    console.log('foo event called');
  }
}

API

.on(eventName, listener)

Attaches a listener to an event name. The listener can be removed by calling .off(eventName, listener). Note that if you pass an anonymous function as the lister, the .off() method will not be able to correctly remove that listener from the event listener list.

.off(eventName, listener)

Removes a listener from an event name. The listener will only correctly be removed if it is a named function (with the exception of if there is only a single listener attached).

.once(eventName, listener)

Attaches a listener to an event name that will only be fired once and then removed after being fired.

.emit(eventName, ...args)

Fire off all listeners for a specified event name and pass in any params to them as well. You can pass in any number of arguments you want e.g. this.emit('foo', 'bar', 'baz', 'boz') or this.emit('foo', { bar: 'baz' }). Note, a listener must have been attached with .on() or .once() before it can be fired with .emit().

.getMaxListeners()

Gets the specified maxListener count for the EventEmitter. Note this is used only to warn you against attaching too many listeners at once to a single event name.

.setMaxListeners()

Sets the maxListener count for the EventEmitter.

.getAllListeners(eventName = null)

Grabs all listeners for a specific event name, or if none is provided simply grabs the entire EventEmitter listeners object.

Keywords

FAQs

Package last updated on 10 Jul 2015

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