emitter20
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

emitter20

npm version Build Status

An event emitter that used to be 20 lines of code but is still quite small. No dependencies.

Install

$ npm install --save emitter20

Methods

  • on: (eventName: string, callback: Function) => void - Subscribe a callback to the given event type.
  • off: (eventName: string, callback: Function) => void - Remove a callback from the given event type.
  • trigger: (eventName: string, callback: Function) => void - Trigger an event, invoking all subscribers.
  • clear: (eventName?: string) => void - Remove all subscribers from the given event type, or all subscribers if no event type is specified.

Usage

var Emitter = require('emitter20')
 
var emitter = new Emitter()
 
emitter.on('karate-chop', function() {
  console.log('Haiaaaaaa!')
})
 
emitter.trigger('karate-chop') // Haiaaaaa!'

Pass arbitrary data to the event handler:

var emitter = new Emitter()
 
emitter.on('welcome', function(name) {
  console.log(`Welcome {name}!`)
})
 
emitter.trigger('welcome', 'bob') // Welcome bob!

Can be used as a mixin:

var assign = require('lodash.assign')
 
var obj = { a: 1, b: 2 }
assign(obj, new Emitter())
 
obj.on('karate-chop', function() {
  console.log('Haiaaaaaa!')
})
 
obj.trigger('karate-chop') // Haiaaaaa!'

License

ISC © Raine Revere

Dependencies (0)

    Dev Dependencies (3)

    Package Sidebar

    Install

    npm i emitter20

    Weekly Downloads

    284

    Version

    2.0.0

    License

    ISC

    Unpacked Size

    6.38 kB

    Total Files

    8

    Last publish

    Collaborators

    • raine