npm

Join us for the "JavaScript Supply Chain Security" tech talk, presented by VP of Security, Adam Baldwin. 6/20 at 10am PT.Sign up here »

@studiomoniker/emitter

1.1.4 • Public • Published

emitter

Browser / Node compatible ES6 event emitter loosely based on Node's implementation with some added features:

  • Forward events from emitter to emitter: emitterA.on('event', emitterB)
  • Subscribe to all events: emitter.onAny((type, ...args) => doSomething(type, args))

Install

npm install @studiomoniker/emitter

Examples

Listening for and emitting events:

import Emitter from '@studiomoniker/emitter';
 
const emitter = new emitter();
emitter.on('foo', (message) => console.log(message));
emitter.emit('foo', 'bar');

Extending Emitter:

import Emitter from '@studiomoniker/emitter';
 
class Animal extends Emitter {
  makeSound(sound) {
    this.emit('a loud ' + sound);
  }
}
 
let animal = new Animal();
animal.on('sound', (sound) => {
  console.log(sound); // a loud growl
});
animal.makeSound('growl');

Adding and removing events:

import Emitter from '@studiomoniker/emitter';
 
function foo(message) {
  console.log('foo: ' + message);
}
 
const emitter = new Emitter();
emitter.on('foo', foo);
emitter.emit('foo', 'bar'); // Logs 'foo: bar'
emitter.off(foo);
emitter.emit('foo', 'bar'); // No longer logs 'foo: bar', because the listener was removed

Listen to all events:

import Emitter from '@studiomoniker/emitter';
 
function logAll(eventName, message) {
  console.log(`${eventName}${message}`);
}
 
const emitter = new Emitter();
emitter.onAny(foo);
emitter.emit('foo', 'bar'); // Logs 'foo: bar'
emitter.emit('poo', 'bar'); // Logs 'poo: bar'

Forward events:

import Emitter from '@studiomoniker/emitter';
 
function logAll(eventName, message) {
  console.log(`${eventName}${message}`);
}
 
const emitter = new Emitter();
const emitter2 = new Emitter();
 
// Forward 'message' events from emitter to emitter2:
emitter.on('message', emitter2);
emitter2.on('message', (message) => console.log('emitter2: ' + message));
 
emitter.emit('message', 'foo'); // Logs 'emitter2: foo'

install

npm i @studiomoniker/emitter

Downloadsweekly downloads

5

version

1.1.4

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability