Skip to content

Commit

Permalink
refactor: events module
Browse files Browse the repository at this point in the history
  • Loading branch information
sjinks committed Apr 13, 2024
1 parent 1c45a75 commit 1a4f154
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"mocha": true
},
"parserOptions": {
"ecmaVersion": 6
"ecmaVersion": 11
},
"extends": "google",
"rules": {
Expand Down
46 changes: 18 additions & 28 deletions lib/events.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

// Modules.
const _ = require('lodash');
const EventEmitter = require('events').EventEmitter;
const Log = require('./logger');
const Promise = require('./promise');
Expand All @@ -15,6 +14,7 @@ class AsyncEvents extends EventEmitter {
super();
// Set things
this.log = log;
/** @type {{ name: string, priority: number; fn: Function }[]} */
this._listeners = [];
};

Expand All @@ -25,8 +25,8 @@ class AsyncEvents extends EventEmitter {
*
* @since 3.0.0
* @alias lando.events.on
* @param {String} name The name of the event
* @param {Integer} [priority=5] The priority the event should run in.
* @param {string} name The name of the event
* @param {number} [priority=5] The priority the event should run in.
* @param {Function} fn The function to call. Should get the args specified in the corresponding `emit` declaration.
* @return {Promise} A Promise
* @example
Expand All @@ -43,8 +43,7 @@ class AsyncEvents extends EventEmitter {
*/
on(name, priority, fn) {
// Handle no priority
// @todo: is there a way to get this working nicely via es6?
if (_.isUndefined(fn) && _.isFunction(priority)) {
if (typeof fn === 'undefined' && typeof priority === 'function') {
fn = priority;
priority = 5;
}
Expand All @@ -63,8 +62,8 @@ class AsyncEvents extends EventEmitter {
*
* @since 3.0.0
* @alias lando.events.emit
* @param {String} name The name of the event
* @param {...Any} [args] Options args to pass.
* @param {string} name The name of the event
* @param {...any} args Options args to pass.
* @return {Promise} A Promise
* @example
* // Emits a global event with a config arg
Expand All @@ -73,46 +72,37 @@ class AsyncEvents extends EventEmitter {
* // Emits an app event with a config arg
* return app.events.emit('sector001', config);
*/
emit(...args) {
/*
emit(name, ...args) {
/**
* Helper function that will always return a promise even if function is
* synchronous and doesn't return a promise.
* @todo: this is very old kbox code, can we update it a bit?
*
* @param {Function} fn
* @param {...any} args
* @return {Promise}
*/
const handle = (...args) => {
const fn = args.shift();
const handle = (fn, ...args) => {
const result = fn.apply(this, args);
return Promise.resolve(result);
};
// Save for later
const self = this;
// Grab name of event from first argument.
const name = args.shift();

// Grab priority sorted listeners for this event
const evnts = _(this._listeners)
// Filter by name
const fns = this._listeners
.filter(listener => listener.name === name)
// Sort by priority
.sortBy('priority')
// Return value
.value();

// Map to array of func
const fns = _.map(evnts, evnt => evnt.fn);
.sort((a, b) => a.priority - b.priority)
.map(evnt => evnt.fn);

// Log non engine events so we can keep things quiet
if (!_.includes(name, '-engine-')) {
if (!name.includes('-engine-')) {
this.log.debug('emitting event %s', name);
this.log.silly('event %s has %s listeners', name, fns.length);
}

// Make listener functions to a promise in series.
return Promise.each(fns, fn => {
// Clone function arguments.
const fnArgs = args.slice();
// Add listener function to front of arguments.
fnArgs.unshift(fn);
const fnArgs = [fn, ...args];
// Apply function that calls the listener function and returns a promise.
return handle.apply(self, fnArgs);
})
Expand Down

0 comments on commit 1a4f154

Please sign in to comment.