Skip to content

Commit ad0a6e3

Browse files
authored
Merge branch 'main' into seriussoft-patch-2
2 parents 5f0d7d8 + c51a69f commit ad0a6e3

1 file changed

Lines changed: 18 additions & 15 deletions

File tree

‎classes/switchcontext.ts‎

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
1-
class SwitchContext {
1+
namespace switchblock {
2+
class SwitchContext {
23
private Cases: { Match: any, Handler: () => void, IsDefault?: boolean }[] = [];
34

45
addCase(match: any, handler: () => void): void {
5-
this.Cases.push({ Match: match, Handler: handler });
6+
this.Cases.push({ Match: match, Handler: handler });
67
}
78

89
addDefault(handler: () => void): void {
9-
this.Cases.push({ Match: null, Handler: handler, IsDefault: true });
10+
this.Cases.push({ Match: null, Handler: handler, IsDefault: true });
1011
}
1112

1213
execute(value: any): void {
13-
let matched = false;
14-
for (let cb of this.Cases) {
15-
if (cb.Match === value || cb.Match == value) {
16-
cb.Handler();
17-
matched = true;
18-
break;
19-
}
14+
let matched = false;
15+
for (let cb of this.Cases) {
16+
if (cb.Match === value || cb.Match == value) {
17+
cb.Handler();
18+
matched = true;
19+
break;
2020
}
21-
if (!matched) {
22-
let defaultCase = this.Cases.find(cb => cb.IsDefault);
23-
if (defaultCase) defaultCase.Handler();
24-
}
25-
this.Cases = []; // clear after execution
21+
}
22+
if (!matched) {
23+
let defaultCase = this.Cases.find(cb => cb.IsDefault);
24+
if (defaultCase) defaultCase.Handler();
25+
}
26+
this.Cases = []; // clear after execution
2627
}
28+
}
29+
2730
}

0 commit comments

Comments
 (0)