eventment

1.0.4 • Public • Published

Eventment

Node's event emitter for all engines with some extra features
With class

This implements the Node.js events module for environments that do not have it, like browsers.

events currently matches the Node.js API:
Event: 'newListener'
Event: 'removeListener'
emitter.addListener(eventName, listener)
emitter.emit(eventName[, ...args])
emitter.eventNames()
emitter.getMaxListeners()
emitter.listenerCount(eventName)
emitter.listeners(eventName)
emitter.off(eventName, listener)
emitter.on(eventName, listener)
emitter.once(eventName, listener)
emitter.prependListener(eventName, listener)
emitter.prependOnceListener(eventName, listener)
emitter.removeAllListeners([eventName])
emitter.removeListener(eventName, listener)
emitter.setMaxListeners(n)
emitter.rawListeners(eventName)

Install

npm i eventment

Usage

import Eventment from 'eventment';
const eventment = new Eventment();

eventment.on('message', event => {
  console.log('on message', event);
});

eventment.emit('message', { hello: 'world' });

API

See the Node.js EventEmitter docs. events currently matches the Node.js API.

Extra features

Multiple arguments

Allows issuing multiple arguments to listeners and emitters in a simple usage.

eventment.on('message', (event1, event2) => {
  console.log('on message', { event1, event2 });
});

eventment.emit('message', { some: 'message' }, { my: 'event' });

Multiple events

It is possible to multiple events in a listener and contrary.

It works for: .on, .once, .attach, .collect, .enable, .disable

eventment.on([ 'message', 'letter' ], data => {
  console.log('on message or letter', data);
});

eventment.emit('message', { my: 'message' });
eventment.emit('letter', { my: 'letter' });

// Or also
eventment.on('message', data => {
  console.log('on message', data);
});
eventment.on('letter', data => {
  console.log('on letter', data);
});

eventment.emit([ 'message', 'letter' ], { my: 'message', or: 'letter' });

Promisify .on and .once

In case there is no callback, the on and once functions return events in promises.

let count = 0;
setInterval(() => eventment.emit('plus', count++), 500);

const number = await eventment.once('plus');
console.log('once promise', { number });

for await (const number of eventment.on('plus')){
  console.log('on promise', { number });
  if(number >= 10) break;
}

Functions .attach and .collect

Accumulate events and capture later.

eventment.attach('thing');

eventment.emit('thing', '💻');
eventment.emit('thing', '⌚');
eventment.emit('thing', '🕹️');

const things = eventment.collect('thing');
// [ '💻', '⌚', '🕹️' ]

// With filter
eventment.attach('thing', thing => [ '💻', '⌨️', '🖱️' ].includes(thing));

eventment.emit('thing', '💻');
eventment.emit('thing', '⌨️');
eventment.emit('thing', '🖱️');
eventment.emit('thing', '💻');
eventment.emit('thing', '⌚');
eventment.emit('thing', '🕹️');

const things = eventment.collect('thing');
// [ '💻', '⌨️', '🖱️', '💻' ]

Functions .enable and .disable

eventment.on('fruit', fruit => console.log('on fruit', fruit));

eventment.emit('fruit', '🍏'); // listener working
eventment.emit('fruit', '🍊'); // listener working

eventment.disable('fruit');
eventment.emit('fruit', '🍇'); // do nothing
eventment.emit('fruit', '🍓'); // do nothing

eventment.enable('fruit');
eventment.emit('fruit', '🥑'); // listener working
eventment.emit('fruit', '🍍'); // listener working

Chained callbacks

Allows listeners with chained callbacks.

eventment.on('divergence', (num1, num2) => {
  console.log('on divergence', { num1, num2 });
  return num1 * num2;
}, (product, num1, num2) => {
  console.log('on product', { product, num1, num2 });
  return Math.pow(product, 2);
}, (square, num1, num2) => {
  console.log('on square', { square, num1, num2 });
});

let count1 = 0, count2 = 0;
eventment.emit('divergence', count1++, count2--);

Global listener *

Listen to all events with *

eventment.on('*', (event, data) => {
  console.log('on event', event, data);
});

eventment.emit('message', { my: 'event' });
eventment.emit('signal', { my: 'signal' });

Bind listener

Bind emitter in callback

const messageEmitterCallback = eventment.bind('message');
messageEmitterCallback({ my: 'data' });

eventment.on('message', data => {
  console.log('on event message', data);
});

License

MIT License © Fellipe Paiva

Package Sidebar

Install

npm i eventment

Weekly Downloads

0

Version

1.0.4

License

ISC

Unpacked Size

12.3 kB

Total Files

3

Last publish

Collaborators

  • fellipepaiva