Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions classes/switchcontext.ts
Original file line number Diff line number Diff line change
@@ -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
}
}