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,225 +1,258 @@
1
- import * as uuid from 'uuid';
2
- import DYOToolsError from './DTError';
3
- import { DTComponentOptions } from '../types';
4
-
5
- /**
6
- * @template {string} DTComponentOptions
7
- */
8
- export default abstract class DYOToolsComponent<IComponentOptions extends DTComponentOptions = DTComponentOptions> {
9
- /**
10
- * Component unique ID. Use uuid v4 generator.
11
- */
12
- protected _id: string;
13
-
14
- /**
15
- * Component specific and accessible label.
16
- * If not provided, the key is set with ID by default.
17
- */
18
- protected _key: string;
19
-
20
- /**
21
- * Component Parent Context.
22
- *
23
- * A component can have only one *context*, and be managed by a parent Component.
24
- */
25
- protected _context?: DYOToolsComponent;
26
-
27
- /**
28
- * Higher Level Component category.
29
- *
30
- * Describing component Type, like Element, Bunch, Manager...
31
- */
32
- protected abstract _componentType: string;
33
-
34
- /**
35
- * Second Level Component category.
36
- *
37
- * Describing component Domain, like Card, Dice, Token...
38
- */
39
- protected _domain?: string;
40
-
41
- /**
42
- * Third Level Component category.
43
- *
44
- * Describing component extra type, like Hand, Deck, Trick...
45
- */
46
- protected _subKind?: string;
47
-
48
- /**
49
- * Array of current errors for the Component.
50
- *
51
- * Errors are only available if the **errors** option is enabled.
52
- */
53
- protected _errors: DYOToolsError[];
54
-
55
- /**
56
- * Component options configuration.
57
- * Defined by generic type IComponentOptions.
58
- *
59
- * For all component, global option can be :
60
- * * **errors** : Default *false*. If *true*, no exception is thrown when an error occurred, a new DTError instance is
61
- * added to the _errors property array instead. If *false*, throw the exception with a DTError instance.
62
- */
63
- protected _options: IComponentOptions;
64
-
65
- /**
66
- * Set automatic unique _id and _key.
67
- *
68
- * @param key Optional Key to set. If not provided, set the _key with the _id value.
69
- * @param options Specific options configuration for the instance. Default empty object.
70
- */
71
- constructor(key?: string, options: Partial<IComponentOptions> = {}) {
72
- this._id = uuid.v4();
73
- this._key = key || this._id;
74
- this._errors = [];
75
-
76
- const defaultOptions: DTComponentOptions = {
77
- errors: false,
78
- };
79
- this._options = {
80
- ...defaultOptions,
81
- ...options,
82
- } as IComponentOptions;
83
- }
84
-
85
- /**
86
- * Getter for _id property.
87
- */
88
- getId(): string {
89
- return this._id;
90
- }
91
-
92
- /**
93
- * Getter for _key property.
94
- */
95
- getKey(): string {
96
- return this._key;
97
- }
98
-
99
- /**
100
- * Getter for _context property.
101
- *
102
- * @param contextType If provided, the getter parse all component level hierarchy to find the corresponding component
103
- * with **contextType** as _componentType value, and returns it. Return undefined if not found.
104
- *
105
- * @returns Direct parent Component or higher level Component if filtered with **contextType**.
106
- * Returns undefined if context doesn't exist.
107
- */
108
- getContext<IContext extends DYOToolsComponent = DYOToolsComponent>(contextType?: string): IContext | undefined {
109
- if (this._context) {
110
- if (!contextType || this._context.getComponentType() === contextType) {
111
- return this._context as IContext;
112
- }
113
- return this._context.getContext(contextType);
114
- }
115
- return undefined;
116
- }
117
-
118
- /**
119
- * Setter for _context property.
120
- */
121
- setContext<IContext extends DYOToolsComponent = DYOToolsComponent>(value: IContext): void {
122
- this._context = value;
123
- }
124
-
125
- /**
126
- * Remove the current context of component.
127
- */
128
- removeContext(): void {
129
- this._context = undefined;
130
- }
131
-
132
- /**
133
- * Getter for _componentType property.
134
- */
135
- getComponentType(): string {
136
- return this._componentType;
137
- }
138
-
139
- /**
140
- * Getter for _domain property.
141
- */
142
- getDomain(): string {
143
- return this._domain;
144
- }
145
-
146
- /**
147
- * Getter for _subKind property.
148
- */
149
- getSubKind(): string {
150
- return this._subKind;
151
- }
152
-
153
- /**
154
- * Getter for _errors property.
155
- *
156
- * Note : Errors are always provided by the higher order component, defined into the _context property.
157
- */
158
- getErrors(): DYOToolsError[] {
159
- if (this.getContext()) {
160
- return this.getContext().getErrors();
161
- }
162
- return this._errors;
163
- }
164
-
165
- /**
166
- * Return the last error (most recent) of the current component. Undefined if _errors is empty.
167
- *
168
- * Note : Errors are always provided by the higher order component, defined into the _context property.
169
- */
170
- getLastError(): DYOToolsError | undefined {
171
- if (this.getContext()) {
172
- return this.getContext().getLastError();
173
- }
174
- return this._errors.length > 0 ? this._errors[this._errors.length - 1] : undefined;
175
- }
176
-
177
- /**
178
- * Generic method to trigger an error, depending on the **errors** option :
179
- * * if the option is set to *false*, throw the DTError instance passed as an argument.
180
- * * if the option is set to *true*, add DTError instance passed as an argument in the _errors array.
181
- *
182
- * Note : Errors are always stored into the higher order component, defined into the _context property.
183
- *
184
- * @param error DYOToolsError instance to trigger
185
- */
186
- triggerError(error: DYOToolsError): void {
187
- const { errors = false } = this._options;
188
- if (this.getContext()) {
189
- this.getContext().triggerError(error);
190
- } else if (!errors) {
191
- throw error;
192
- } else {
193
- this._errors.push(error);
194
- }
195
- }
196
-
197
- /**
198
- * Clear all current errors.
199
- *
200
- * Note : Errors are always stored into the higher order component, defined into the _context property.
201
- */
202
- clearErrors(): void {
203
- if (this.getContext()) {
204
- this.getContext().clearErrors();
205
- }
206
- this._errors = [];
207
- }
208
-
209
- /**
210
- * Getter for _options property.
211
- */
212
- getOptions(): IComponentOptions {
213
- return this._options;
214
- }
215
-
216
- /**
217
- * Abstract method for JSON Object representation of the component and returning it.
218
- */
219
- abstract toObject(): unknown;
220
-
221
- /**
222
- * Abstract method for String representation of the component and returning it.
223
- */
224
- abstract toString(): string;
225
- }
1
+ import { v6 as uuidV6 } from 'uuid';
2
+ import DTError from './DTError';
3
+ import { DIXObject, DTComponentOptions } from '../types';
4
+
5
+ /**
6
+ * @template {string} DTComponentOptions
7
+ */
8
+ export default abstract class DTComponent<
9
+ IComponentOptions extends DTComponentOptions = DTComponentOptions,
10
+ > {
11
+ /**
12
+ * Component unique ID. Use uuid v4 generator.
13
+ */
14
+ protected _id: string;
15
+
16
+ /**
17
+ * Component specific and accessible label.
18
+ * If not provided, the key is set with ID by default.
19
+ */
20
+ protected _key: string;
21
+
22
+ /**
23
+ * Component Parent Context.
24
+ *
25
+ * A component can have only one *context*, and be managed by a parent Component.
26
+ */
27
+ protected _context?: DTComponent;
28
+
29
+ /**
30
+ * Higher Level Component category.
31
+ *
32
+ * Describing component Type, like Element, Bunch, Manager...
33
+ */
34
+ protected abstract _componentType: string;
35
+
36
+ /**
37
+ * Second Level Component category.
38
+ *
39
+ * Describing component Domain, like Card, Dice, Token...
40
+ */
41
+ protected _domain?: string;
42
+
43
+ /**
44
+ * Third Level Component category.
45
+ *
46
+ * Describing component extra type, like Hand, Deck, Trick...
47
+ */
48
+ protected _subKind?: string;
49
+
50
+ /**
51
+ * Array of current errors for the Component.
52
+ *
53
+ * Errors are only available if the **errors** option is enabled.
54
+ */
55
+ protected _errors: DTError[];
56
+
57
+ /**
58
+ * Component options configuration.
59
+ * Defined by generic type IComponentOptions.
60
+ *
61
+ * For all component, global option can be :
62
+ * * **errors** : Default *false*. If *true*, no exception is thrown when an error occurred, a new DTError instance is
63
+ * added to the _errors property array instead. If *false*, throw the exception with a DTError instance.
64
+ * * **syncId** : Default *undefined*. If specified, the string argument passed will be the id of the new DTComponent.
65
+ * Used to synchronize ids between to instances (with the DIX Module). Be careful when using.
66
+ */
67
+ protected _options: IComponentOptions;
68
+
69
+ /**
70
+ * Set automatic unique _id and _key.
71
+ *
72
+ * @param key Optional Key to set. If not provided, set the _key with the _id value.
73
+ * @param options Specific options configuration for the instance. Default empty object.
74
+ */
75
+ constructor(key?: string, options: Partial<IComponentOptions> = {}) {
76
+ const defaultOptions: DTComponentOptions = {
77
+ errors: false,
78
+ syncId: undefined,
79
+ };
80
+
81
+ this._options = {
82
+ ...defaultOptions,
83
+ ...options,
84
+ } as IComponentOptions;
85
+
86
+ this._id = this._options.syncId ?? uuidV6();
87
+ this._key = key || this._id;
88
+ this._errors = [];
89
+ }
90
+
91
+ /**
92
+ * Getter for _id property.
93
+ */
94
+ getId(): string {
95
+ return this._id;
96
+ }
97
+
98
+ /**
99
+ * Getter for _key property.
100
+ */
101
+ getKey(): string {
102
+ return this._key;
103
+ }
104
+
105
+ /**
106
+ * Getter for _context property.
107
+ *
108
+ * @param contextType If provided, the getter parse all component level hierarchy to find the corresponding component
109
+ * with **contextType** as _componentType value, and returns it. Return undefined if not found.
110
+ *
111
+ * @returns Direct parent Component or higher level Component if filtered with **contextType**.
112
+ * Returns undefined if context doesn't exist.
113
+ */
114
+ getContext<IContext extends DTComponent = DTComponent>(contextType?: string): IContext | undefined {
115
+ if (this._context) {
116
+ if (!contextType || this._context.getComponentType() === contextType) {
117
+ return this._context as IContext;
118
+ }
119
+ return this._context.getContext(contextType);
120
+ }
121
+ return undefined;
122
+ }
123
+
124
+ /**
125
+ * Setter for _context property.
126
+ */
127
+ setContext<IContext extends DTComponent = DTComponent>(value: IContext): void {
128
+ this._context = value;
129
+ }
130
+
131
+ /**
132
+ * Remove the current context of component.
133
+ */
134
+ removeContext(): void {
135
+ this._context = undefined;
136
+ }
137
+
138
+ /**
139
+ * Getter for _componentType property.
140
+ */
141
+ getComponentType(): string {
142
+ return this._componentType;
143
+ }
144
+
145
+ /**
146
+ * Getter for _domain property.
147
+ */
148
+ getDomain(): string {
149
+ return this._domain;
150
+ }
151
+
152
+ /**
153
+ * Getter for _subKind property.
154
+ */
155
+ getSubKind(): string {
156
+ return this._subKind;
157
+ }
158
+
159
+ /**
160
+ * Getter for _errors property.
161
+ *
162
+ * Note : Errors are always provided by the higher order component, defined into the _context property.
163
+ */
164
+ getErrors(): DTError[] {
165
+ if (this.getContext()) {
166
+ return this.getContext().getErrors();
167
+ }
168
+ return this._errors;
169
+ }
170
+
171
+ /**
172
+ * Return the last error (most recent) of the current component. Undefined if _errors is empty.
173
+ *
174
+ * Note : Errors are always provided by the higher order component, defined into the _context property.
175
+ */
176
+ getLastError(): DTError | undefined {
177
+ if (this.getContext()) {
178
+ return this.getContext().getLastError();
179
+ }
180
+ return this._errors.length > 0 ? this._errors[this._errors.length - 1] : undefined;
181
+ }
182
+
183
+ /**
184
+ * Generic method to trigger an error, depending on the **errors** option :
185
+ * * if the option is set to *false*, throw the DTError instance passed as an argument.
186
+ * * if the option is set to *true*, add DTError instance passed as an argument in the _errors array.
187
+ *
188
+ * Note : Errors are always stored into the higher order component, defined into the _context property.
189
+ *
190
+ * @param error DYOToolsError instance to trigger
191
+ */
192
+ triggerError(error: DTError): void {
193
+ const { errors = false } = this._options;
194
+ if (this.getContext()) {
195
+ this.getContext().triggerError(error);
196
+ } else if (!errors) {
197
+ throw error;
198
+ } else {
199
+ this._errors.push(error);
200
+ }
201
+ }
202
+
203
+ /**
204
+ * Clear all current errors.
205
+ *
206
+ * Note : Errors are always stored into the higher order component, defined into the _context property.
207
+ */
208
+ clearErrors(): void {
209
+ if (this.getContext()) {
210
+ this.getContext().clearErrors();
211
+ }
212
+ this._errors = [];
213
+ }
214
+
215
+ /**
216
+ * Getter for _options property.
217
+ */
218
+ getOptions(): IComponentOptions {
219
+ return this._options;
220
+ }
221
+
222
+ /**
223
+ * Return DIX Object representation of the DTComponent instance.
224
+ *
225
+ * @returns DIX Object representation of the DTComponent.
226
+ */
227
+ toDIXObject(): Record<string, DIXObject> {
228
+ const dixOptions = this._options;
229
+ delete dixOptions.syncId;
230
+
231
+ const dixObject: DIXObject = {
232
+ useClass: this.constructor.name,
233
+ key: this._key,
234
+ componentType: this._componentType,
235
+ options: JSON.stringify(this._options),
236
+ };
237
+
238
+ return {
239
+ [this._id]: dixObject,
240
+ };
241
+ }
242
+
243
+ /**
244
+ * Abstract method to update the component with a DIX Object Format.
245
+ * @param dix DIX Object to use for updating.
246
+ */
247
+ abstract updateFromDix(dix: Record<string, DIXObject>): void;
248
+
249
+ /**
250
+ * Abstract method for JSON Object representation of the component and returning it.
251
+ */
252
+ abstract toObject(): unknown;
253
+
254
+ /**
255
+ * Abstract method for String representation of the component and returning it.
256
+ */
257
+ abstract toString(): string;
258
+ }
@@ -1,53 +1,88 @@
1
- import { DTAcceptedMetaData, DTComponentOptions } from '../types';
2
- import DYOToolsComponentWithMeta from './DTComponentWithMeta';
3
- import DYOToolsMaster from './DTMaster';
4
-
5
- export default abstract class DYOToolsComponentPhysical<
6
- IComponentMeta extends DTAcceptedMetaData,
7
- IComponentOptions extends DTComponentOptions = DTComponentOptions,
8
- > extends DYOToolsComponentWithMeta<IComponentMeta, IComponentOptions> {
9
- /**
10
- * Owner string representation, if defined.
11
- */
12
- protected _owner?: string;
13
-
14
- /**
15
- * Returns Manager component from context, or undefined.
16
- */
17
- getManager(): DYOToolsMaster | undefined {
18
- return this.getContext<DYOToolsMaster>('manager');
19
- }
20
-
21
- /**
22
- * Returns Master component from context, or undefined.
23
- */
24
- getMaster(): DYOToolsMaster | undefined {
25
- return this.getContext<DYOToolsMaster>('master');
26
- }
27
-
28
- /**
29
- * Getter for _owner property.
30
- */
31
- getOwner(): string {
32
- return this._owner;
33
- }
34
-
35
- /**
36
- * Setter for _owner property.
37
- */
38
- setOwner(value: string): void {
39
- this._owner = value;
40
- }
41
-
42
- /**
43
- * Remove the current owner of element.
44
- */
45
- removeOwner(): void {
46
- this._owner = undefined;
47
- }
48
-
49
- /**
50
- * Abstract method for copying the Component and returning it.
51
- */
52
- abstract copy(): DYOToolsComponentPhysical<IComponentMeta, IComponentOptions>;
53
- }
1
+ import { DIXObject, DTAcceptedMetaData, DTComponentOptions } from '../types';
2
+ import DTComponentWithMeta from './DTComponentWithMeta';
3
+ import DTMaster from './DTMaster';
4
+ import DTManager from './DTManager';
5
+
6
+ export default abstract class DTComponentPhysical<
7
+ IComponentMeta extends DTAcceptedMetaData,
8
+ IComponentOptions extends DTComponentOptions = DTComponentOptions,
9
+ > extends DTComponentWithMeta<IComponentMeta, IComponentOptions> {
10
+ /**
11
+ * Owner string representation, if defined.
12
+ */
13
+ protected _owner?: string;
14
+
15
+ /**
16
+ * Returns Manager component from context, or undefined.
17
+ */
18
+ getManager(): DTManager | undefined {
19
+ return this.getContext<DTManager>('manager');
20
+ }
21
+
22
+ /**
23
+ * Returns Master component from context, or undefined.
24
+ */
25
+ getMaster(): DTMaster | undefined {
26
+ return this.getContext<DTMaster>('master');
27
+ }
28
+
29
+ /**
30
+ * Getter for _owner property.
31
+ */
32
+ getOwner(): string {
33
+ return this._owner;
34
+ }
35
+
36
+ /**
37
+ * Setter for _owner property.
38
+ */
39
+ setOwner(value: string): void {
40
+ this._owner = value;
41
+ }
42
+
43
+ /**
44
+ * Remove the current owner of element.
45
+ */
46
+ removeOwner(): void {
47
+ this._owner = undefined;
48
+ }
49
+
50
+ /**
51
+ * Return DIX Object representation of the DTComponentPhysical instance.
52
+ *
53
+ * @returns DIX Object representation of the DTComponentPhysical.
54
+ */
55
+ toDIXObject(): Record<string, DIXObject> {
56
+ const parentDixObject = super.toDIXObject();
57
+ const dixObject: DIXObject = {
58
+ ...parentDixObject[this._id],
59
+ };
60
+
61
+ if (this._owner) {
62
+ dixObject.owner = this._owner;
63
+ }
64
+
65
+ return {
66
+ [this._id]: dixObject,
67
+ };
68
+ }
69
+
70
+ /**
71
+ * Update the DTComponentPhysical instance with a DIX Object.
72
+ *
73
+ * @param dix DIX Object to use for updating.
74
+ */
75
+ updateFromDix(dix: Record<string, DIXObject>) {
76
+ super.updateFromDix(dix);
77
+ const dixObject = dix[this._id];
78
+
79
+ if (dixObject && dixObject.owner) {
80
+ this._owner = dixObject.owner;
81
+ }
82
+ }
83
+
84
+ /**
85
+ * Abstract method for copying the Component and returning it.
86
+ */
87
+ abstract copy(): DTComponentPhysical<IComponentMeta, IComponentOptions>;
88
+ }