Push update to workerware, this adds configuration support, timing, ability to make middleware names random and delete middleware by name, id, or event.
This commit is contained in:
parent
9d74ddf394
commit
4b75efbddd
1 changed files with 44 additions and 1 deletions
|
|
@ -1,14 +1,26 @@
|
|||
importScripts("/workerware/WWError.js");
|
||||
const dbg = console.log.bind(console, "[WorkerWare]");
|
||||
const time = console.time.bind(console, "[WorkerWare]");
|
||||
const timeEnd = console.timeEnd.bind(console, "[WorkerWare]");
|
||||
|
||||
/*
|
||||
OPTS:
|
||||
debug - Enables debug logging.
|
||||
randomNames - Generate random names for middlewares.
|
||||
timing - Logs timing for each middleware.
|
||||
*/
|
||||
|
||||
const defaultOpt = {
|
||||
debug: false,
|
||||
randomNames: false,
|
||||
timing: false
|
||||
};
|
||||
|
||||
// type middlewareManifest = {
|
||||
// function: Function,
|
||||
// name?: string,
|
||||
// events: string[], // Should be a union of validEvents.
|
||||
// configuration?: Object // Optional configuration for the middleware.
|
||||
// }
|
||||
|
||||
const validEvents = [
|
||||
|
|
@ -45,22 +57,48 @@ class WorkerWare {
|
|||
// This means the middleware is an anonymous function, or the user is silly and named their function "function"
|
||||
if (middleware.function.name == "function") middleware.name = crypto.randomUUID();
|
||||
if (!middleware.name) middleware.name = middleware.function.name;
|
||||
if (this._opt.randomNames) middleware.name = crypto.randomUUID();
|
||||
if (this._opt.debug) dbg("Adding middleware:", middleware.name);
|
||||
this._middlewares.push(middleware);
|
||||
}
|
||||
// Run all middlewares for the event type passed in.
|
||||
run(event) {
|
||||
const middlewares = this._middlewares;
|
||||
const returnList = [];
|
||||
let fn = async () => {
|
||||
for (let i = 0; i < middlewares.length; i++) {
|
||||
if (middlewares[i].events.includes(event.type)) {
|
||||
returnList.push(await middlewares[i].function(event));
|
||||
if (this._opt.timing) console.time(middlewares[i].name)
|
||||
// Add the configuration to the event object.
|
||||
event.middlewareConfig = middlewares[i].configuration || {};
|
||||
let res = await middlewares[i].function(event);
|
||||
if (this._opt.timing) console.timeEnd(middlewares[i].name)
|
||||
returnList.push(res);
|
||||
}
|
||||
}
|
||||
return returnList;
|
||||
};
|
||||
return fn;
|
||||
}
|
||||
deleteByID(middlewareID) {
|
||||
if (this._opt.debug) dbg("Deleting middleware:", middlewareID);
|
||||
this._middlewares = this._middlewares.filter((mw) => mw.name !== middlewareID);
|
||||
}
|
||||
deleteByEvent(middlewareEvent) {
|
||||
if (this._opt.debug) dbg("Deleting middleware by event:", middlewareEvent);
|
||||
this._middlewares = this._middlewares.filter((mw) => !mw.events.includes(middlewareEvent));
|
||||
}
|
||||
deleteByName(middlewareName) {
|
||||
if (this._opt.debug) dbg("Deleting middleware by name:", middlewareName);
|
||||
this._middlewares = this._middlewares.filter((mw) => mw.name !== middlewareName);
|
||||
}
|
||||
get() {
|
||||
return this._middlewares;
|
||||
}
|
||||
/*
|
||||
Run a single middleware by ID.
|
||||
This assumes that the user knows what they're doing, and is running the middleware on an event that it's supposed to run on.
|
||||
*/
|
||||
runMW(id, event) {
|
||||
const middlewares = this._middlewares;
|
||||
if (this._opt.debug) dbg("Running middleware:", id);
|
||||
|
|
@ -77,6 +115,11 @@ class WorkerWare {
|
|||
return {
|
||||
error: "middleware.function must be typeof function",
|
||||
};
|
||||
if (typeof middleware.configuration !== "object" && middleware.configuration !== undefined) {
|
||||
return {
|
||||
error: "middleware.configuration must be typeof object",
|
||||
};
|
||||
}
|
||||
if (!middleware.events)
|
||||
return {
|
||||
error: "middleware.events is required",
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue