0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00

Refactored events wrapper into class

- we have our own class in order to add the `hasRegisteredListener`
  function
- this commit refactors the implementation to use the class syntactic
  sugar, which means we get better editor autocomplete
- this shouldn't change the functionality
This commit is contained in:
Daniel Lockyer 2023-03-10 09:54:46 +01:00 committed by Daniel Lockyer
parent b93b38a8e8
commit 8df5b1a974

View file

@ -12,29 +12,22 @@
*/
const events = require('events');
const util = require('util');
let EventRegistry;
let EventRegistryInstance;
EventRegistry = function () {
events.EventEmitter.call(this);
};
class EventRegistry extends events.EventEmitter {
/**
* This is method is semi-hack to make sure listeners are only registered once
* during the lifetime of the process. And example problem it solves is
* registering duplicate listeners between Ghost instance reboots when running tests.
* @param {String} eventName
* @param {String} listenerName named function name registered as a listener for the event
* @returns {Boolean}
*/
hasRegisteredListener(eventName, listenerName) {
return !!(this.listeners(eventName).find(listener => (listener.name === listenerName)));
}
}
util.inherits(EventRegistry, events.EventEmitter);
const eventRegistryInstance = new EventRegistry();
eventRegistryInstance.setMaxListeners(100);
/**
* This is method is semi-hack to make sure listeners are only registered once
* during the lifetime of the process. And example problem it solves is
* registering duplicate listeners between Ghost instance reboots when running tests.
* @param {String} eventName
* @param {String} listenerName named function name registered as a listener for the event
* @returns {Boolean}
*/
EventRegistry.prototype.hasRegisteredListener = function (eventName, listenerName) {
return !!(this.listeners(eventName).find(listener => (listener.name === listenerName)));
};
EventRegistryInstance = new EventRegistry();
EventRegistryInstance.setMaxListeners(100);
module.exports = EventRegistryInstance;
module.exports = eventRegistryInstance;