Skip to content

Commit b327382

Browse files
authored
Updated codebase to support a syntax where you place your cases to generate the flow and then once the switch is placed, it will recognize the case blocks and run through them at execution time. Added a couple different paths to get the same result in case we need to setup the switch beforehand.
1 parent 971852e commit b327382

4 files changed

Lines changed: 319 additions & 206 deletions

File tree

caseblock.ts

Lines changed: 64 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
import { SwitchContext } from "./classes/switchcontext";
2-
import { SwitchManager } from "./classes/switchmanager";
1+
//import { SwitchContext } from "./classes/switchcontext";
2+
//import { SwitchManager } from "./classes/switchmanager";
33

44

55
namespace switchcase {
66

77

88
/**
9-
* TODO: [ ] Import SwitchManager and SwitchContext
10-
* TODO: [ ] Hook-Up SwitchManager and SwitchContext (and switchBlock?)
9+
* TODO: [X] Import SwitchManager and SwitchContext
10+
* TODO: [X] Hook-Up SwitchManager and SwitchContext (and switchBlock?)
1111
* TODO: [ ] Does caseBlockValue need to have the pointer decorator (to point to switchBlock)?
1212
* TODO: [ ] Can we put a test "game" inside this project?
1313
* TODO: [ ] If we do the above (test "game"), will it bloat people's games who import our switchcase extension? Options?
1414
* TODO: [ ] Assign Iconic FFVII Green to the Master Group and it's Children blocks
1515
* TODO: [ ] Create Master Group for each of these subgroups and blocks
16-
* TODO: [ ] Assign weight so that Master Group is higher up in the list
16+
* TODO: [X] Assign weight so that Master Group is higher up in the list
1717
* TODO: [ ] Assign [Advanced] to Master Group so it is only available for advanced users (maybe?)
1818
* TODO: [ ] Need Subgroups... How to implement?
1919
*/
@@ -45,46 +45,68 @@ namespace switchcase {
4545
*
4646
*/
4747

48+
4849

49-
/**
50-
* Case Block Container
51-
*/
52-
//% block="case $match do $handler"
53-
//% blockId=switchcase_case_block
54-
//% group="Control"
55-
//% weight=90
56-
//% draggableParameters
57-
//% draggableStatement=true
58-
export function caseBlock(match: any, handler: () => void): void {
59-
//Placeholder for CodeGen
60-
//currentSwitchCase.addCase(match, handler)
61-
}
50+
/**
51+
* Case Block Container
52+
*/
53+
//% block="case $match do $handler"
54+
//% blockId=switchcase_case_block
55+
//% group="Control"
56+
//% weight=90
57+
//% draggableParameters
58+
//% draggableStatement=true
59+
export function caseBlock(name: string, match: any, handler: () => void): void {
60+
//Placeholder for CodeGen
61+
//currentSwitchCase.addCase(match, handler)
62+
//let cxt = switchManager.get
6263

63-
/**
64-
* Case Block Value
65-
*/
66-
//% block="case $match"
67-
//% blockId=switchcase_case_block_value
68-
//% group="Control"
69-
//% weight=80
70-
//% draggableParameters
71-
export function caseBlockValue(match: any): boolean {
72-
//return match === switchValue || match == switchValue;
73-
74-
return false;
75-
64+
//this should try to get before creating, so unless that changes,
65+
// this code should suffice as uber simple as it is...
66+
let cxt = switchcase.createSwitch(name);
67+
if (cxt) {
68+
cxt.addCase(match, handler);
7669
}
70+
// else {
71+
// cxt = switchcase.getSwitch(name);
72+
// }
7773

78-
/**
79-
* Default-Case Block Container
80-
*/
81-
//% block="default case"
82-
//% blockId=switchcase_default_case_block
83-
//% group="Control"
84-
//% weight=70
85-
//% draggableStatement=true
86-
export function defaultCaseBlock(): void {
87-
//Placeholder for CodeGen
88-
}
74+
// cxt.addCase(match, handler);
75+
}
76+
77+
/**
78+
* Case Block Value
79+
* Outside of debugging, testing, or rare niche scenarios: this may not be useful
80+
*/
81+
//% block="[Advanced/Test/Debug] switch $name matches case $match?"
82+
//% blockId=switchcase_case_block_value
83+
//% group="Control"
84+
//% weight=80
85+
//% draggableParameters
86+
export function caseBlockValue(name: string, match: any): boolean {
87+
//return match === switchValue || match == switchValue;
88+
let cxt = switchcase.createSwitch(name);
89+
if (cxt && cxt.IsValueSet)
90+
return cxt.matches(match);
91+
92+
return false;
93+
94+
}
95+
96+
/**
97+
* Default-Case Block Container
98+
*/
99+
//% block="default case"
100+
//% blockId=switchcase_default_case_block
101+
//% group="Control"
102+
//% weight=70
103+
//% draggableStatement=true
104+
export function defaultCaseBlock(name: string, handler: () => void): void {
105+
106+
let cxt = switchcase.createSwitch(name);
107+
108+
if (cxt)
109+
cxt.addDefault(handler);
110+
}
89111

90112
}

classes/switchcontext.ts

Lines changed: 123 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,135 +1,152 @@
11

2+
namespace switchcase {
3+
export class SwitchContext {
4+
private Cases: { Match: any, Handler: () => void, IsDefault?: boolean }[] = [];
5+
private Value: any;
6+
public IsValueSet: boolean;
7+
8+
addCase(match: any, handler: () => void): void {
9+
this.Cases.push({ Match: match, Handler: handler });
10+
}
211

3-
export class SwitchContext {
4-
private Cases: { Match: any, Handler: () => void, IsDefault?: boolean }[] = [];
5-
6-
addCase(match: any, handler: () => void): void {
7-
this.Cases.push({ Match: match, Handler: handler });
8-
}
12+
addDefault(handler: () => void): void {
13+
this.Cases.push({ Match: null, Handler: handler, IsDefault: true });
14+
}
915

10-
addDefault(handler: () => void): void {
11-
this.Cases.push({ Match: null, Handler: handler, IsDefault: true });
12-
}
16+
execute(): void {
17+
this.executeFromValue(this.Value);
18+
}
1319

14-
execute(value: any): void {
15-
let matched = false;
16-
for (let cb of this.Cases) {
17-
if (cb.Match === value || cb.Match == value) {
18-
cb.Handler();
19-
matched = true;
20-
break;
20+
executeFromValue(value: any): void {
21+
let matched = false;
22+
for (let cb of this.Cases) {
23+
if (cb.Match === value || cb.Match == value) {
24+
cb.Handler();
25+
matched = true;
26+
break;
27+
}
2128
}
29+
if (!matched) {
30+
let defaultCase = this.Cases.find(cb => cb.IsDefault);
31+
if (defaultCase) defaultCase.Handler();
32+
}
33+
//for now, let's keep the cases present, and instead of clearing them (for hygene),
34+
// let's instead go with a route that only sets them up the first time, and every other
35+
// time, instead of adding a case, the user would simply detect it already exists
36+
// and use what's there
37+
//this.Cases = []; // clear after execution
2238
}
23-
if (!matched) {
24-
let defaultCase = this.Cases.find(cb => cb.IsDefault);
25-
if (defaultCase) defaultCase.Handler();
39+
40+
setValue(value: any): void {
41+
this.Value = value;
42+
if (this.Value)
43+
this.IsValueSet = this.Value;
44+
else
45+
this.IsValueSet = false;
2646
}
27-
//for now, let's keep the cases present, and instead of clearing them (for hygene),
28-
// let's instead go with a route that only sets them up the first time, and every other
29-
// time, instead of adding a case, the user would simply detect it already exists
30-
// and use what's there
31-
//this.Cases = []; // clear after execution
32-
}
3347

34-
clearCases(): void {
35-
this.Cases = [];
36-
}
48+
matches(match: any): boolean {
49+
return (this.Value == match || this.Value == match);
50+
}
3751

38-
/**
39-
* Remove Case (Non-Default Case)
40-
* and return both the success of removal and the removed case.
41-
*
42-
* Parameters:
43-
* @param {any} match
44-
* This is the match property found on your case. For DefaultCase, please see: {@look SwitchContext#removeDefaultCase}
45-
* @returns:
46-
* { removalSuccess: boolean, removed: { Match: any, Handler: () => void, IsDefault?: boolean } }
47-
*/
48-
removeCase(match: any): { RemovalSuccess: boolean, Removed: { Match: any, Handler: () => void, IsDefault?: boolean } } {
52+
clearCases(): void {
53+
this.Cases = [];
54+
}
55+
56+
/**
57+
* Remove Case (Non-Default Case)
58+
* and return both the success of removal and the removed case.
59+
*
60+
* Parameters:
61+
* @param {any} match
62+
* This is the match property found on your case. For DefaultCase, please see: {@look SwitchContext#removeDefaultCase}
63+
* @returns:
64+
* { removalSuccess: boolean, removed: { Match: any, Handler: () => void, IsDefault?: boolean } }
65+
*/
66+
removeCase(match: any): { RemovalSuccess: boolean, Removed: { Match: any, Handler: () => void, IsDefault?: boolean } } {
4967

50-
/*
51-
let toRemove: { Match: any, Handler: () => void, IsDefault?: boolean };
52-
let matched = false;
53-
54-
for (let cb in this.Cases) {
55-
if (cb.Match === match || cb.Match == match) {
56-
toRemove = cb;
57-
matched = true;
58-
break;
68+
/*
69+
let toRemove: { Match: any, Handler: () => void, IsDefault?: boolean };
70+
let matched = false;
71+
72+
for (let cb in this.Cases) {
73+
if (cb.Match === match || cb.Match == match) {
74+
toRemove = cb;
75+
matched = true;
76+
break;
77+
}
5978
}
60-
}
61-
*/
79+
*/
6280

63-
let toRemove: { Match: any, Handler: () => void, IsDefault?: boolean };
64-
let removeSuccess = false;
81+
let toRemove: { Match: any, Handler: () => void, IsDefault?: boolean };
82+
let removeSuccess = false;
6583

66-
toRemove =
67-
this.Cases.find(cb => cb.Match === match || cb.Match == match);
84+
toRemove =
85+
this.Cases.find(cb => cb.Match === match || cb.Match == match);
6886

69-
if (toRemove) removeSuccess = this.Cases.removeElement(toRemove);
70-
//if (removeSuccess) return toRemove;
87+
if (toRemove) removeSuccess = this.Cases.removeElement(toRemove);
88+
//if (removeSuccess) return toRemove;
7189

72-
return { RemovalSuccess: removeSuccess, Removed: toRemove };
73-
74-
/*
75-
if(match.IsDefault)
76-
77-
if (match.IsDefault) {
78-
toRemove = this.Cases.find(cb => cb.IsDefault);
79-
if (toRemove) {
80-
if (this.Cases.removeElement(defaultCase)) return defaultCase;
90+
return { RemovalSuccess: removeSuccess, Removed: toRemove };
91+
92+
/*
93+
if(match.IsDefault)
94+
95+
if (match.IsDefault) {
96+
toRemove = this.Cases.find(cb => cb.IsDefault);
97+
if (toRemove) {
98+
if (this.Cases.removeElement(defaultCase)) return defaultCase;
99+
}
81100
}
82-
}
83-
*/
101+
*/
84102

85-
}
103+
}
86104

87-
/**
88-
* Remove Default Case
89-
* and return both the success of removal and the removed case
90-
* To remove a specific NON-Default case, please see: {@look SwitchContext#removeCase}
91-
*
92-
* Parameters:
93-
* NONE
94-
* Returns:
95-
* @returns:
96-
* { RemovalSuccess: boolean, Removed: { Match: any, Handler: () => void, IsDefault?: boolean } }
97-
* The success of finding and removing the defaultCaseBlock AND the defaultCaseBlock that was removed.
98-
*/
99-
removeDefaultCase():
100-
{
105+
/**
106+
* Remove Default Case
107+
* and return both the success of removal and the removed case
108+
* To remove a specific NON-Default case, please see: {@see SwitchContext#removeCase}
109+
*
110+
* Parameters:
111+
* NONE
112+
* Returns:
113+
* @returns:
114+
* { RemovalSuccess: boolean, Removed: { Match: any, Handler: () => void, IsDefault?: boolean } }
115+
* The success of finding and removing the defaultCaseBlock AND the defaultCaseBlock that was removed.
116+
*/
117+
removeDefaultCase(): {
101118
RemovalSuccess: boolean
102119
, Removed: { Match: any, Handler: () => void, IsDefault?: boolean }
103120
} {
104121

105122
let defaultCaseBlock: { Match: any, Handler: () => void, IsDefault?: boolean };
106123
defaultCaseBlock = this.Cases.find(dcb => dcb.IsDefault);
107124

108-
let returnValue: { RemovalSuccess: boolean, Removed: { Match: any, Handler: () => void, IsDefault?: boolean } };
109-
if (defaultCaseBlock) {
110-
returnValue.RemovalSuccess = true;
111-
returnValue.Removed == defaultCaseBlock;
112-
}
113-
else {
114-
returnValue.RemovalSuccess = false;
115-
returnValue.Removed = defaultCaseBlock
116-
}
125+
let returnValue: { RemovalSuccess: boolean, Removed: { Match: any, Handler: () => void, IsDefault?: boolean } };
126+
if (defaultCaseBlock) {
127+
returnValue.RemovalSuccess = true;
128+
returnValue.Removed == defaultCaseBlock;
129+
}
130+
else {
131+
returnValue.RemovalSuccess = false;
132+
returnValue.Removed = defaultCaseBlock
133+
}
117134

118-
return returnValue;
119-
}
135+
return returnValue;
136+
}
120137

121-
caseCount(): number {
122-
return this.Cases.length;
123-
}
124-
/**
125-
* class SwitchContext {
126-
* [private] Cases { Match: any, handler: () => void, IsDefault?: boolean }[]
127-
* addCase(match: any, handler: () => void): void;
128-
* addDefault(handler: () => void): void;
129-
* execute(value: any): void;
130-
* caseCount(): number;
131-
* }
132-
*/
138+
caseCount(): number {
139+
return this.Cases.length;
140+
}
141+
/**
142+
* class SwitchContext {
143+
* [private] Cases { Match: any, handler: () => void, IsDefault?: boolean }[]
144+
* addCase(match: any, handler: () => void): void;
145+
* addDefault(handler: () => void): void;
146+
* execute(value: any): void;
147+
* caseCount(): number;
148+
* }
149+
*/
133150
}
134-
151+
}
135152

0 commit comments

Comments
 (0)