@momsfriendlydevco/eventer
Promise based alternative to the native Node EventEmitter
Last updated a month ago by hash-bang .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @momsfriendlydevco/eventer 
SYNC missed versions from official npm registry.

@momsfriendlydevco/eventer

Yet-another-implementation of the Node standard Event_Emitter library.

This module acts like a drop-in replacement for the standard require("events") library but is promise compatible, pipeable and easily extendible.

Eventer instances are both simultaniously an event emitter stand-in and a promisable while leaving the original context alone.

var myEmitter = eventer.extend({foo: 123})
	.on('thing', ()=> { /*...*/ }) // React to an event
	.emit('thing', val) // Emit an event
	.on('end', ()=> { /*...*/}) // React to a closing event
	.then(()=> { /*...*/ }) // ...or use promise syntax (context is the original object so `this.foo` is available)
	.catch(()=> { /*...*/ }) // ...likewise use promise rejections
	.resolve(val) // Trigger the closing event (resolves all 'thens' and `.on('end')`)
	.reject(val) // ...or reject


// Original object is still accessible:
myEmitter.foo //= 123

Why?

This module differs from the standard event emitter library in several ways:

  • emit() returns a promise which resolves with the combined Promise.all() result of all subscribers. It also rejects with the first subscriber throw.
  • emit() resolves all registered events in series, waiting for each unless one throws
  • emit.reduce() acts as a transform pipeline, where each callback can mutate the result before passing it on
  • eventer.extend(anObject) is nicer than the rather strange prototype inheritance system that EventEmitter recommends
  • Easily chainable
  • Eventer instances are "promise like" in that you can call .on('end', func) or .then(func) on and they will react accordingly. Likewise .on('error', func) and .catch(func)
  • Can proxy events from one event emitter to another
  • Ability to hook into the event call sequence via meta:preEmit + meta:postEmit
  • Debugging for the standard event_emitter object is terrible

Debugging

This module uses the debug NPM module for debugging. To enable set the environment variable to DEBUG=eventer.

For example:

DEBUG=eventer node myFile.js

If you want detailed eventer information (like what exact functions are calling queued), set DEBUG=eventer:detail.

API

emit(event, ...payload)

Emit an event and return a Promise which will only resolve if all the downstream subscribers resolve. Each subscriber is called in series and in the order they subscribed. Any return value from the event handler is discarded - use emit.reduce() if you want to work with pipelines. If any subscriber throws, the promise rejection payload is the first subscriber error. Note: This function will also emit meta:preEmit (as (eventName, ...args)) and meta:postEmit before and after each event.

eventer.extend()
	.on('greeting', name => console.log(`Hello ${name`)) //= "Hello Matt"
	.on('greeting', name => console.log(`Hi ${name`)) //= "Hi Matt"
	.emit('greeting', 'Matt')

emit.reduce(event, ...payload)

Works the same as emit() except that the return value (or eventual value via a Promise) will mutate the first argument of the next function. If any subscriber returns a non-undefined value, the next subscriber gets that result as the next first value. Returns final result of the last promise passed (see example below).

eventer.extend()
	.on('pipe', v => v++) // Given 0, returns 1
	.on('pipe', v => v++) // Given 1, returns 2
	.on('pipe', v => v++) // Given 2, returns 3
	.emit('pipe', 0) // Start the chain
	.then(v => ...) // Fnal result of 'emit' promise series, Given 3

on(events, function)

Subscribe to one event (if its a string) or multiple events (if an array of strings). The callback is treated as a Promise factory. Returns the chainable source object.

once(eventName, function)

Bind to one event binding exactly once. This is effectively the same as subscribing via on() then calling off() to unsubscribing the same function. Returns the chainable source object.

off(eventName, function)

Remove an event subscription. If a specific function is specified that lone function is removed, otherwise all bindings are reset. Returns the chainable source object.

listenerCount(eventName)

Return the number of listeners for a given event.

eventNames()

Return an array of strings representing each registered event.

extend(object, [options])

Glue the above methods to the supplied object without all the faff of extending object prototypes.

extendPromise(object, [options])

Same as extend() but also add promise functionality.

proxy(source, destination)

Proxy events from a source emitter into a destination. This in effect glues the destinations exposed methods to the source emitter.

promise()

Returns the raw promise object rather than its wrapped functions (then, catch + finally).

then(func)

Queue up a promisable action which will run on any .resolve(val) or .emit('end', val) call. The function is executed with the original object context.

catch(func)

Queue up a promisable action which will run on any .reject(val) or .emit('error', val) call. The function is executed with the original object context.

finally(func)

Queue up a promisable action which will run on any resolution or rejection. The function is executed with the original object context.

resolve(val)

Resolve the internal promise. This is syntactic sugar for .emit('end', val).

reject(val)

Reject the internal promise. This is syntactic sugar for .emit('end', val).

Current Tags

  • 1.5.1                                ...           latest (a month ago)

18 Versions

  • 1.5.1                                ...           a month ago
  • 1.5.0                                ...           3 months ago
  • 1.4.6                                ...           5 months ago
  • 1.4.4                                ...           6 months ago
  • 1.4.2                                ...           6 months ago
  • 1.4.0                                ...           9 months ago
  • 1.3.1                                ...           9 months ago
  • 1.3.0                                ...           9 months ago
  • 1.2.1                                ...           a year ago
  • 1.2.0                                ...           a year ago
  • 1.1.6                                ...           a year ago
  • 1.1.5                                ...           a year ago
  • 1.1.4                                ...           a year ago
  • 1.1.3                                ...           a year ago
  • 1.1.2                                ...           a year ago
  • 1.1.1                                ...           a year ago
  • 1.1.0                                ...           2 years ago
  • 1.0.0                                ...           2 years ago
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 20
Dependencies (0)
None
Dev Dependencies (2)

Copyright 2014 - 2017 © taobao.org |