Git Product home page Git Product logo

ev-emitter's Introduction

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.

ev-emitter's People

Contributors

desandro avatar salimhb avatar tompazourek avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.