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:
wearrrrr 2024-06-18 23:19:32 -05:00
parent 9d74ddf394
commit 4b75efbddd

View file

@ -1,14 +1,26 @@
importScripts("/workerware/WWError.js"); importScripts("/workerware/WWError.js");
const dbg = console.log.bind(console, "[WorkerWare]"); 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 = { const defaultOpt = {
debug: false, debug: false,
randomNames: false,
timing: false
}; };
// type middlewareManifest = { // type middlewareManifest = {
// function: Function, // function: Function,
// name?: string, // name?: string,
// events: string[], // Should be a union of validEvents. // events: string[], // Should be a union of validEvents.
// configuration?: Object // Optional configuration for the middleware.
// } // }
const validEvents = [ 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" // 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.function.name == "function") middleware.name = crypto.randomUUID();
if (!middleware.name) middleware.name = middleware.function.name; 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); if (this._opt.debug) dbg("Adding middleware:", middleware.name);
this._middlewares.push(middleware); this._middlewares.push(middleware);
} }
// Run all middlewares for the event type passed in.
run(event) { run(event) {
const middlewares = this._middlewares; const middlewares = this._middlewares;
const returnList = []; const returnList = [];
let fn = async () => { let fn = async () => {
for (let i = 0; i < middlewares.length; i++) { for (let i = 0; i < middlewares.length; i++) {
if (middlewares[i].events.includes(event.type)) { 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 returnList;
}; };
return fn; 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) { runMW(id, event) {
const middlewares = this._middlewares; const middlewares = this._middlewares;
if (this._opt.debug) dbg("Running middleware:", id); if (this._opt.debug) dbg("Running middleware:", id);
@ -77,6 +115,11 @@ class WorkerWare {
return { return {
error: "middleware.function must be typeof function", 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) if (!middleware.events)
return { return {
error: "middleware.events is required", error: "middleware.events is required",