1.0.2 • Published 6 years ago

events-universal-patch v1.0.2

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

events-universal-patch

Catch all emitted events from an eventemitter with one listener.

Inofficial patch for pull request #54 of the node core module 'events'.

Features

For explenation the universal event is '*' (take a look at 'Usage').

  • Listeners listening to the event '*' getting triggered by every emitted event.
  • The handler function of a listener listening to '*' gets the actually emitted event as first argument.
  • .emit only returns true if at least one non-universal listener was triggered.
  • .emit('*' triggers each listeners listening to '*' only once. (with the actually emitted event as first argument)

Install

npm i events-universal-patch

Usage

const universalpatch = require('events-universal-patch');
const EventEmitter = universalpatch('*', require('events'));

Examples

emitter.on('foo', function() {
  console.log("foo");
  console.log(arguments);
})

emitter.on('*', function() {
  console.log("*");
  console.log(arguments);
})

console.log(emitter.emit('foo', 'bar1', 'bar2', 'bar3'));

//Output:
//foo
//[Arguments] { '0': 'bar1', '1': 'bar2', '2': 'bar3' }
//*
//[Arguments] { '0': 'foo', '1': 'bar1', '2': 'bar2', '3': 'bar3' }
//true
emitter.on('*', function() {
  console.log("*");
  console.log(arguments);
})

console.log(emitter.emit('foo', 'bar1', 'bar2', 'bar3'));

//Output:
//*
//[Arguments] { '0': 'foo', '1': 'bar1', '2': 'bar2', '3': 'bar3' }
//false
emitter.on('foo', function() {
  console.log("foo");
  console.log(arguments);
})

emitter.on('*', function() {
  console.log("*");
  console.log(arguments);
})

console.log(emitter.emit('foobar', 'bar1', 'bar2', 'bar3'));

//Output:
//*
//[Arguments] { '0': 'foobar', '1': 'bar1', '2': 'bar2', '3': 'bar3' }
//false
emitter.on('*', function() {
  console.log("*");
  console.log(arguments);
})

console.log(emitter.emit('*', 'bar1', 'bar2', 'bar3'));

//Output:
//*
//[Arguments] { '0': '*', '1': 'bar1', '2': 'bar2', '3': 'bar3' }
//true