From 1a4f1543ac654660ca8c4a6520551ce09e413581 Mon Sep 17 00:00:00 2001 From: Volodymyr Kolesnykov Date: Sat, 13 Apr 2024 06:11:34 +0300 Subject: [PATCH] refactor: events module --- .eslintrc.json | 2 +- lib/events.js | 46 ++++++++++++++++++---------------------------- 2 files changed, 19 insertions(+), 29 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 6865388d0..534dd275b 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -4,7 +4,7 @@ "mocha": true }, "parserOptions": { - "ecmaVersion": 6 + "ecmaVersion": 11 }, "extends": "google", "rules": { diff --git a/lib/events.js b/lib/events.js index 4d4556297..009cd53ab 100644 --- a/lib/events.js +++ b/lib/events.js @@ -1,7 +1,6 @@ 'use strict'; // Modules. -const _ = require('lodash'); const EventEmitter = require('events').EventEmitter; const Log = require('./logger'); const Promise = require('./promise'); @@ -15,6 +14,7 @@ class AsyncEvents extends EventEmitter { super(); // Set things this.log = log; + /** @type {{ name: string, priority: number; fn: Function }[]} */ this._listeners = []; }; @@ -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 @@ -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; } @@ -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 @@ -73,36 +72,29 @@ 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); } @@ -110,9 +102,7 @@ class AsyncEvents extends EventEmitter { // 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); })