0.0.0 • Published 9 years ago

event-mux v0.0.0

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

event-mux

NPM version build status Test coverage Downloads js-standard-style

Duplex event multiplexer.

Installation

$ npm install event-mux

Usage

const EventEmitter = require('events').EventEmitter
const mux = require('event-mux')

const emitter = mux({
  1: new EventEmitter(),
  2: new EventEmitter()
})

emitter.on('1:beep', () => console.log('beep called'))
emitter.on('2:boop', () => console.log('boop called'))

emitter.emit('1:beep')
// => 'beep called'

emitter.emit('2:boop')
// => 'boop called'

API

emitter = mux(emitters)

Compose multiple event emitters by namespacing them by their keys. Returns an instance of EventEmitter2.

emitter.emit(event, data)

Emit an event.

emitter.on(event, cb)

Listen to a namespaced event.

Why?

Using a lot of EventEmitters in an application can lead to complex, interdependent code. By wrapping emitters together the pain is mitigated by being able to pass a single object around rather than pulling in each emitter manually. This reduces complexity from n^2 possible connections to n connections.

License

MIT