dyo-tools 0.3.0 → 0.4.0

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.
Files changed (208) hide show
  1. package/.c8rc.json +4 -4
  2. package/.eslintignore +1 -1
  3. package/.eslintrc.json +47 -47
  4. package/Makefile +34 -34
  5. package/dist/constants.d.ts +1 -0
  6. package/dist/constants.d.ts.map +1 -0
  7. package/dist/constants.js +24 -20
  8. package/dist/constants.js.map +1 -1
  9. package/dist/core/DTAction.d.ts +6 -4
  10. package/dist/core/DTAction.d.ts.map +1 -0
  11. package/dist/core/DTAction.js +3 -6
  12. package/dist/core/DTAction.js.map +1 -1
  13. package/dist/core/DTBunch.d.ts +8 -5
  14. package/dist/core/DTBunch.d.ts.map +1 -0
  15. package/dist/core/DTBunch.js +54 -20
  16. package/dist/core/DTBunch.js.map +1 -1
  17. package/dist/core/DTComponent.d.ts +13 -10
  18. package/dist/core/DTComponent.d.ts.map +1 -0
  19. package/dist/core/DTComponent.js +23 -10
  20. package/dist/core/DTComponent.js.map +1 -1
  21. package/dist/core/DTComponentPhysical.d.ts +11 -7
  22. package/dist/core/DTComponentPhysical.d.ts.map +1 -0
  23. package/dist/core/DTComponentPhysical.js +21 -6
  24. package/dist/core/DTComponentPhysical.js.map +1 -1
  25. package/dist/core/DTComponentWithMeta.d.ts +6 -3
  26. package/dist/core/DTComponentWithMeta.d.ts.map +1 -0
  27. package/dist/core/DTComponentWithMeta.js +21 -7
  28. package/dist/core/DTComponentWithMeta.js.map +1 -1
  29. package/dist/core/DTElement.d.ts +10 -8
  30. package/dist/core/DTElement.d.ts.map +1 -0
  31. package/dist/core/DTElement.js +8 -9
  32. package/dist/core/DTElement.js.map +1 -1
  33. package/dist/core/DTError.d.ts +8 -7
  34. package/dist/core/DTError.d.ts.map +1 -0
  35. package/dist/core/DTError.js +1 -5
  36. package/dist/core/DTError.js.map +1 -1
  37. package/dist/core/DTManager.d.ts +17 -14
  38. package/dist/core/DTManager.d.ts.map +1 -0
  39. package/dist/core/DTManager.js +84 -26
  40. package/dist/core/DTManager.js.map +1 -1
  41. package/dist/core/DTMaster.d.ts +19 -16
  42. package/dist/core/DTMaster.d.ts.map +1 -0
  43. package/dist/core/DTMaster.js +48 -31
  44. package/dist/core/DTMaster.js.map +1 -1
  45. package/dist/core/DTModule.d.ts +8 -5
  46. package/dist/core/DTModule.d.ts.map +1 -0
  47. package/dist/core/DTModule.js +6 -6
  48. package/dist/core/DTModule.js.map +1 -1
  49. package/dist/index.d.ts +1 -0
  50. package/dist/index.d.ts.map +1 -0
  51. package/dist/index.js +12 -28
  52. package/dist/index.js.map +1 -1
  53. package/dist/libs/DYOFinder.d.ts +1 -0
  54. package/dist/libs/DYOFinder.d.ts.map +1 -0
  55. package/dist/libs/DYOFinder.js +10 -14
  56. package/dist/libs/DYOFinder.js.map +1 -1
  57. package/dist/libs/dix/DIXModule.module.d.ts +8 -0
  58. package/dist/libs/dix/DIXModule.module.d.ts.map +1 -0
  59. package/dist/libs/dix/DIXModule.module.js +73 -0
  60. package/dist/libs/player/DTPlayer.element.d.ts +3 -2
  61. package/dist/libs/player/DTPlayer.element.d.ts.map +1 -0
  62. package/dist/libs/player/DTPlayer.element.js +2 -6
  63. package/dist/libs/player/DTPlayer.element.js.map +1 -1
  64. package/dist/libs/player/DTPlayer.manager.d.ts +8 -7
  65. package/dist/libs/player/DTPlayer.manager.d.ts.map +1 -0
  66. package/dist/libs/player/DTPlayer.manager.js +13 -20
  67. package/dist/libs/player/DTPlayer.manager.js.map +1 -1
  68. package/dist/tsconfig.dev.tsbuildinfo +1 -0
  69. package/dist/tsconfig.tsbuildinfo +1 -1
  70. package/dist/types/core.d.ts +40 -12
  71. package/dist/types/core.d.ts.map +1 -0
  72. package/dist/types/core.js +2 -6
  73. package/dist/types/core.js.map +1 -1
  74. package/dist/types/index.d.ts +1 -0
  75. package/dist/types/index.d.ts.map +1 -0
  76. package/dist/types/index.js +2 -19
  77. package/dist/types/index.js.map +1 -1
  78. package/dist/types/player.d.ts +1 -1
  79. package/dist/types/player.d.ts.map +1 -0
  80. package/dist/types/player.js +1 -3
  81. package/docs/assets/main.js +59 -59
  82. package/docs/assets/style.css +1414 -1414
  83. package/integration/data/components/DTHero.ts +42 -0
  84. package/integration/data/components/DTHeroManager.ts +11 -0
  85. package/integration/data/components/DTHeroPlayZone.ts +6 -0
  86. package/integration/data/components/DTHeroPlayerHand.ts +11 -0
  87. package/integration/data/components/index.ts +5 -0
  88. package/integration/data/in/heroIn.helper.ts +8 -0
  89. package/integration/data/in/playerIn.helper.ts +13 -0
  90. package/integration/data/out/heroOut.helper.ts +48 -0
  91. package/integration/endings/synchronisation.ending.ts +25 -0
  92. package/integration/scenes/drawCard.scene.ts +31 -0
  93. package/integration/scenes/empty.scene.ts +6 -0
  94. package/integration/scenes/playCard.scene.ts +23 -0
  95. package/integration/scenes/playerLeave.scene.ts +33 -0
  96. package/integration/stages/baseDix.stage.ts +137 -0
  97. package/integration/stages/syncDix.stage.ts +36 -0
  98. package/integration/tests/scenario1.spec.ts +55 -0
  99. package/integration/types/index.ts +24 -0
  100. package/jest-integration.config.ts +25 -0
  101. package/jest.config.ts +26 -0
  102. package/package.json +31 -19
  103. package/src/constants.ts +87 -85
  104. package/src/core/DTAction.ts +54 -52
  105. package/src/core/DTBunch.ts +531 -467
  106. package/src/core/DTComponent.ts +258 -225
  107. package/src/core/DTComponentPhysical.ts +88 -53
  108. package/src/core/DTComponentWithMeta.ts +98 -65
  109. package/src/core/DTElement.ts +111 -102
  110. package/src/core/DTError.ts +78 -78
  111. package/src/core/DTManager.ts +555 -465
  112. package/src/core/DTMaster.ts +366 -318
  113. package/src/core/DTModule.ts +96 -90
  114. package/src/libs/dix/DIXModule.module.ts +98 -0
  115. package/src/libs/player/DTPlayer.element.ts +9 -9
  116. package/src/libs/player/DTPlayer.manager.ts +70 -84
  117. package/src/types/core.ts +204 -169
  118. package/src/types/player.ts +5 -6
  119. package/test/core/DTAction.double.ts +12 -28
  120. package/test/core/DTAction.spec.ts +8 -16
  121. package/test/core/DTBunch.double.ts +49 -135
  122. package/test/core/DTBunch.spec.ts +163 -238
  123. package/test/core/DTComponent.double.ts +17 -2
  124. package/test/core/DTComponent.spec.ts +6 -4
  125. package/test/core/DTComponentPhysical.double.ts +29 -43
  126. package/test/core/DTComponentPhysical.spec.ts +22 -19
  127. package/test/core/DTComponentWithMeta.double.ts +38 -31
  128. package/test/core/DTComponentWithMeta.spec.ts +23 -18
  129. package/test/core/DTElement.double.ts +32 -53
  130. package/test/core/DTElement.spec.ts +15 -38
  131. package/test/core/DTError.double.ts +18 -53
  132. package/test/core/DTError.spec.ts +21 -32
  133. package/test/core/DTManager.double.ts +74 -141
  134. package/test/core/DTManager.spec.ts +289 -380
  135. package/test/core/DTMaster.double.ts +56 -80
  136. package/test/core/DTMaster.spec.ts +185 -232
  137. package/test/core/DTModule.double.ts +12 -25
  138. package/test/core/DTModule.spec.ts +14 -28
  139. package/test/core/copy.spec.ts +9 -30
  140. package/tsconfig.dev.json +5 -8
  141. package/tsconfig.json +5 -8
  142. package/cucumber-report.html +0 -48
  143. package/docs/assets/navigation.js +0 -1
  144. package/docs/classes/core_DTAction.DYOToolsAction.html +0 -89
  145. package/docs/classes/core_DTBunch.DYOToolsBunch.html +0 -254
  146. package/docs/classes/core_DTComponent.DYOToolsComponent.html +0 -76
  147. package/docs/classes/core_DTComponentPhysical.DYOToolsComponentPhysical.html +0 -110
  148. package/docs/classes/core_DTComponentWithMeta.DYOToolsComponentWithMeta.html +0 -96
  149. package/docs/classes/core_DTElement.DYOToolsElement.html +0 -135
  150. package/docs/classes/core_DTError.DYOToolsError.html +0 -37
  151. package/docs/classes/core_DTManager.DYOToolsManager.html +0 -237
  152. package/docs/classes/core_DTMaster.DYOToolsMaster.html +0 -150
  153. package/docs/classes/core_DTModule.DYOToolsModule.html +0 -92
  154. package/docs/classes/libs_DYOFinder.DYOFinder.html +0 -34
  155. package/docs/classes/libs_player_DTPlayer_element.DYOToolsPlayer.html +0 -134
  156. package/docs/classes/libs_player_DTPlayer_manager.DYOToolsPlayerManager.html +0 -243
  157. package/docs/enums/types_core.FilterOperatorType.html +0 -10
  158. package/docs/hierarchy.html +0 -1
  159. package/docs/interfaces/types_core.DTBunchFilters.html +0 -6
  160. package/docs/interfaces/types_core.DTBunchOptions.html +0 -19
  161. package/docs/interfaces/types_core.DTBunchToObject.html +0 -7
  162. package/docs/interfaces/types_core.DTComponentOptions.html +0 -5
  163. package/docs/interfaces/types_core.DTComponentToObject.html +0 -4
  164. package/docs/interfaces/types_core.DTElementToObject.html +0 -7
  165. package/docs/interfaces/types_core.DTManagerFilters.html +0 -6
  166. package/docs/interfaces/types_core.DTManagerOptions.html +0 -8
  167. package/docs/interfaces/types_core.DTManagerToObject.html +0 -5
  168. package/docs/interfaces/types_core.DTMasterToObject.html +0 -8
  169. package/docs/interfaces/types_core.DTModuleToObject.html +0 -6
  170. package/docs/interfaces/types_core.DYOFinderConfigurationPropDefault.html +0 -4
  171. package/docs/interfaces/types_core.DYOFinderConfigurationPropObjectSearch.html +0 -4
  172. package/docs/interfaces/types_core.DYOFinderFilterOperatorAdvanced.html +0 -5
  173. package/docs/interfaces/types_core.DYOFinderFilterOperatorBase.html +0 -5
  174. package/docs/interfaces/types_player.DTPlayerManagerSimpleConfiguration.html +0 -3
  175. package/docs/modules/constants.html +0 -6
  176. package/docs/modules/core_DTAction.html +0 -2
  177. package/docs/modules/core_DTBunch.html +0 -2
  178. package/docs/modules/core_DTComponent.html +0 -2
  179. package/docs/modules/core_DTComponentPhysical.html +0 -2
  180. package/docs/modules/core_DTComponentWithMeta.html +0 -2
  181. package/docs/modules/core_DTElement.html +0 -2
  182. package/docs/modules/core_DTError.html +0 -2
  183. package/docs/modules/core_DTManager.html +0 -2
  184. package/docs/modules/core_DTMaster.html +0 -2
  185. package/docs/modules/core_DTModule.html +0 -2
  186. package/docs/modules/libs_DYOFinder.html +0 -2
  187. package/docs/modules/libs_player_DTPlayer_element.html +0 -2
  188. package/docs/modules/libs_player_DTPlayer_manager.html +0 -2
  189. package/docs/modules/types.html +0 -29
  190. package/docs/modules/types_core.html +0 -28
  191. package/docs/modules/types_player.html +0 -2
  192. package/docs/types/types_core.DTAcceptedMetaData.html +0 -2
  193. package/docs/types/types_core.DTManagerItemType.html +0 -1
  194. package/docs/types/types_core.DTManagerItemsType.html +0 -2
  195. package/docs/types/types_core.DYOFinderComponentType.html +0 -1
  196. package/docs/types/types_core.DYOFinderConfiguration.html +0 -2
  197. package/docs/types/types_core.DYOFinderConfigurationProp.html +0 -1
  198. package/docs/types/types_core.DYOFinderFilterOperator.html +0 -1
  199. package/docs/types/types_core.DYOFinderFilterOperatorArgument.html +0 -1
  200. package/docs/types/types_core.DYOFinderFilters.html +0 -1
  201. package/docs/types/types_core.StandardPrimitiveType.html +0 -2
  202. package/docs/types/types_core.StandardPrimitiveTypeWithArray.html +0 -1
  203. package/docs/variables/constants.bunchDefaultOptions.html +0 -2
  204. package/docs/variables/constants.componentBunchDefaultFinderConfiguration.html +0 -1
  205. package/docs/variables/constants.componentManagerDefaultFinderConfiguration.html +0 -1
  206. package/docs/variables/constants.componentPhysicalDefaultFinderConfiguration.html +0 -1
  207. package/docs/variables/constants.managerDefaultOptions.html +0 -2
  208. package/jest.config.js +0 -6
