-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitchblock.ts
More file actions
189 lines (158 loc) · 4.63 KB
/
switchblock.ts
File metadata and controls
189 lines (158 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//import { SwitchContext } from "./classes/switchcontext";
//import { SwitchManager } from "./classes/switchmanager";
//let switchManager = new SwitchManager();
namespace switchcase {
/**
* TODO: [ ] Check ToDo List in case.ts, classes/switchcontext.ts, and classes/switchmanager.ts
* TODO: [ ] Import SwitchManager and SwitchContext
* TODO: [ ] Hook-Up SwitchManager and SwitchContext
*/
let switchManager: SwitchManager = new switchcase.SwitchManager();
let currentSwitch: SwitchContext = switchManager.create("default");
let switchContext: SwitchContext = currentSwitch;
let currentSwitchName: string = "default";
console.debug(currentSwitchName);
/**
* Switch Block Container
*/
//% block="switch $name looking for $value"
//% blockId=switchcase_switch_block
//% group="Control"
//% weight=199
//% draggableParameters
export function switchBlock(name: string, value: any): void {
/*
let cxt = switchManager.get(name);
if (cxt) {
//nothing will go here, i merely wanted an easy way to handle what's in the else block
}
else {
cxt = switchManager.create(name);
cxt.setValue(value);
}
if (cxt.IsValueSet && myCurrent)
cxt.execute();
else
myCurrentSwitch.execute(value);
*/
let cxt = createSwitchFull(name, value);
if (!cxt)
return;
if (cxt.IsValueSet && cxt.caseCount() > 0)
cxt.execute();
else if (cxt.caseCount() > 0)
cxt.executeFromValue(value);
else
return;
}
/**
* Use this after all case blocks have been added to your switch.
* It will run the SwitchContext.execute() after pulling the switch by the correct name.
*/
//% block="switch $name :: execute on registered value against registered cases"
//% blockId=switchcase_switch_execute
//% group="Advanced Timing"
//% weight=190
//% draggableParameters
export function executeSwitch(name: string) {
let cxt = switchManager.create(name);
if (!cxt) {
//throw an error?
return;
}
if (!cxt.IsValueSet) {
// throw error?
return;
}
cxt.execute();
}
/**
* execute the switch [name] on [value].
* Use this when you want to run a registered switch and it's
* registered cases against a specific value
*/
//% block="switch $name :: execute on $value against registered cases"
//% blockId=switchcase_execute_switch_on_value
//% group="Advanced Timing"
//% weight=60
//% draggableParameters
export function executeSwitchOnValue(name: string, value: any) {
let cxt = switchManager.create(name);
if(!cxt) {
//throw error?
return;
}
cxt.executeFromValue(value);
}
/**
* Instantiate the SwitchContext, or retrieve it, but do NOT execute on it. Merely assign the value to it.
*/
//% block="create switch $name looking for $value"
//% blockId="switchcase_create_switch_full"
//% group="Advanced Timing"
//% blockSetVariable=switchContext
export function createSwitchFull(name: string, value: any): SwitchContext {
let cxt = createSwitch(name);
if (cxt) {
cxt.setValue(value);
switchContext = cxt;
}
return cxt;
}
/**
* Get or Create SwitchContext instance.
*/
//% block="create (or get) switch $name"
//% blockId="switchcase_create_or_get_switch"
//% group="Core"
//% blockSetVariable=switchContext
export function createSwitch(name: string) : SwitchContext {
let cxt = switchManager.get(name);
if(cxt) {
switchContext = cxt;
return cxt;
}
cxt = switchManager.create(name);
switchContext = cxt;
return cxt;
}
/**
* Set the Current Switch by name (from the local switchManager)
*/
//% block="set active switch name: $name"
//% blockId=switchcase_set_current_switch_name
//% group="Debugging & Testing"
//% blockSetVariable=currentSwitchName
export function setCurrentSwitch(name: string) {
currentSwitchName = name;
}
/**
* Case Block Container
* File: "case.ts"
* caseBlock(match: any, handler = () => void);
* block: "case $match do $handler"
* blockId: switchcase_case_block
* group: "Control"
* weight: 90
* draggableParameters
* draggableStatement=true
*/
/**
* Case Block Value
* File: "case.ts"
* caseBlockValue(match: any) : boolean;
* block: "case $match $switch"
* blockId: switchcase_case_block_value
* group:
*/
/**
* Default Case Block
*
* block: "default case"
* blockId: switchcase_default_case_block
* group: "Control"
* weight: 70
* draggableStatement=true
*
*/
}