UNPKG

event-mux

Version:
69 lines (53 loc) 2.12 kB
# event-mux [![NPM version][npm-image]][npm-url] [![build status][travis-image]][travis-url] [![Test coverage][coveralls-image]][coveralls-url] [![Downloads][downloads-image]][downloads-url] [![js-standard-style][standard-image]][standard-url] Duplex event multiplexer. ## Installation ```bash $ npm install event-mux ``` ## Usage ```js 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`][ee2]. ### emitter.emit(event, data) Emit an event. ### emitter.on(event, cb) Listen to a namespaced event. ## Why? Using a lot of `EventEmitter`s 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](https://tldrlegal.com/license/mit-license) [npm-image]: https://img.shields.io/npm/v/event-mux.svg?style=flat-square [npm-url]: https://npmjs.org/package/event-mux [travis-image]: https://img.shields.io/travis/yoshuawuyts/event-mux.svg?style=flat-square [travis-url]: https://travis-ci.org/yoshuawuyts/event-mux [coveralls-image]: https://img.shields.io/coveralls/yoshuawuyts/event-mux.svg?style=flat-square [coveralls-url]: https://coveralls.io/r/yoshuawuyts/event-mux?branch=master [downloads-image]: http://img.shields.io/npm/dm/event-mux.svg?style=flat-square [downloads-url]: https://npmjs.org/package/event-mux [standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square [standard-url]: https://github.com/feross/standard [ee2]: https://github.com/asyncly/EventEmitter2