@@ -1,318 +1,366 @@
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 DTComponent from './DTComponent';
2
+ import DTError from './DTError';
3
+ import DTManager from './DTManager';
4
+ import DTAction from './DTAction';
5
+ import DTModule from './DTModule';
6
+ import { DIXObject, DTComponentOptions, DTMasterToObject } from '../types';
7
+ import DTDIXModule from '../libs/dix/DIXModule.module';
8
+
9
+ export default class DTMaster extends DTComponent {
10
+ /**
11
+ * Defining component type to "master".
12
+ */
13
+ protected _componentType: string = 'master';
14
+
15
+ /**
16
+ * DTManager instances handled by the Master.
17
+ *
18
+ * The property is an object with _id value of the Manager instance as key, and Manager instance as value.
19
+ * @protected
20
+ */
21
+ protected _managers: Record<string, DTManager>;
22
+
23
+ /**
24
+ * DTAction instances handled by the Master.
25
+ *
26
+ * The property is an object with _id value of the Action instance as key, and Action instance as value.
27
+ * @protected
28
+ */
29
+ protected _actions: Record<string, DTAction>;
30
+
31
+ /**
32
+ * DTModule instance handled by the Master.
33
+ *
34
+ * The property is an object with _id value of the Module instance as key, and Module instance as value.
35
+ * @protected
36
+ */
37
+ protected _modules: Record<string, DTModule>;
38
+
39
+ /**
40
+ * Applying the parent constructor, and initializing all properties to empty object.
41
+ *
42
+ * @param key
43
+ * @param options
44
+ */
45
+ constructor(key?: string, options: Partial<DTComponentOptions> = {}) {
46
+ super(key, options);
47
+
48
+ this._managers = {};
49
+ this._actions = {};
50
+ this._modules = {};
51
+ }
52
+
53
+ /**
54
+ * Add a DTManager **item** into _managers property.
55
+ *
56
+ * The adding process has the following specifications :
57
+ * * If the added manager has the same _id than existing manager, an error occurred (depending on **errors** option).
58
+ * * If the added manager has the same _key than existing manager, an error occurred (depending on **errors** option).
59
+ * * The Master instance becomes the new context of the added item.
60
+ *
61
+ * @param manager DYOToolsManager instance to add.
62
+ */
63
+ addManager(manager: DTManager): void {
64
+ // Id conflict
65
+ if (Object.keys(this._managers).includes(manager.getId())) {
66
+ this.triggerError(new DTError(
67
+ 'id_conflict',
68
+ 'Manager with same id already exists in the Master instance',
69
+ this,
70
+ manager,
71
+ ));
72
+ return;
73
+ }
74
+
75
+ // Key conflict
76
+ if (Object.values(this._managers).find((m) => m.getKey() === manager.getKey())) {
77
+ this.triggerError(new DTError(
78
+ 'key_conflict',
79
+ 'Manager with same key already exists in the Master instance',
80
+ this,
81
+ manager,
82
+ ));
83
+ return;
84
+ }
85
+
86
+ this._managers[manager.getId()] = manager;
87
+ manager.setContext<DTMaster>(this);
88
+ }
89
+
90
+ /**
91
+ * Return one DTManager instance handled by the Master.
92
+ * It could be selected by _id or _key property of the Manager.
93
+ *
94
+ * @param idOrKey String id or key to select.
95
+ * @returns DYOToolsManager selected Manager instance, or undefined if not found.
96
+ */
97
+ getManager(idOrKey: string): DTManager | undefined {
98
+ let managerFound = this._managers[idOrKey];
99
+
100
+ if (!managerFound) {
101
+ managerFound = Object.values(this._managers).find((m) => m.getKey() === idOrKey);
102
+ }
103
+
104
+ return managerFound;
105
+ }
106
+
107
+ /**
108
+ * Remove a handled Manager from the Master instance.
109
+ *
110
+ * @param idOrKey String id or key to select.
111
+ */
112
+ removeManager(idOrKey: string): void {
113
+ const manager = this.getManager(idOrKey);
114
+
115
+ if (manager) {
116
+ delete this._managers[manager.getId()];
117
+ }
118
+ }
119
+
120
+ /**
121
+ * Add a DTAction **item** into _actions property.
122
+ *
123
+ * The adding process has the following specifications :
124
+ * * If the added action has the same _id than existing action, an error occurred (depending on **errors** option).
125
+ * * If the added action has the same _key than existing action, an error occurred (depending on **errors** option).
126
+ * * The Master instance becomes the new context of the added item.
127
+ *
128
+ * @param action DYOToolsAction instance to add.
129
+ */
130
+ addAction(action: DTAction): void {
131
+ // Id conflict
132
+ if (Object.keys(this._actions).includes(action.getId())) {
133
+ this.triggerError(new DTError(
134
+ 'id_conflict',
135
+ 'Action with same id already exists in the Master instance',
136
+ this,
137
+ action,
138
+ ));
139
+ return;
140
+ }
141
+
142
+ // Key conflict
143
+ if (Object.values(this._actions).find((m) => m.getKey() === action.getKey())) {
144
+ this.triggerError(new DTError(
145
+ 'key_conflict',
146
+ 'Action with same key already exists in the Master instance',
147
+ this,
148
+ action,
149
+ ));
150
+ return;
151
+ }
152
+
153
+ this._actions[action.getId()] = action;
154
+ action.setContext<DTMaster>(this);
155
+ }
156
+
157
+ /**
158
+ * Return one DTAction instance handled by the Master.
159
+ * It could be selected by _id or _key property of the Action.
160
+ *
161
+ * @param idOrKey String id or key to select.
162
+ * @returns DYOToolsAction selected Action instance, or undefined if not found.
163
+ */
164
+ getAction(idOrKey: string): DTAction | undefined {
165
+ let actionFound = this._actions[idOrKey];
166
+
167
+ if (!actionFound) {
168
+ actionFound = Object.values(this._actions).find((a) => a.getKey() === idOrKey);
169
+ }
170
+
171
+ return actionFound;
172
+ }
173
+
174
+ /**
175
+ * Remove a handled Action from the Master instance.
176
+ *
177
+ * @param idOrKey String id or key to select.
178
+ */
179
+ removeAction(idOrKey: string): void {
180
+ const action = this.getAction(idOrKey);
181
+
182
+ if (action) {
183
+ delete this._actions[action.getId()];
184
+ }
185
+ }
186
+
187
+ /**
188
+ * Trigger the **execute** process of an action handled by the Master instance.
189
+ * It could be selected by _id or _key property of the Action.
190
+ *
191
+ * @param idOrKey String id or key to select.
192
+ * @param payload Object payload for the action execution.
193
+ * @returns Promise action execution.
194
+ */
195
+ async executeAction(idOrKey: string, payload: unknown): Promise<void> {
196
+ const action = this.getAction(idOrKey);
197
+
198
+ if (action) {
199
+ await this.getAction(idOrKey).execute(payload);
200
+ }
201
+ }
202
+
203
+ /**
204
+ * Add a DTModule **item** into _modules property.
205
+ *
206
+ * The adding process has the following specifications :
207
+ * * If the added module has the same _id than existing module, an error occurred (depending on **errors** option).
208
+ * * If the added module has the same _key than existing module, an error occurred (depending on **errors** option).
209
+ * * The Master instance becomes the new context of the added item.
210
+ *
211
+ * @param module DYOToolsModule instance to add.
212
+ */
213
+ addModule(module: DTModule): void {
214
+ // Id conflict
215
+ if (Object.keys(this._modules).includes(module.getId())) {
216
+ this.triggerError(new DTError(
217
+ 'id_conflict',
218
+ 'Module with same id already exists in the Master instance',
219
+ this,
220
+ module,
221
+ ));
222
+ return;
223
+ }
224
+
225
+ // Key conflict
226
+ if (Object.values(this._modules).find((m) => m.getKey() === module.getKey())) {
227
+ this.triggerError(new DTError(
228
+ 'key_conflict',
229
+ 'Module with same key already exists in the Master instance',
230
+ this,
231
+ module,
232
+ ));
233
+ return;
234
+ }
235
+
236
+ this._modules[module.getId()] = module;
237
+ module.setContext<DTMaster>(this);
238
+ }
239
+
240
+ /**
241
+ * Return one DTModule instance handled by the Master.
242
+ * It could be selected by _id or _key property of the Module.
243
+ *
244
+ * @param idOrKey String id or key to select.
245
+ * @returns DYOToolsModule selected Module instance, or undefined if not found.
246
+ */
247
+ getModule(idOrKey: string): DTModule | undefined {
248
+ let moduleFound = this._modules[idOrKey];
249
+
250
+ if (!moduleFound) {
251
+ moduleFound = Object.values(this._modules).find((m) => m.getKey() === idOrKey);
252
+ }
253
+
254
+ return moduleFound;
255
+ }
256
+
257
+ /**
258
+ * Remove a handled Module from the Master instance.
259
+ *
260
+ * @param idOrKey String id or key to select.
261
+ */
262
+ removeModule(idOrKey: string): void {
263
+ const module = this.getModule(idOrKey);
264
+
265
+ if (module) {
266
+ delete this._modules[module.getId()];
267
+ }
268
+ }
269
+
270
+ /**
271
+ * Return String representation of the Master instance.
272
+ *
273
+ * @returns String representation of the Master.
274
+ */
275
+ toString(): string {
276
+ const managersLabel = `Managers: ${Object.keys(this._managers).length}`;
277
+ const actionsLabel = `Actions: ${Object.keys(this._actions).length}`;
278
+ const modulesLabel = `Modules: ${Object.keys(this._modules).length}`;
279
+
280
+ return `Component ${this._key} - Type: Master - ${managersLabel} - ${actionsLabel} - ${modulesLabel}`;
281
+ }
282
+
283
+ /**
284
+ * Return JSON Object representation of the Master instance.
285
+ *
286
+ * JSON Object returned has the following structure :
287
+ * * **id** : _id property of the Master.
288
+ * * **key** : _key property of the Master.
289
+ * * **type** : _componentType property of the Master.
290
+ * * **managers** : Array of JSON Object representation for each DTManager instance in _managers property of the Master.
291
+ * * **actions** : Array of JSON Object representation for each DTAction instance in _actions property of the Master.
292
+ * * **modules** : Array of JSON Object representation for each DTModule instance in _modules property of the Master.
293
+ *
294
+ * @returns JSON Object representation of the Master.
295
+ */
296
+ toObject(): DTMasterToObject {
297
+ const toObject: DTMasterToObject = {
298
+ id: this._id,
299
+ key: this._key,
300
+ type: this._componentType,
301
+ managers: [],
302
+ actions: [],
303
+ modules: [],
304
+ };
305
+
306
+ if (Object.keys(this._managers).length) {
307
+ toObject.managers = Object.values(this._managers).map((obj) => obj.toObject());
308
+ }
309
+
310
+ if (Object.keys(this._actions).length) {
311
+ toObject.actions = Object.values(this._actions).map((obj) => obj.toObject());
312
+ }
313
+
314
+ if (Object.keys(this._modules).length) {
315
+ toObject.modules = Object.values(this._modules).map((obj) => obj.toObject());
316
+ }
317
+
318
+ return toObject;
319
+ }
320
+
321
+ /**
322
+ * Return DIX Object representation of the DTManager instance.
323
+ *
324
+ * @returns DIX Object representation of the DTManager.
325
+ */
326
+ toDIXObject(): Record<string, DIXObject> {
327
+ const parentDixObject = super.toDIXObject();
328
+ const dixObject: DIXObject = {
329
+ ...parentDixObject[this._id],
330
+ items: Object.keys(this._managers),
331
+ };
332
+
333
+ const finalDix = {
334
+ [this._id]: dixObject,
335
+ };
336
+ Object.values(this._managers).forEach((item) => {
337
+ Object.assign(finalDix, {
338
+ ...item.toDIXObject(),
339
+ });
340
+ });
341
+
342
+ return finalDix;
343
+ }
344
+
345
+ /**
346
+ * Update the DTMaster instance with a DIX Object.
347
+ *
348
+ * @param dix DIX Object to use for updating.
349
+ */
350
+ updateFromDix(dix: Record<string, DIXObject>): void {
351
+ const dixObject = dix[this._id];
352
+ const dixModule = this.getModule('dix') as DTDIXModule;
353
+ if (dixObject && dixObject.items && dixModule?.isEnabled()) {
354
+ // Synchronize Managers
355
+ dixObject.items.forEach((itemId) => {
356
+ if (this.getManager(itemId)) {
357
+ this.getManager(itemId).updateFromDix(dix);
358
+ } else {
359
+ const newManager = dixModule.syncNewItem(itemId, dix) as DTManager;
360
+ this.addManager(newManager);
361
+ newManager.updateFromDix(dix);
362
+ }
363
+ });
364
+ }
365
+ }
366
+ }