Socket
Socket
Sign inDemoInstall

ev-emitter

Package Overview
Dependencies
0
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    ev-emitter

lil' event emitter


Version published
Weekly downloads
366K
decreased by-6.7%
Maintainers
1
Install size
12.3 kB
Created
Weekly downloads
 

Readme

Source

EvEmitter

Lil' event emitter — add a little pub/sub

EvEmitter adds publish/subscribe pattern to a browser class. It's a smaller version of Olical/EventEmitter. That EventEmitter is full featured, widely used, and great. This EvEmitter has just the base event functionality to power the event API in libraries like Isotope, Flickity, Masonry, and imagesLoaded.

API

// class inheritence
class MyClass extends EvEmitter {}

// mixin prototype
Object.assign( MyClass.prototype, EvEmitter.prototype );

// single instance
let emitter = new EvEmitter();

on

Add an event listener.

emitter.on( eventName, listener )
  • eventName - String - name of the event
  • listener - Function

off

Remove an event listener.

emitter.off( eventName, listener )

once

Add an event listener to be triggered only once.

emitter.once( eventName, listener )

emitEvent

Trigger an event.

emitter.emitEvent( eventName, args )
  • eventName - String - name of the event
  • args - Array - arguments passed to listeners

allOff

Removes all event listeners.

emitter.allOff()

Code example

// create event emitter
var emitter = new EvEmitter();

// listeners
function hey( a, b, c ) {
  console.log( 'Hey', a, b, c )
}

function ho( a, b, c ) {
  console.log( 'Ho', a, b, c )
}

function letsGo( a, b, c ) {
  console.log( 'Lets go', a, b, c )
}

// bind listeners
emitter.on( 'rock', hey )
emitter.on( 'rock', ho )
// trigger letsGo once
emitter.once( 'rock', letsGo )

// emit event
emitter.emitEvent( 'rock', [ 1, 2, 3 ] )
// => 'Hey', 1, 2, 3
// => 'Ho', 1, 2, 3
// => 'Lets go', 1, 2, 3

// unbind
emitter.off( 'rock', ho )

emitter.emitEvent( 'rock', [ 4, 5, 6 ] )
// => 'Hey' 4, 5, 6

Browser support

EvEmitter v2 uses ES6 features like for...of loops and class definition as such it supports Chrome 49+, Firefox 45+, Safari 9+, and Edge 13+.

For older browser support, use EvEmitter v1.

License

EvEmitter is released under the MIT License. Have at it.

Keywords

FAQs

Last updated on 28 Dec 2021

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc