dyo-tools 0.3.0 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core/DTBunch.js +1 -1
- package/dist/core/DTBunch.js.map +1 -1
- package/dist/core/DTComponentPhysical.d.ts +2 -1
- package/dist/core/DTComponentPhysical.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/docs/assets/main.js +59 -59
- package/docs/assets/style.css +1414 -1414
- package/package.json +1 -1
- package/src/constants.ts +85 -85
- package/src/core/DTAction.ts +52 -52
- package/src/core/DTBunch.ts +467 -467
- package/src/core/DTComponent.ts +225 -225
- package/src/core/DTComponentPhysical.ts +54 -53
- package/src/core/DTComponentWithMeta.ts +65 -65
- package/src/core/DTElement.ts +102 -102
- package/src/core/DTError.ts +78 -78
- package/src/core/DTManager.ts +465 -465
- package/src/core/DTMaster.ts +318 -318
- package/src/core/DTModule.ts +90 -90
- package/src/index.ts +17 -17
- package/src/libs/DYOFinder.ts +175 -175
- package/src/libs/player/DTPlayer.element.ts +9 -9
- package/src/libs/player/DTPlayer.manager.ts +84 -84
- package/src/types/core.ts +169 -169
- package/src/types/index.ts +2 -2
- package/src/types/player.ts +6 -6
- package/test/core/DTBunch.spec.ts +13 -0
package/src/core/DTMaster.ts
CHANGED
|
@@ -1,318 +1,318 @@
|
|
|
1
|
-
import DYOToolsComponent from './DTComponent';
|
|
2
|
-
import DYOToolsError from './DTError';
|
|
3
|
-
import DYOToolsManager from './DTManager';
|
|
4
|
-
import DYOToolsAction from './DTAction';
|
|
5
|
-
import DYOToolsModule from './DTModule';
|
|
6
|
-
import { DTMasterToObject } from '../types';
|
|
7
|
-
|
|
8
|
-
export default class DYOToolsMaster extends DYOToolsComponent {
|
|
9
|
-
/**
|
|
10
|
-
* Defining component type to "master".
|
|
11
|
-
*/
|
|
12
|
-
protected _componentType: string = 'master';
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* DTManager instances handled by the Master.
|
|
16
|
-
*
|
|
17
|
-
* The property is an object with _id value of the Manager instance as key, and Manager instance as value.
|
|
18
|
-
* @protected
|
|
19
|
-
*/
|
|
20
|
-
protected _managers: Record<string, DYOToolsManager>;
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* DTAction instances handled by the Master.
|
|
24
|
-
*
|
|
25
|
-
* The property is an object with _id value of the Action instance as key, and Action instance as value.
|
|
26
|
-
* @protected
|
|
27
|
-
*/
|
|
28
|
-
protected _actions: Record<string, DYOToolsAction>;
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* DTModule instance handled by the Master.
|
|
32
|
-
*
|
|
33
|
-
* The property is an object with _id value of the Module instance as key, and Module instance as value.
|
|
34
|
-
* @protected
|
|
35
|
-
*/
|
|
36
|
-
protected _modules: Record<string, DYOToolsModule>;
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Applying the parent constructor, and initializing all properties to empty object.
|
|
40
|
-
*
|
|
41
|
-
* @param key
|
|
42
|
-
*/
|
|
43
|
-
constructor(key?: string) {
|
|
44
|
-
super(key);
|
|
45
|
-
|
|
46
|
-
this._managers = {};
|
|
47
|
-
this._actions = {};
|
|
48
|
-
this._modules = {};
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Add a DTManager **item** into _managers property.
|
|
53
|
-
*
|
|
54
|
-
* The adding process has the following specifications :
|
|
55
|
-
* * If the added manager has the same _id than existing manager, an error occurred (depending on **errors** option).
|
|
56
|
-
* * If the added manager has the same _key than existing manager, an error occurred (depending on **errors** option).
|
|
57
|
-
* * The Master instance becomes the new context of the added item.
|
|
58
|
-
*
|
|
59
|
-
* @param manager DYOToolsManager instance to add.
|
|
60
|
-
*/
|
|
61
|
-
addManager(manager: DYOToolsManager): void {
|
|
62
|
-
// Id conflict
|
|
63
|
-
if (Object.keys(this._managers).includes(manager.getId())) {
|
|
64
|
-
this.triggerError(new DYOToolsError(
|
|
65
|
-
'id_conflict',
|
|
66
|
-
'Manager with same id already exists in the Master instance',
|
|
67
|
-
this,
|
|
68
|
-
manager,
|
|
69
|
-
));
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// Key conflict
|
|
74
|
-
if (Object.values(this._managers).find((m) => m.getKey() === manager.getKey())) {
|
|
75
|
-
this.triggerError(new DYOToolsError(
|
|
76
|
-
'key_conflict',
|
|
77
|
-
'Manager with same key already exists in the Master instance',
|
|
78
|
-
this,
|
|
79
|
-
manager,
|
|
80
|
-
));
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
this._managers[manager.getId()] = manager;
|
|
85
|
-
manager.setContext<DYOToolsMaster>(this);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Return one DTManager instance handled by the Master.
|
|
90
|
-
* It could be selected by _id or _key property of the Manager.
|
|
91
|
-
*
|
|
92
|
-
* @param idOrKey String id or key to select.
|
|
93
|
-
* @returns DYOToolsManager selected Manager instance, or undefined if not found.
|
|
94
|
-
*/
|
|
95
|
-
getManager(idOrKey: string): DYOToolsManager | undefined {
|
|
96
|
-
let managerFound = this._managers[idOrKey];
|
|
97
|
-
|
|
98
|
-
if (!managerFound) {
|
|
99
|
-
managerFound = Object.values(this._managers).find((m) => m.getKey() === idOrKey);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return managerFound;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Remove a handled Manager from the Master instance.
|
|
107
|
-
*
|
|
108
|
-
* @param idOrKey String id or key to select.
|
|
109
|
-
*/
|
|
110
|
-
removeManager(idOrKey: string): void {
|
|
111
|
-
const manager = this.getManager(idOrKey);
|
|
112
|
-
|
|
113
|
-
if (manager) {
|
|
114
|
-
delete this._managers[manager.getId()];
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* Add a DTAction **item** into _actions property.
|
|
120
|
-
*
|
|
121
|
-
* The adding process has the following specifications :
|
|
122
|
-
* * If the added action has the same _id than existing action, an error occurred (depending on **errors** option).
|
|
123
|
-
* * If the added action has the same _key than existing action, an error occurred (depending on **errors** option).
|
|
124
|
-
* * The Master instance becomes the new context of the added item.
|
|
125
|
-
*
|
|
126
|
-
* @param action DYOToolsAction instance to add.
|
|
127
|
-
*/
|
|
128
|
-
addAction(action: DYOToolsAction): void {
|
|
129
|
-
// Id conflict
|
|
130
|
-
if (Object.keys(this._actions).includes(action.getId())) {
|
|
131
|
-
this.triggerError(new DYOToolsError(
|
|
132
|
-
'id_conflict',
|
|
133
|
-
'Action with same id already exists in the Master instance',
|
|
134
|
-
this,
|
|
135
|
-
action,
|
|
136
|
-
));
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
// Key conflict
|
|
141
|
-
if (Object.values(this._actions).find((m) => m.getKey() === action.getKey())) {
|
|
142
|
-
this.triggerError(new DYOToolsError(
|
|
143
|
-
'key_conflict',
|
|
144
|
-
'Action with same key already exists in the Master instance',
|
|
145
|
-
this,
|
|
146
|
-
action,
|
|
147
|
-
));
|
|
148
|
-
return;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
this._actions[action.getId()] = action;
|
|
152
|
-
action.setContext<DYOToolsMaster>(this);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Return one DTAction instance handled by the Master.
|
|
157
|
-
* It could be selected by _id or _key property of the Action.
|
|
158
|
-
*
|
|
159
|
-
* @param idOrKey String id or key to select.
|
|
160
|
-
* @returns DYOToolsAction selected Action instance, or undefined if not found.
|
|
161
|
-
*/
|
|
162
|
-
getAction(idOrKey: string): DYOToolsAction | undefined {
|
|
163
|
-
let actionFound = this._actions[idOrKey];
|
|
164
|
-
|
|
165
|
-
if (!actionFound) {
|
|
166
|
-
actionFound = Object.values(this._actions).find((a) => a.getKey() === idOrKey);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
return actionFound;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* Remove a handled Action from the Master instance.
|
|
174
|
-
*
|
|
175
|
-
* @param idOrKey String id or key to select.
|
|
176
|
-
*/
|
|
177
|
-
removeAction(idOrKey: string): void {
|
|
178
|
-
const action = this.getAction(idOrKey);
|
|
179
|
-
|
|
180
|
-
if (action) {
|
|
181
|
-
delete this._actions[action.getId()];
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* Trigger the **execute** process of an action handled by the Master instance.
|
|
187
|
-
* It could be selected by _id or _key property of the Action.
|
|
188
|
-
*
|
|
189
|
-
* @param idOrKey String id or key to select.
|
|
190
|
-
* @param payload Object payload for the action execution.
|
|
191
|
-
* @returns Promise action execution.
|
|
192
|
-
*/
|
|
193
|
-
async executeAction(idOrKey: string, payload: unknown): Promise<void> {
|
|
194
|
-
const action = this.getAction(idOrKey);
|
|
195
|
-
|
|
196
|
-
if (action) {
|
|
197
|
-
await this.getAction(idOrKey).execute(payload);
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* Add a DTModule **item** into _modules property.
|
|
203
|
-
*
|
|
204
|
-
* The adding process has the following specifications :
|
|
205
|
-
* * If the added module has the same _id than existing module, an error occurred (depending on **errors** option).
|
|
206
|
-
* * If the added module has the same _key than existing module, an error occurred (depending on **errors** option).
|
|
207
|
-
* * The Master instance becomes the new context of the added item.
|
|
208
|
-
*
|
|
209
|
-
* @param module DYOToolsModule instance to add.
|
|
210
|
-
*/
|
|
211
|
-
addModule(module: DYOToolsModule): void {
|
|
212
|
-
// Id conflict
|
|
213
|
-
if (Object.keys(this._modules).includes(module.getId())) {
|
|
214
|
-
this.triggerError(new DYOToolsError(
|
|
215
|
-
'id_conflict',
|
|
216
|
-
'Module with same id already exists in the Master instance',
|
|
217
|
-
this,
|
|
218
|
-
module,
|
|
219
|
-
));
|
|
220
|
-
return;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
// Key conflict
|
|
224
|
-
if (Object.values(this._modules).find((m) => m.getKey() === module.getKey())) {
|
|
225
|
-
this.triggerError(new DYOToolsError(
|
|
226
|
-
'key_conflict',
|
|
227
|
-
'Module with same key already exists in the Master instance',
|
|
228
|
-
this,
|
|
229
|
-
module,
|
|
230
|
-
));
|
|
231
|
-
return;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
this._modules[module.getId()] = module;
|
|
235
|
-
module.setContext<DYOToolsMaster>(this);
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
/**
|
|
239
|
-
* Return one DTModule instance handled by the Master.
|
|
240
|
-
* It could be selected by _id or _key property of the Module.
|
|
241
|
-
*
|
|
242
|
-
* @param idOrKey String id or key to select.
|
|
243
|
-
* @returns DYOToolsModule selected Module instance, or undefined if not found.
|
|
244
|
-
*/
|
|
245
|
-
getModule(idOrKey: string): DYOToolsModule | undefined {
|
|
246
|
-
let moduleFound = this._modules[idOrKey];
|
|
247
|
-
|
|
248
|
-
if (!moduleFound) {
|
|
249
|
-
moduleFound = Object.values(this._modules).find((m) => m.getKey() === idOrKey);
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
return moduleFound;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
/**
|
|
256
|
-
* Remove a handled Module from the Master instance.
|
|
257
|
-
*
|
|
258
|
-
* @param idOrKey String id or key to select.
|
|
259
|
-
*/
|
|
260
|
-
removeModule(idOrKey: string): void {
|
|
261
|
-
const module = this.getModule(idOrKey);
|
|
262
|
-
|
|
263
|
-
if (module) {
|
|
264
|
-
delete this._modules[module.getId()];
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
/**
|
|
269
|
-
* Return String representation of the Master instance.
|
|
270
|
-
*
|
|
271
|
-
* @returns String representation of the Master.
|
|
272
|
-
*/
|
|
273
|
-
toString(): string {
|
|
274
|
-
const managersLabel = `Managers: ${Object.keys(this._managers).length}`;
|
|
275
|
-
const actionsLabel = `Actions: ${Object.keys(this._actions).length}`;
|
|
276
|
-
const modulesLabel = `Modules: ${Object.keys(this._modules).length}`;
|
|
277
|
-
|
|
278
|
-
return `Component ${this._key} - Type: Master - ${managersLabel} - ${actionsLabel} - ${modulesLabel}`;
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
/**
|
|
282
|
-
* Return JSON Object representation of the Master instance.
|
|
283
|
-
*
|
|
284
|
-
* JSON Object returned has the following structure :
|
|
285
|
-
* * **id** : _id property of the Master.
|
|
286
|
-
* * **key** : _key property of the Master.
|
|
287
|
-
* * **type** : _componentType property of the Master.
|
|
288
|
-
* * **managers** : Array of JSON Object representation for each DTManager instance in _managers property of the Master.
|
|
289
|
-
* * **actions** : Array of JSON Object representation for each DTAction instance in _actions property of the Master.
|
|
290
|
-
* * **modules** : Array of JSON Object representation for each DTModule instance in _modules property of the Master.
|
|
291
|
-
*
|
|
292
|
-
* @returns JSON Object representation of the Master.
|
|
293
|
-
*/
|
|
294
|
-
toObject(): DTMasterToObject {
|
|
295
|
-
const toObject: DTMasterToObject = {
|
|
296
|
-
id: this._id,
|
|
297
|
-
key: this._key,
|
|
298
|
-
type: this._componentType,
|
|
299
|
-
managers: [],
|
|
300
|
-
actions: [],
|
|
301
|
-
modules: [],
|
|
302
|
-
};
|
|
303
|
-
|
|
304
|
-
if (Object.keys(this._managers).length) {
|
|
305
|
-
toObject.managers = Object.values(this._managers).map((obj) => obj.toObject());
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
if (Object.keys(this._actions).length) {
|
|
309
|
-
toObject.actions = Object.values(this._actions).map((obj) => obj.toObject());
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
if (Object.keys(this._modules).length) {
|
|
313
|
-
toObject.modules = Object.values(this._modules).map((obj) => obj.toObject());
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
return toObject;
|
|
317
|
-
}
|
|
318
|
-
}
|
|
1
|
+
import DYOToolsComponent from './DTComponent';
|
|
2
|
+
import DYOToolsError from './DTError';
|
|
3
|
+
import DYOToolsManager from './DTManager';
|
|
4
|
+
import DYOToolsAction from './DTAction';
|
|
5
|
+
import DYOToolsModule from './DTModule';
|
|
6
|
+
import { DTMasterToObject } from '../types';
|
|
7
|
+
|
|
8
|
+
export default class DYOToolsMaster extends DYOToolsComponent {
|
|
9
|
+
/**
|
|
10
|
+
* Defining component type to "master".
|
|
11
|
+
*/
|
|
12
|
+
protected _componentType: string = 'master';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* DTManager instances handled by the Master.
|
|
16
|
+
*
|
|
17
|
+
* The property is an object with _id value of the Manager instance as key, and Manager instance as value.
|
|
18
|
+
* @protected
|
|
19
|
+
*/
|
|
20
|
+
protected _managers: Record<string, DYOToolsManager>;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* DTAction instances handled by the Master.
|
|
24
|
+
*
|
|
25
|
+
* The property is an object with _id value of the Action instance as key, and Action instance as value.
|
|
26
|
+
* @protected
|
|
27
|
+
*/
|
|
28
|
+
protected _actions: Record<string, DYOToolsAction>;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* DTModule instance handled by the Master.
|
|
32
|
+
*
|
|
33
|
+
* The property is an object with _id value of the Module instance as key, and Module instance as value.
|
|
34
|
+
* @protected
|
|
35
|
+
*/
|
|
36
|
+
protected _modules: Record<string, DYOToolsModule>;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Applying the parent constructor, and initializing all properties to empty object.
|
|
40
|
+
*
|
|
41
|
+
* @param key
|
|
42
|
+
*/
|
|
43
|
+
constructor(key?: string) {
|
|
44
|
+
super(key);
|
|
45
|
+
|
|
46
|
+
this._managers = {};
|
|
47
|
+
this._actions = {};
|
|
48
|
+
this._modules = {};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Add a DTManager **item** into _managers property.
|
|
53
|
+
*
|
|
54
|
+
* The adding process has the following specifications :
|
|
55
|
+
* * If the added manager has the same _id than existing manager, an error occurred (depending on **errors** option).
|
|
56
|
+
* * If the added manager has the same _key than existing manager, an error occurred (depending on **errors** option).
|
|
57
|
+
* * The Master instance becomes the new context of the added item.
|
|
58
|
+
*
|
|
59
|
+
* @param manager DYOToolsManager instance to add.
|
|
60
|
+
*/
|
|
61
|
+
addManager(manager: DYOToolsManager): void {
|
|
62
|
+
// Id conflict
|
|
63
|
+
if (Object.keys(this._managers).includes(manager.getId())) {
|
|
64
|
+
this.triggerError(new DYOToolsError(
|
|
65
|
+
'id_conflict',
|
|
66
|
+
'Manager with same id already exists in the Master instance',
|
|
67
|
+
this,
|
|
68
|
+
manager,
|
|
69
|
+
));
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Key conflict
|
|
74
|
+
if (Object.values(this._managers).find((m) => m.getKey() === manager.getKey())) {
|
|
75
|
+
this.triggerError(new DYOToolsError(
|
|
76
|
+
'key_conflict',
|
|
77
|
+
'Manager with same key already exists in the Master instance',
|
|
78
|
+
this,
|
|
79
|
+
manager,
|
|
80
|
+
));
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
this._managers[manager.getId()] = manager;
|
|
85
|
+
manager.setContext<DYOToolsMaster>(this);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Return one DTManager instance handled by the Master.
|
|
90
|
+
* It could be selected by _id or _key property of the Manager.
|
|
91
|
+
*
|
|
92
|
+
* @param idOrKey String id or key to select.
|
|
93
|
+
* @returns DYOToolsManager selected Manager instance, or undefined if not found.
|
|
94
|
+
*/
|
|
95
|
+
getManager(idOrKey: string): DYOToolsManager | undefined {
|
|
96
|
+
let managerFound = this._managers[idOrKey];
|
|
97
|
+
|
|
98
|
+
if (!managerFound) {
|
|
99
|
+
managerFound = Object.values(this._managers).find((m) => m.getKey() === idOrKey);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return managerFound;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Remove a handled Manager from the Master instance.
|
|
107
|
+
*
|
|
108
|
+
* @param idOrKey String id or key to select.
|
|
109
|
+
*/
|
|
110
|
+
removeManager(idOrKey: string): void {
|
|
111
|
+
const manager = this.getManager(idOrKey);
|
|
112
|
+
|
|
113
|
+
if (manager) {
|
|
114
|
+
delete this._managers[manager.getId()];
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Add a DTAction **item** into _actions property.
|
|
120
|
+
*
|
|
121
|
+
* The adding process has the following specifications :
|
|
122
|
+
* * If the added action has the same _id than existing action, an error occurred (depending on **errors** option).
|
|
123
|
+
* * If the added action has the same _key than existing action, an error occurred (depending on **errors** option).
|
|
124
|
+
* * The Master instance becomes the new context of the added item.
|
|
125
|
+
*
|
|
126
|
+
* @param action DYOToolsAction instance to add.
|
|
127
|
+
*/
|
|
128
|
+
addAction(action: DYOToolsAction): void {
|
|
129
|
+
// Id conflict
|
|
130
|
+
if (Object.keys(this._actions).includes(action.getId())) {
|
|
131
|
+
this.triggerError(new DYOToolsError(
|
|
132
|
+
'id_conflict',
|
|
133
|
+
'Action with same id already exists in the Master instance',
|
|
134
|
+
this,
|
|
135
|
+
action,
|
|
136
|
+
));
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Key conflict
|
|
141
|
+
if (Object.values(this._actions).find((m) => m.getKey() === action.getKey())) {
|
|
142
|
+
this.triggerError(new DYOToolsError(
|
|
143
|
+
'key_conflict',
|
|
144
|
+
'Action with same key already exists in the Master instance',
|
|
145
|
+
this,
|
|
146
|
+
action,
|
|
147
|
+
));
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
this._actions[action.getId()] = action;
|
|
152
|
+
action.setContext<DYOToolsMaster>(this);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Return one DTAction instance handled by the Master.
|
|
157
|
+
* It could be selected by _id or _key property of the Action.
|
|
158
|
+
*
|
|
159
|
+
* @param idOrKey String id or key to select.
|
|
160
|
+
* @returns DYOToolsAction selected Action instance, or undefined if not found.
|
|
161
|
+
*/
|
|
162
|
+
getAction(idOrKey: string): DYOToolsAction | undefined {
|
|
163
|
+
let actionFound = this._actions[idOrKey];
|
|
164
|
+
|
|
165
|
+
if (!actionFound) {
|
|
166
|
+
actionFound = Object.values(this._actions).find((a) => a.getKey() === idOrKey);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return actionFound;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Remove a handled Action from the Master instance.
|
|
174
|
+
*
|
|
175
|
+
* @param idOrKey String id or key to select.
|
|
176
|
+
*/
|
|
177
|
+
removeAction(idOrKey: string): void {
|
|
178
|
+
const action = this.getAction(idOrKey);
|
|
179
|
+
|
|
180
|
+
if (action) {
|
|
181
|
+
delete this._actions[action.getId()];
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Trigger the **execute** process of an action handled by the Master instance.
|
|
187
|
+
* It could be selected by _id or _key property of the Action.
|
|
188
|
+
*
|
|
189
|
+
* @param idOrKey String id or key to select.
|
|
190
|
+
* @param payload Object payload for the action execution.
|
|
191
|
+
* @returns Promise action execution.
|
|
192
|
+
*/
|
|
193
|
+
async executeAction(idOrKey: string, payload: unknown): Promise<void> {
|
|
194
|
+
const action = this.getAction(idOrKey);
|
|
195
|
+
|
|
196
|
+
if (action) {
|
|
197
|
+
await this.getAction(idOrKey).execute(payload);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Add a DTModule **item** into _modules property.
|
|
203
|
+
*
|
|
204
|
+
* The adding process has the following specifications :
|
|
205
|
+
* * If the added module has the same _id than existing module, an error occurred (depending on **errors** option).
|
|
206
|
+
* * If the added module has the same _key than existing module, an error occurred (depending on **errors** option).
|
|
207
|
+
* * The Master instance becomes the new context of the added item.
|
|
208
|
+
*
|
|
209
|
+
* @param module DYOToolsModule instance to add.
|
|
210
|
+
*/
|
|
211
|
+
addModule(module: DYOToolsModule): void {
|
|
212
|
+
// Id conflict
|
|
213
|
+
if (Object.keys(this._modules).includes(module.getId())) {
|
|
214
|
+
this.triggerError(new DYOToolsError(
|
|
215
|
+
'id_conflict',
|
|
216
|
+
'Module with same id already exists in the Master instance',
|
|
217
|
+
this,
|
|
218
|
+
module,
|
|
219
|
+
));
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Key conflict
|
|
224
|
+
if (Object.values(this._modules).find((m) => m.getKey() === module.getKey())) {
|
|
225
|
+
this.triggerError(new DYOToolsError(
|
|
226
|
+
'key_conflict',
|
|
227
|
+
'Module with same key already exists in the Master instance',
|
|
228
|
+
this,
|
|
229
|
+
module,
|
|
230
|
+
));
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
this._modules[module.getId()] = module;
|
|
235
|
+
module.setContext<DYOToolsMaster>(this);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Return one DTModule instance handled by the Master.
|
|
240
|
+
* It could be selected by _id or _key property of the Module.
|
|
241
|
+
*
|
|
242
|
+
* @param idOrKey String id or key to select.
|
|
243
|
+
* @returns DYOToolsModule selected Module instance, or undefined if not found.
|
|
244
|
+
*/
|
|
245
|
+
getModule(idOrKey: string): DYOToolsModule | undefined {
|
|
246
|
+
let moduleFound = this._modules[idOrKey];
|
|
247
|
+
|
|
248
|
+
if (!moduleFound) {
|
|
249
|
+
moduleFound = Object.values(this._modules).find((m) => m.getKey() === idOrKey);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
return moduleFound;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Remove a handled Module from the Master instance.
|
|
257
|
+
*
|
|
258
|
+
* @param idOrKey String id or key to select.
|
|
259
|
+
*/
|
|
260
|
+
removeModule(idOrKey: string): void {
|
|
261
|
+
const module = this.getModule(idOrKey);
|
|
262
|
+
|
|
263
|
+
if (module) {
|
|
264
|
+
delete this._modules[module.getId()];
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Return String representation of the Master instance.
|
|
270
|
+
*
|
|
271
|
+
* @returns String representation of the Master.
|
|
272
|
+
*/
|
|
273
|
+
toString(): string {
|
|
274
|
+
const managersLabel = `Managers: ${Object.keys(this._managers).length}`;
|
|
275
|
+
const actionsLabel = `Actions: ${Object.keys(this._actions).length}`;
|
|
276
|
+
const modulesLabel = `Modules: ${Object.keys(this._modules).length}`;
|
|
277
|
+
|
|
278
|
+
return `Component ${this._key} - Type: Master - ${managersLabel} - ${actionsLabel} - ${modulesLabel}`;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Return JSON Object representation of the Master instance.
|
|
283
|
+
*
|
|
284
|
+
* JSON Object returned has the following structure :
|
|
285
|
+
* * **id** : _id property of the Master.
|
|
286
|
+
* * **key** : _key property of the Master.
|
|
287
|
+
* * **type** : _componentType property of the Master.
|
|
288
|
+
* * **managers** : Array of JSON Object representation for each DTManager instance in _managers property of the Master.
|
|
289
|
+
* * **actions** : Array of JSON Object representation for each DTAction instance in _actions property of the Master.
|
|
290
|
+
* * **modules** : Array of JSON Object representation for each DTModule instance in _modules property of the Master.
|
|
291
|
+
*
|
|
292
|
+
* @returns JSON Object representation of the Master.
|
|
293
|
+
*/
|
|
294
|
+
toObject(): DTMasterToObject {
|
|
295
|
+
const toObject: DTMasterToObject = {
|
|
296
|
+
id: this._id,
|
|
297
|
+
key: this._key,
|
|
298
|
+
type: this._componentType,
|
|
299
|
+
managers: [],
|
|
300
|
+
actions: [],
|
|
301
|
+
modules: [],
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
if (Object.keys(this._managers).length) {
|
|
305
|
+
toObject.managers = Object.values(this._managers).map((obj) => obj.toObject());
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
if (Object.keys(this._actions).length) {
|
|
309
|
+
toObject.actions = Object.values(this._actions).map((obj) => obj.toObject());
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
if (Object.keys(this._modules).length) {
|
|
313
|
+
toObject.modules = Object.values(this._modules).map((obj) => obj.toObject());
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
return toObject;
|
|
317
|
+
}
|
|
318
|
+
}
|