Skip to content

Instantly share code, notes, and snippets.

@dishuostec
Last active September 4, 2019 09:56
Show Gist options
  • Save dishuostec/056871cb645b0f9d92fb4eec98bfbb4d to your computer and use it in GitHub Desktop.
Save dishuostec/056871cb645b0f9d92fb4eec98bfbb4d to your computer and use it in GitHub Desktop.
Simple and tiny event emitter library for JavaScript, support wildcard event name.
/*
fork from https://github.com/ai/nanoevents
changes:
- support listening wildcard event name
- return handler result, use Promise.all() to wait task done.
*/
(
/**
* Interface for event subscription.
*
* @example
* var NanoEvents = require('nanoevents')
*
* class Ticker {
* constructor() {
* this.emitter = new NanoEvents()
* }
* on() {
* return this.emitter.on.apply(this.emitter, arguments)
* }
* tick() {
* this.emitter.emit('tick')
* }
* }
*
* @alias NanoEvents
* @class
*/
module.exports = function NanoEvents() {
/**
* Event names in keys and arrays with listeners in values.
* @type {object}
*
* @example
* Object.keys(ee.events)
*
* @alias NanoEvents#events
*/
this.events = {};
}
).prototype = {
/**
* Calls each of the listeners registered for a given event.
*
* @param {string} event The event name.
* @param {...*} arguments The arguments for listeners.
*
* @return {undefined}
*
* @example
* ee.emit('tick', tickType, tickDuration)
*
* @alias NanoEvents#emit
* @method
*/
emit: function emit(event) {
var args = [].slice.call(arguments, 1);
// Array.prototype.call() returns empty array if context is not array-like
return event
.split('.')
.reduce(function (h, e, i, arr) {
return h.concat(this.events[arr.slice(0, i).concat('*').join('.')] || []);
}.bind(this), this.events[event] || [])
.map(function (i) {
i.apply(null, args);
});
},
/**
* Add a listener for a given event.
*
* @param {string} event The event name.
* @param {function} cb The listener function.
*
* @return {function} Unbind listener from event.
*
* @example
* const unbind = ee.on('tick', (tickType, tickDuration) => {
* count += 1
* })
*
* disable () {
* unbind()
* }
*
* @alias NanoEvents#on
* @method
*/
on: function on(event, cb) {
if (process.env.NODE_ENV !== 'production' && typeof cb !== 'function') {
throw new Error('Listener must be a function');
}
(this.events[event] = this.events[event] || []).push(cb);
return function () {
this.events[event] = this.events[event].filter(function (i) {
return i !== cb;
});
}.bind(this);
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment