-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds JSDoc support for EventManager #327
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,98 @@ | ||
// @ts-check | ||
const e = require("./errors"); | ||
const v = require("./validations"); | ||
|
||
/** | ||
* @typedef {import("..").ElectroEventListener} ElectroEventListener | ||
* @typedef {import("..").ElectroEvent} ElectroEvent | ||
*/ | ||
|
||
/** | ||
* @see {@link https://electrodb.dev/en/reference/events-logging/ | Events and Logging} for more information. | ||
* @class | ||
*/ | ||
class EventManager { | ||
/** | ||
* @type {Array<ElectroEventListener>} | ||
* */ | ||
#listeners; | ||
|
||
/** | ||
* Wraps the provided listener in order to safely invoke it. | ||
* @static | ||
* @template {(...args: any[]) => void} T | ||
* @param {T} [listener] - The listener to wrap. | ||
*/ | ||
static createSafeListener(listener) { | ||
if (listener === undefined) { | ||
return undefined; | ||
// no-op | ||
return () => {}; | ||
Comment on lines
+28
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could not get the filter to understand that undefined would have been removed even with type guards, so changed it to a no-op and removed the filter entirely. |
||
} | ||
|
||
if (!v.isFunction(listener)) { | ||
throw new e.ElectroError( | ||
e.ErrorCodes.InvalidListenerProvided, | ||
`Provided listener is not of type 'function'`, | ||
); | ||
} else { | ||
return (...params) => { | ||
try { | ||
listener(...params); | ||
} catch (err) { | ||
console.error(`Error invoking user supplied listener`, err); | ||
} | ||
}; | ||
} | ||
|
||
/** @param {Parameters<T>} args */ | ||
return (...args) => { | ||
try { | ||
listener(...args); | ||
} catch (err) { | ||
console.error(`Error invoking user supplied listener`, err); | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* @static | ||
* @template {(...args: any[]) => void} T | ||
* @param {T[]} [listeners=[]] | ||
*/ | ||
static normalizeListeners(listeners = []) { | ||
if (!Array.isArray(listeners)) { | ||
throw new e.ElectroError( | ||
e.ErrorCodes.InvalidListenerProvided, | ||
`Listeners must be provided as an array of functions`, | ||
); | ||
} | ||
return listeners | ||
.map((listener) => EventManager.createSafeListener(listener)) | ||
.filter((listener) => { | ||
switch (typeof listener) { | ||
case "function": | ||
return true; | ||
case "undefined": | ||
return false; | ||
default: | ||
throw new e.ElectroError( | ||
e.ErrorCodes.InvalidListenerProvided, | ||
`Provided listener is not of type 'function`, | ||
); | ||
} | ||
}); | ||
|
||
return listeners.map((listener) => | ||
EventManager.createSafeListener(listener), | ||
); | ||
} | ||
|
||
/** | ||
* @constructor | ||
* @param {Object} [config={}] | ||
* @param {Array<ElectroEventListener>} [config.listeners=[]] An array of listeners to be invoked after certain request lifecycles. | ||
*/ | ||
constructor({ listeners = [] } = {}) { | ||
this.listeners = EventManager.normalizeListeners(listeners); | ||
this.#listeners = EventManager.normalizeListeners(listeners); | ||
} | ||
|
||
/** | ||
* @param {ElectroEventListener | Array<ElectroEventListener>} [listeners=[]] | ||
*/ | ||
add(listeners = []) { | ||
if (!Array.isArray(listeners)) { | ||
listeners = [listeners]; | ||
} | ||
|
||
this.listeners = this.listeners.concat( | ||
this.#listeners = this.#listeners.concat( | ||
EventManager.normalizeListeners(listeners), | ||
); | ||
} | ||
|
||
/** | ||
* @param {ElectroEvent} event | ||
* @param {Array<ElectroEventListener>} [adHocListeners=[]] | ||
*/ | ||
trigger(event, adHocListeners = []) { | ||
const allListeners = [ | ||
...this.listeners, | ||
...this.#listeners, | ||
...EventManager.normalizeListeners(adHocListeners), | ||
]; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the class to use private properties