nemitter

0.0.10 • Public • Published

nemitter

Build Status

Node compatible ES2015 event emitter for all engines with more powerfull features.

Install

npm i nemitter

Require / Import

const EventEmitter = require('nemitter');

import EventEmitter from 'nemitter';

Examples

Add listener asynchronously

const myEmitter = new EventEmitter();
 
myEmitter.on('event', (a, b) => {
  setImmediate(() => {
    console.log('this happens asynchronously');
  });
});
myEmitter.emit('event', 'a', 'b');

AddListener / on / once

const myEmitter = new EventEmitter();
 
myEmitter.on('type', listener);
myEmitter.emit('type', arg1, arg2);

Listening for and emitting events:

const myEmitter = new EventEmitter();
 
myEmitter.on('foo', (message) => console.log(message));
myEmitter.emit('foo', 'bar');

Adding and removing events:

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

Listen to all events:

const emitter = new EventEmitter();
 
myEmitter.onAny((eventName, message) => console.log(`${eventName}${message}`));
myEmitter.emit('foo', 'bar'); // Logs 'foo: bar'
myEmitter.emit('poo', 'bar'); // Logs 'poo: bar'

Forward events:

const firstEmitter = new EventEmitter();
const secondEmitter = new EventEmitter();
 
// Forward 'message' events from firstEmitter to secondEmitter:
firstEmitter.on('message', secondEmitter);
secondEmitter.on('message', (message) => console.log('secondEmitter: ' + message));
 
firstEmitter.emit('message', 'foo'); // Logs 'secondEmitter: foo'

Package Sidebar

Install

npm i nemitter

Weekly Downloads

0

Version

0.0.10

License

MIT

Last publish

Collaborators

  • kflash123