From fa6a22ce3827e073f2aeac6a9992f56bbcc0c33c Mon Sep 17 00:00:00 2001 From: Nate VanBuskirk Date: Mon, 8 Sep 2025 22:10:21 -0500 Subject: [PATCH] Create switchcontext.ts ... ... to provide an easy way for EACH switchBlock to track their own caseBlock garage and all the entities within. Before, we were going to use a shared space, akin to a shared parking lot for all the switchBlocks in the "mall", with each switchBlock being akin to a store. This would have made things awkward and extremely difficult to maintain or even test safely with more than one switchBlock in use in a project/game. So, we are tackling that here and now before we wind up with s'ghetti code or worse, circular dependencies, **shudders** This class will depend upon content, logic, and assumptions that are present in other files and classes/functions/etc. This commit isn't to complete this step, nor to even hook it up with the main logic stream - instead, we are just placing the bricks, mortar, tools, and workers about the project so they will be ready at the time this is actually put together. --- classes/switchcontext.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 classes/switchcontext.ts diff --git a/classes/switchcontext.ts b/classes/switchcontext.ts new file mode 100644 index 0000000..7dcfe27 --- /dev/null +++ b/classes/switchcontext.ts @@ -0,0 +1,27 @@ +class SwitchContext { + private Cases: { Match: any, Handler: () => void, IsDefault?: boolean }[] = []; + + addCase(match: any, handler: () => void): void { + this.Cases.push({ Match: match, Handler: handler }); + } + + addDefault(handler: () => void): void { + this.Cases.push({ Match: null, Handler: handler, IsDefault: true }); + } + + execute(value: any): void { + let matched = false; + for (let cb of this.Cases) { + if (cb.Match === value || cb.Match == value) { + cb.Handler(); + matched = true; + break; + } + } + if (!matched) { + let defaultCase = this.Cases.find(cb => cb.IsDefault); + if (defaultCase) defaultCase.Handler(); + } + this.Cases = []; // clear after execution + } +}