@futdevpro/fsm-dynamo 1.14.11 → 1.14.12

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 (144) hide show
  1. package/.vscode/settings.json +11 -0
  2. package/build/_collections/utils/async.util.js +5 -3
  3. package/build/_collections/utils/async.util.js.map +1 -1
  4. package/build/_collections/utils/json-error-helper.util.d.ts.map +1 -1
  5. package/build/_collections/utils/json-error-helper.util.js.map +1 -1
  6. package/build/_collections/utils/log.util.js +50 -48
  7. package/build/_collections/utils/log.util.js.map +1 -1
  8. package/build/_collections/utils/math/box-bounds.util.js +2 -6
  9. package/build/_collections/utils/math/box-bounds.util.js.map +1 -1
  10. package/build/_collections/utils/math/math.util.js +3 -1
  11. package/build/_collections/utils/math/math.util.js.map +1 -1
  12. package/build/_collections/utils/math/vector2.util.js +62 -60
  13. package/build/_collections/utils/math/vector2.util.js.map +1 -1
  14. package/build/_collections/utils/object.util.js +9 -7
  15. package/build/_collections/utils/object.util.js.map +1 -1
  16. package/build/_collections/utils/stack.util.d.ts.map +1 -1
  17. package/build/_collections/utils/stack.util.js +3 -2
  18. package/build/_collections/utils/stack.util.js.map +1 -1
  19. package/build/_collections/utils/stack.util.spec.js +0 -13
  20. package/build/_collections/utils/stack.util.spec.js.map +1 -1
  21. package/build/_collections/utils/time.util.js +28 -28
  22. package/build/_collections/utils/time.util.js.map +1 -1
  23. package/build/_models/control-models/data-model-params.control-model.js +0 -21
  24. package/build/_models/control-models/data-model-params.control-model.js.map +1 -1
  25. package/build/_models/control-models/data-property-params.control-model.js +3 -59
  26. package/build/_models/control-models/data-property-params.control-model.js.map +1 -1
  27. package/build/_models/control-models/error.control-model.js +8 -25
  28. package/build/_models/control-models/error.control-model.js.map +1 -1
  29. package/build/_models/control-models/http/http-error-response.control-model.js +5 -7
  30. package/build/_models/control-models/http/http-error-response.control-model.js.map +1 -1
  31. package/build/_models/control-models/http/http-headers.control-model.js +4 -17
  32. package/build/_models/control-models/http/http-headers.control-model.js.map +1 -1
  33. package/build/_models/control-models/http/http-response.model-base.js +14 -26
  34. package/build/_models/control-models/http/http-response.model-base.js.map +1 -1
  35. package/build/_models/control-models/poll.control-model.js +4 -26
  36. package/build/_models/control-models/poll.control-model.js.map +1 -1
  37. package/build/_models/control-models/range-value.control-model.js +2 -4
  38. package/build/_models/control-models/range-value.control-model.js.map +1 -1
  39. package/build/_models/control-models/server-status.control-model.js +0 -12
  40. package/build/_models/control-models/server-status.control-model.js.map +1 -1
  41. package/build/_models/control-models/service-endpoint-settings-base.control-model.d.ts +22 -1
  42. package/build/_models/control-models/service-endpoint-settings-base.control-model.d.ts.map +1 -1
  43. package/build/_models/control-models/service-endpoint-settings-base.control-model.js +14 -5
  44. package/build/_models/control-models/service-endpoint-settings-base.control-model.js.map +1 -1
  45. package/build/_models/data-models/errors.data-model.js +3 -18
  46. package/build/_models/data-models/errors.data-model.js.map +1 -1
  47. package/build/_models/data-models/metadata.data-model.js +0 -12
  48. package/build/_models/data-models/metadata.data-model.js.map +1 -1
  49. package/build/_modules/ai/_models/ai-call-settings.interface.js +46 -49
  50. package/build/_modules/ai/_models/ai-call-settings.interface.js.map +1 -1
  51. package/build/_modules/ai/_models/ai-settings.interface.js +0 -3
  52. package/build/_modules/ai/_models/ai-settings.interface.js.map +1 -1
  53. package/build/_modules/ai/_modules/anthropic/_models/aai-call-settings.control-model.js +2 -2
  54. package/build/_modules/ai/_modules/anthropic/_models/aai-call-settings.control-model.js.map +1 -1
  55. package/build/_modules/ai/_modules/anthropic/_models/aai-settings.control-model.js +3 -3
  56. package/build/_modules/ai/_modules/anthropic/_models/aai-settings.control-model.js.map +1 -1
  57. package/build/_modules/ai/_modules/google-ai/_models/gai-call-settings.control-model.js +2 -2
  58. package/build/_modules/ai/_modules/google-ai/_models/gai-call-settings.control-model.js.map +1 -1
  59. package/build/_modules/ai/_modules/google-ai/_models/gai-settings.control-model.js +3 -3
  60. package/build/_modules/ai/_modules/google-ai/_models/gai-settings.control-model.js.map +1 -1
  61. package/build/_modules/ai/_modules/local-ai/_models/lai-call-settings.control-model.js +1 -1
  62. package/build/_modules/ai/_modules/local-ai/_models/lai-call-settings.control-model.js.map +1 -1
  63. package/build/_modules/ai/_modules/local-ai/_models/lai-settings.control-model.js +3 -3
  64. package/build/_modules/ai/_modules/local-ai/_models/lai-settings.control-model.js.map +1 -1
  65. package/build/_modules/ai/_modules/open-ai/_models/oai-call-settings.control-model.js +2 -2
  66. package/build/_modules/ai/_modules/open-ai/_models/oai-call-settings.control-model.js.map +1 -1
  67. package/build/_modules/ai/_modules/open-ai/_models/oai-settings.control-model.js +3 -3
  68. package/build/_modules/ai/_modules/open-ai/_models/oai-settings.control-model.js.map +1 -1
  69. package/build/_modules/ai/_modules/open-ai/index.d.ts.map +1 -1
  70. package/build/_modules/ai/_modules/open-ai/index.js +7 -7
  71. package/build/_modules/ai/_modules/open-ai/index.js.map +1 -1
  72. package/build/_modules/ci-tools/_models/cit-ci-result-info.data-models.js +1 -12
  73. package/build/_modules/ci-tools/_models/cit-ci-result-info.data-models.js.map +1 -1
  74. package/build/_modules/crypto/_collections/crypto-2-non-stable.util.js +2 -2
  75. package/build/_modules/crypto/_collections/crypto-2-non-stable.util.js.map +1 -1
  76. package/build/_modules/crypto/_collections/crypto-old.util.d.ts.map +1 -1
  77. package/build/_modules/crypto/_collections/crypto-old.util.js +16 -16
  78. package/build/_modules/crypto/_collections/crypto-old.util.js.map +1 -1
  79. package/build/_modules/crypto/_collections/crypto.util.js +9 -9
  80. package/build/_modules/crypto/_collections/crypto.util.js.map +1 -1
  81. package/build/_modules/custom-data/_models/cud.data-model.js +0 -1
  82. package/build/_modules/custom-data/_models/cud.data-model.js.map +1 -1
  83. package/build/_modules/data-handler/_models/data-handler-settings.control-model.d.ts +73 -0
  84. package/build/_modules/data-handler/_models/data-handler-settings.control-model.d.ts.map +1 -0
  85. package/build/_modules/data-handler/_models/data-handler-settings.control-model.js +41 -0
  86. package/build/_modules/data-handler/_models/data-handler-settings.control-model.js.map +1 -0
  87. package/build/_modules/data-handler/_models/data-handler.control-model.d.ts +136 -0
  88. package/build/_modules/data-handler/_models/data-handler.control-model.d.ts.map +1 -0
  89. package/build/_modules/data-handler/_models/data-handler.control-model.js +293 -0
  90. package/build/_modules/data-handler/_models/data-handler.control-model.js.map +1 -0
  91. package/build/_modules/data-handler/_models/data-list-handler.control-model.d.ts +111 -0
  92. package/build/_modules/data-handler/_models/data-list-handler.control-model.d.ts.map +1 -0
  93. package/build/_modules/data-handler/_models/data-list-handler.control-model.js +197 -0
  94. package/build/_modules/data-handler/_models/data-list-handler.control-model.js.map +1 -0
  95. package/build/_modules/data-handler/_models/data-search-handler.control-model.d.ts +172 -0
  96. package/build/_modules/data-handler/_models/data-search-handler.control-model.d.ts.map +1 -0
  97. package/build/_modules/data-handler/_models/data-search-handler.control-model.js +319 -0
  98. package/build/_modules/data-handler/_models/data-search-handler.control-model.js.map +1 -0
  99. package/build/_modules/data-handler/_models/list-collector-data-handler.control-model.d.ts +116 -0
  100. package/build/_modules/data-handler/_models/list-collector-data-handler.control-model.d.ts.map +1 -0
  101. package/build/_modules/data-handler/_models/list-collector-data-handler.control-model.js +220 -0
  102. package/build/_modules/data-handler/_models/list-collector-data-handler.control-model.js.map +1 -0
  103. package/build/_modules/data-handler/index.d.ts +6 -0
  104. package/build/_modules/data-handler/index.d.ts.map +1 -0
  105. package/build/_modules/data-handler/index.js +10 -0
  106. package/build/_modules/data-handler/index.js.map +1 -0
  107. package/build/_modules/location/_collections/loc-regions.util.js +4 -4
  108. package/build/_modules/location/_collections/loc-regions.util.js.map +1 -1
  109. package/build/_modules/messaging/_models/msg-conversation.data-model.js +0 -23
  110. package/build/_modules/messaging/_models/msg-conversation.data-model.js.map +1 -1
  111. package/build/_modules/messaging/_models/msg-message.data-model.js +0 -34
  112. package/build/_modules/messaging/_models/msg-message.data-model.js.map +1 -1
  113. package/build/_modules/socket/_models/sck-client-params.control-model.js +2 -7
  114. package/build/_modules/socket/_models/sck-client-params.control-model.js.map +1 -1
  115. package/build/_modules/socket/_models/sck-socket-event.control-model.js +0 -8
  116. package/build/_modules/socket/_models/sck-socket-event.control-model.js.map +1 -1
  117. package/build/_modules/socket/_services/sck-client.service-base.js +69 -72
  118. package/build/_modules/socket/_services/sck-client.service-base.js.map +1 -1
  119. package/build/_modules/usage/_models/usg-action.control-model.js +0 -4
  120. package/build/_modules/usage/_models/usg-action.control-model.js.map +1 -1
  121. package/build/_modules/usage/_models/usg-daily-usage-data.control-model.js +10 -12
  122. package/build/_modules/usage/_models/usg-daily-usage-data.control-model.js.map +1 -1
  123. package/build/_modules/usage/_models/usg-data.control-model.js +2 -8
  124. package/build/_modules/usage/_models/usg-data.control-model.js.map +1 -1
  125. package/build/_modules/usage/_models/usg-session.data-model.js +2 -18
  126. package/build/_modules/usage/_models/usg-session.data-model.js.map +1 -1
  127. package/eslint.config.js +4 -0
  128. package/futdevpro-fsm-dynamo-01.14.12.tgz +0 -0
  129. package/package.json +22 -3
  130. package/src/_collections/utils/json-error-helper.util.ts +5 -3
  131. package/src/_collections/utils/stack.util.ts +8 -4
  132. package/src/_models/control-models/service-endpoint-settings-base.control-model.ts +41 -4
  133. package/src/_modules/ai/_modules/open-ai/index.ts +4 -3
  134. package/src/_modules/crypto/_collections/crypto-old.util.ts +17 -9
  135. package/src/_modules/data-handler/_models/data-handler-settings.control-model.ts +110 -0
  136. package/src/_modules/data-handler/_models/data-handler.control-model.ts +459 -0
  137. package/src/_modules/data-handler/_models/data-list-handler.control-model.ts +245 -0
  138. package/src/_modules/data-handler/_models/data-search-handler.control-model.ts +390 -0
  139. package/src/_modules/data-handler/_models/list-collector-data-handler.control-model.ts +274 -0
  140. package/src/_modules/data-handler/index.ts +6 -0
  141. package/src/_modules/usage/_collections/usg-module-settings.const.ts +1 -1
  142. package/tsconfig.json +2 -2
  143. package/.eslintrc.json +0 -155
  144. package/futdevpro-fsm-dynamo-01.14.11.tgz +0 -0
@@ -0,0 +1,459 @@
1
+ import { DyFM_Log, DyFM_Metadata, DyFM_SearchQuery, DyFM_SearchResult } from '../../..';
2
+ import { BehaviorSubject, Observable } from 'rxjs';
3
+
4
+ import { DyFM_DataHandler_Settings } from './data-handler-settings.control-model';
5
+
6
+ /**
7
+ * DyFM_DataHandlerDataLoadInProgress
8
+ */
9
+ export interface DyFM_DataHandlerDataLoadInProgress<T_LoadBy> {
10
+ loadBy: T_LoadBy;
11
+ progress: Promise<any>;
12
+ }
13
+
14
+ /**
15
+ * DyFM_DataHandlerDataPostProcess
16
+ */
17
+ export type DyFM_DataHandlerDataLoadPostProcess<T_LoadBy> = (data: any, loadedBy: T_LoadBy) => Promise<any> | any;
18
+
19
+ /**
20
+ * DynamoDataHandler is used to create application wide data handling
21
+ * used to create subscribeAble data handler
22
+ *
23
+ * this implements data reload function that uses predefined apiService,
24
+ * using previous or given id,
25
+ * prevents multiple load calls and triggers data change subscriptions when done
26
+ *
27
+ * load post processes can be implemented to ensure special conditions
28
+ *
29
+ * @example
30
+ * personalInformationDH = new DynamoDataHandler<PersonalInformation>({
31
+ * apiService: this.accountApiService,
32
+ * getDataFunctionName: 'getPersonalInformation',
33
+ * });
34
+ */
35
+ export abstract class DyFM_DataHandler<
36
+ T extends DyFM_Metadata | DyFM_Metadata[],
37
+ T_Dependency extends DyFM_Metadata = any,
38
+ T_LoadBy = string
39
+ > {
40
+ name: string;
41
+
42
+ /**
43
+ * BehaviorSubject that holds the current data value.
44
+ * This is the source of truth for data in DyFM.
45
+ */
46
+ protected readonly data_BS: BehaviorSubject<T>;
47
+
48
+ /**
49
+ * Observable that emits the current data value.
50
+ * Derived from the BehaviorSubject.
51
+ */
52
+ readonly data$: Observable<T>;
53
+
54
+ /**
55
+ * BehaviorSubject that indicates whether the data has been loaded.
56
+ * True when data is loaded, false otherwise.
57
+ */
58
+ protected readonly loaded_BS: BehaviorSubject<boolean>;
59
+
60
+ /**
61
+ * Observable that indicates whether the data has been loaded.
62
+ */
63
+ readonly loaded$: Observable<boolean>;
64
+
65
+ /**
66
+ * BehaviorSubject that tracks the current loading operation.
67
+ * Contains information about the ongoing data load operation or false if no load is in progress.
68
+ */
69
+ protected readonly loadInProgress_BS: BehaviorSubject<DyFM_DataHandlerDataLoadInProgress<T_LoadBy> | false>;
70
+
71
+ /**
72
+ * Observable that tracks the current loading operation.
73
+ */
74
+ readonly loadInProgress$: Observable<DyFM_DataHandlerDataLoadInProgress<T_LoadBy> | false>;
75
+
76
+ /**
77
+ * Gets the current data value.
78
+ * @returns The current data value
79
+ */
80
+ abstract get data(): T;
81
+
82
+ /**
83
+ * last loaded by will work properly only if you use the Dynamo form of "_id" for loading
84
+ */
85
+ lastLoadedBy: T_LoadBy; // for lists
86
+ defaultLoadBy: T_LoadBy; // for lists
87
+
88
+ /** this will handle the data loading from the api */
89
+ protected readonly get: (loadBy?: T_LoadBy) => Promise<any>;
90
+ /** this will handle the data setting to the data handler */
91
+ protected readonly set: (data: T) => Promise<T>;
92
+ /** this will handle the data patching to the data handler */
93
+ /* patch: (data: T) => Promise<T>; */
94
+
95
+ // TODO
96
+ /* directApiSetup: any; */
97
+
98
+ /**
99
+ * post processes will be called as pipes after data is loaded by get function,
100
+ * but before data is set to data handler by data_$.set (after get, before Signal triggers)
101
+ */
102
+ protected readonly getPostProcesses: DyFM_DataHandlerDataLoadPostProcess<T_LoadBy>[] = [];
103
+
104
+ noId?: boolean;
105
+
106
+ waitsOnLoad: {
107
+ promise: Promise<void>;
108
+ resolve: () => void;
109
+ }[] = [];
110
+
111
+ protected readonly dependentDataHandlers?: DyFM_DataHandler<any>[] = [];
112
+
113
+ debugLog?: boolean;
114
+
115
+ /**
116
+ * DynamoDataHandler
117
+ * @param apiService any service can be used as apiService
118
+ * as long as the function, you pass in getDataFunctionName exists,
119
+ * uses (or not uses an ID as input) and returns the data as Promise<Type>
120
+ * @param getDataFunctionName the function name on the apiService,
121
+ * that will be used as get api call
122
+ *
123
+ * @param successfulReloadCallBack you can create direct callback for successful reloads
124
+ * by passing it here
125
+ *
126
+ * @param getPostPreprocessLoad post processes can be implemented to ensure special conditions on data
127
+ *
128
+ * @param defaultValue you can set default value that will be used before any load
129
+ * @param noId this flag needs to be true, when using api function without id
130
+ *
131
+ * @example
132
+ * personalInformationDH = new DynamoDataHandler<PersonalInformation>({
133
+ * apiService: this.accountApiService,
134
+ * getDataFunctionName: 'getPersonalInformation',
135
+ * });
136
+ */
137
+ constructor(
138
+ set: DyFM_DataHandler_Settings<T, T_Dependency, T_LoadBy>,
139
+ /** is in construct?
140
+ * so the dependent data handlers are not added here, but in the extended class */
141
+ skipDependencyConnections?: boolean
142
+ ) {
143
+ // TODO: + TRYTOGETTYPE(this.data);
144
+ if (!set.name) {
145
+ set.name = 'Unnamed DataHandler ' + (+new Date());
146
+
147
+ DyFM_Log.warn(
148
+ `DYNAMO DataHandler (${this.name}) WARN: ` +
149
+ `\n Name is not set!`,
150
+ '\n\n', new Error()
151
+ );
152
+ }
153
+
154
+ this.name = set.name;
155
+ this.debugLog = set?.debugLog;
156
+
157
+ // Initialize BehaviorSubjects
158
+ this.data_BS = new BehaviorSubject<T>(set.defaultValue);
159
+ this.data$ = this.data_BS.asObservable();
160
+
161
+ this.loaded_BS = new BehaviorSubject<boolean>(false);
162
+ this.loaded$ = this.loaded_BS.asObservable();
163
+
164
+ this.loadInProgress_BS = new BehaviorSubject<DyFM_DataHandlerDataLoadInProgress<T_LoadBy> | false>(false);
165
+ this.loadInProgress$ = this.loadInProgress_BS.asObservable();
166
+
167
+ if (set.get) {
168
+ this.get = set.get;
169
+ }
170
+
171
+ if (set.set) {
172
+ this.set = set.set;
173
+ }
174
+
175
+ this.noId = !!set.noId;
176
+
177
+ if (set.defaultLoadBy) {
178
+ this.defaultLoadBy = set.defaultLoadBy;
179
+ }
180
+
181
+ if (!skipDependencyConnections) {
182
+ if (set.dependencyDataHandler) {
183
+ this.addDependencyDataHandler(set.dependencyDataHandler);
184
+ }
185
+
186
+ if (set.dependentDataHandlers) {
187
+ this.addDependentDataHandlers(set.dependentDataHandlers, true);
188
+ }
189
+ }
190
+
191
+ if (set.getPostProcess) {
192
+ this.getPostProcesses.push(set.getPostProcess);
193
+ }
194
+ }
195
+
196
+ addDependencyDataHandler(dependencyDataHandler: DyFM_DataHandler<any>): void {
197
+ try {
198
+ if (!dependencyDataHandler) {
199
+ throw new Error(
200
+ `DYNAMO DataHandler ERROR: ` +
201
+ `\n Dependent DataHandler is null or undefined! (${this.name})`
202
+ );
203
+ }
204
+
205
+ if (!(dependencyDataHandler instanceof DyFM_DataHandler)) {
206
+ throw new Error(
207
+ `DYNAMO DataHandler ERROR: ` +
208
+ `\n Dependent DataHandler is not instance of DyFM_DataHandler! (${this.name})`
209
+ );
210
+ }
211
+
212
+ dependencyDataHandler.addDependentDataHandlers([ this ]);
213
+ } catch (error) {
214
+ console.error(
215
+ `DYNAMO DataHandler ERROR: ` +
216
+ `\n Failed to add dependency data handler! ("${this.name}": "${this.constructor.name}")`,
217
+ '\n\n', error,
218
+ '\n\n', new Error()
219
+ );
220
+ }
221
+ }
222
+
223
+ addDependentDataHandlers(
224
+ dependentDataHandlers: DyFM_DataHandler<any, any, any>[],
225
+ isInConstruct?: boolean
226
+ ): void {
227
+ dependentDataHandlers.forEach((dependent_DH: DyFM_DataHandler<any>): void => {
228
+ if (!dependent_DH) {
229
+ throw new Error(
230
+ `DYNAMO DataHandler ERROR: ` +
231
+ `\n Dependent DataHandler is null or undefined! (${this.name})`
232
+ );
233
+ }
234
+
235
+ if (!(dependent_DH instanceof DyFM_DataHandler)) {
236
+ throw new Error(
237
+ `DYNAMO DataHandler ERROR: ` +
238
+ `\n Dependent DataHandler is not instance of DyFM_DataHandler! (${this.name})`
239
+ );
240
+ }
241
+
242
+ if (dependent_DH.name === this.name) {
243
+ throw new Error(
244
+ `DYNAMO DataHandler ERROR: ` +
245
+ `\n Dependent DataHandler has the same name as the current DataHandler! (${this.name})`
246
+ );
247
+ }
248
+
249
+ if (!dependent_DH.data_BS) {
250
+ throw new Error(
251
+ `DYNAMO DataHandler ERROR: ` +
252
+ `\n Dependent DataHandler has not been set up yet! ` +
253
+ `(this: "${this.name}", dependent: "${dependent_DH.name}")` +
254
+ `\n Try the opposite direction of dependency!\n\n`
255
+ );
256
+ }
257
+
258
+ if (!dependentDataHandlers.includes(dependent_DH)) {
259
+ dependentDataHandlers.push(dependent_DH);
260
+ }
261
+
262
+ if (this.debugLog) {
263
+ this.getPostProcesses.push(async (newData: T): Promise<T> => {
264
+ console.log(
265
+ `DYNAMO DataHandler (${this.name}) DEBUG:` +
266
+ `\nDataHandler triggering dependent dataHandler (${dependent_DH.name}) to reload... ` +
267
+ `\n(using id: ${newData && (newData as DyFM_Metadata)._id ? (newData as DyFM_Metadata)._id : ''})`
268
+ );
269
+
270
+ return newData;
271
+ });
272
+ }
273
+
274
+ this.getPostProcesses.push(
275
+ async (newData: T): Promise<T> => {
276
+ if (newData && (newData as DyFM_Metadata)._id && (newData as DyFM_Metadata)._id !== dependent_DH.lastLoadedBy) {
277
+ dependent_DH.reloadData((newData as DyFM_Metadata)._id);
278
+ } else if (!newData) {
279
+ dependent_DH.clear();
280
+ }
281
+
282
+ return newData;
283
+ }
284
+ );
285
+
286
+ if (!isInConstruct && this.data) {
287
+ dependent_DH.reloadData((this.data as DyFM_Metadata)._id);
288
+ }
289
+ });
290
+ }
291
+
292
+ /**
293
+ * reloads data on DynamoDataHandler using predefined apiservice,
294
+ * using previous or given id,
295
+ * prevents multiple load calls and triggers data change subscriptions when done
296
+ * @param loadBy previous or given id (or can be disabled trough setup by: { noId: true })
297
+ * @returns the data as Promise<T>, finishes when the call ends (throws error on fail)
298
+ */
299
+ async reloadData(loadBy?: T_LoadBy): Promise<T> {
300
+ try {
301
+ const loadInProgress = this.loadInProgress_BS.value;
302
+ const useLoadBy: T_LoadBy = loadBy ??
303
+ ((this.data as DyFM_Metadata)?._id as T_LoadBy) ??
304
+ this.lastLoadedBy ??
305
+ this.defaultLoadBy;
306
+
307
+ if (!useLoadBy && !this.noId) {
308
+ console.error(
309
+ `DynamoERROR: Missing ID! (${this.name})`,
310
+ this.data,
311
+ '\n\n', new Error()
312
+ );
313
+
314
+ throw new Error(`DynamoERROR: Missing ID! (${this.name})`);
315
+ }
316
+
317
+ if (loadInProgress && loadInProgress.loadBy === useLoadBy) {
318
+ if (this.debugLog) {
319
+ console.log(
320
+ `DYNAMO DataHandler (${this.name}) DEBUG:` +
321
+ `\n DataHandler reloadData... (using id: ${useLoadBy})` +
322
+ `\n DataHandler is already loading data...`
323
+ );
324
+ }
325
+
326
+ return this.data;
327
+ }
328
+
329
+ if (this.debugLog) {
330
+ console.log(
331
+ `DYNAMO DataHandler (${this.name}) DEBUG:` +
332
+ `\n DataHandler reloadData... (using id: ${useLoadBy})`
333
+ );
334
+ }
335
+
336
+ const loadProgress = this.doReloadDataBy(useLoadBy);
337
+ this.loadInProgress_BS.next({
338
+ loadBy: useLoadBy,
339
+ progress: loadProgress
340
+ });
341
+ loadProgress.finally(() => {
342
+ this.loadInProgress_BS.next(false);
343
+ });
344
+
345
+ return await loadProgress;
346
+ } catch (error) {
347
+ this.loadInProgress_BS.next(false);
348
+ this.loaded_BS.next(false);
349
+
350
+ if (!error?.error?.handled) {
351
+ console.error(
352
+ `DYNAMO DynamoDataHandler (${this.name}) ERROR: ` +
353
+ `\n reloadData was UNSUCCESSFUL`,
354
+ '\n\n', error,
355
+ '\n\n', new Error()
356
+ );
357
+
358
+ if (error?.error?.flag?.includes('DYNAMO')) {
359
+ error.error.handled = true;
360
+ }
361
+ }
362
+
363
+ throw error;
364
+ }
365
+ }
366
+
367
+ async getData(): Promise<T> {
368
+ if (!this.data) {
369
+ const loadProgress = this.loadInProgress_BS.value;
370
+ if (loadProgress) {
371
+ return await loadProgress.progress;
372
+ } else {
373
+ return await this.reloadData();
374
+ }
375
+ } else {
376
+ return this.data;
377
+ }
378
+ }
379
+
380
+ protected async doReloadDataBy(loadedBy: T_LoadBy): Promise<T> {
381
+ try {
382
+ this.lastLoadedBy = loadedBy;
383
+
384
+ return await this.processLoadResult(await this.get(loadedBy), loadedBy);
385
+ } catch (error) {
386
+ this.resolveWaitsOnLoad();
387
+ throw error;
388
+ }
389
+ }
390
+
391
+ async updateData(data: T, dontSendUpdate?: boolean): Promise<void> {
392
+ if (this.set && !dontSendUpdate) {
393
+ this.data_BS.next(await this.set(data));
394
+ } else {
395
+ this.data_BS.next(data);
396
+ }
397
+ }
398
+
399
+ clear(dontClearDependents?: boolean): void {
400
+ this.data_BS.next(undefined);
401
+ this.lastLoadedBy = undefined;
402
+ if (!dontClearDependents) {
403
+ this.dependentDataHandlers.forEach((ddh: DyFM_DataHandler<any>): void => {
404
+ ddh.clear();
405
+ });
406
+ }
407
+ }
408
+
409
+ protected async processLoadResult(result: any, loadedBy: T_LoadBy): Promise<T> {
410
+ if (this.debugLog) {
411
+ console.log(
412
+ `DYNAMO DataHandler (${this.name}) DEBUG:` +
413
+ `\n DataHandler processLoadResult... (using id: ${loadedBy})` +
414
+ `\n Result:`, result
415
+ );
416
+ }
417
+
418
+ for (const postProcess of this.getPostProcesses) {
419
+ if (this.debugLog) {
420
+ console.log(
421
+ `DYNAMO DataHandler (${this.name}) DEBUG:` +
422
+ `\n Post process:`, postProcess
423
+ );
424
+ }
425
+
426
+ result = await postProcess(result, loadedBy);
427
+
428
+ if (!result) {
429
+ DyFM_Log.warn(
430
+ `DYNAMO DataHandler (${this.name}) WARN: ` +
431
+ `\nPost process returned null or undefined!`,
432
+ '\n\n', new Error()
433
+ );
434
+ }
435
+ }
436
+ this.data_BS.next(result);
437
+ this.loaded_BS.next(true);
438
+ this.resolveWaitsOnLoad();
439
+ return result;
440
+ }
441
+
442
+ private resolveWaitsOnLoad(): void {
443
+ this.waitsOnLoad.forEach(({ promise, resolve }) => {
444
+ resolve();
445
+ });
446
+ this.waitsOnLoad = [];
447
+ }
448
+
449
+ /**
450
+ * waits for the first load to finish
451
+ * @returns a promise that resolves when the first load finishes after this "subscription"
452
+ */
453
+ waitForLoad(): Promise<void> {
454
+ let resolve: (value: void) => void;
455
+ const promise = new Promise<void>((r) => { resolve = r; });
456
+ this.waitsOnLoad.push({ promise, resolve });
457
+ return promise;
458
+ }
459
+ }
@@ -0,0 +1,245 @@
1
+ import { BehaviorSubject, Observable } from 'rxjs';
2
+
3
+ import { DyFM_DataListHandler_Settings } from './data-handler-settings.control-model';
4
+
5
+ import { DyFM_DataHandler } from './data-handler.control-model';
6
+ import { DyFM_Metadata } from '../../../_models/data-models/metadata.data-model';
7
+ import { DyFM_Error } from '../../../_models/control-models/error.control-model';
8
+
9
+ /**
10
+ * A specialized data handler for managing arrays of data items.
11
+ * Extends DyFM_DataHandler to provide additional functionality for handling lists of data.
12
+ *
13
+ * @template T_DataList - The type of the data list (must be an array of T_DataItem)
14
+ * @template T_DependencyData - The type of dependency data (defaults to any)
15
+ * @template T_DataLoadBy - The type used to identify how data should be loaded (defaults to string)
16
+ * @template T_DataItem - The type of individual items in the list (must extend DyFM_Metadata)
17
+ *
18
+ * @example
19
+ * ```typescript
20
+ * // Create a data list handler for user messages
21
+ * const messagesHandler = new DyFM_DataListHandler<UserMessage[]>({
22
+ * get: async (userId: string): Promise<UserMessage[]> => {
23
+ * return await userApiService.getUserMessages(userId, 0, 100);
24
+ * }
25
+ * });
26
+ *
27
+ * // Access the data list
28
+ * const messages = messagesHandler.dataList;
29
+ *
30
+ * // Add a new message
31
+ * messagesHandler.addItem(newMessage);
32
+ *
33
+ * // Update an existing message
34
+ * messagesHandler.updateItem(updatedMessage);
35
+ *
36
+ * // Subscribe to changes
37
+ * messagesHandler.dataList$.subscribe(messages => {
38
+ * console.log('Messages updated:', messages);
39
+ * });
40
+ * ```
41
+ */
42
+ export class DyFM_DataListHandler<
43
+ T_DataItem extends DyFM_Metadata,
44
+ T_DependencyData extends DyFM_Metadata = any,
45
+ T_DataLoadBy = string,
46
+ T_UpdateItemBy = string,
47
+ > extends DyFM_DataHandler<T_DataItem[], T_DependencyData, T_DataLoadBy> {
48
+
49
+ protected readonly setItem: (item: T_DataItem, updateBy?: T_UpdateItemBy) => Promise<T_DataItem>;
50
+ protected readonly deleteItem: (id: string, updateBy?: T_UpdateItemBy) => Promise<void>;
51
+
52
+ /**
53
+ * BehaviorSubject that holds the current data list.
54
+ * Computed from the base data_BS BehaviorSubject, ensuring it always returns an array.
55
+ */
56
+ protected readonly dataList_BS: BehaviorSubject<T_DataItem[]>;
57
+
58
+ /**
59
+ * Observable that holds the current data list.
60
+ * Computed from the base data$ Observable, ensuring it always returns an array.
61
+ */
62
+ readonly dataList$: Observable<T_DataItem[]>;
63
+
64
+ /**
65
+ * BehaviorSubject that holds the currently active item.
66
+ */
67
+ readonly activeItem_BS: BehaviorSubject<T_DataItem>;
68
+
69
+ /**
70
+ * Observable that holds the currently active item.
71
+ */
72
+ readonly activeItem$: Observable<T_DataItem>;
73
+
74
+ /**
75
+ * Gets the current data list.
76
+ * @returns The current array of data items
77
+ */
78
+ override get data(): T_DataItem[] {
79
+ return this.dataList_BS.value;
80
+ }
81
+
82
+ /**
83
+ * Gets the current data list.
84
+ * Alias for data getter.
85
+ * @returns The current array of data items
86
+ */
87
+ get dataList(): T_DataItem[] {
88
+ return this.dataList_BS.value;
89
+ }
90
+
91
+ /**
92
+ * Creates a new instance of DyFM_DataListHandler.
93
+ *
94
+ * @param set - Configuration settings for the data handler
95
+ * @param skipDependencyConnections - Optional flag to skip dependency connections during construction
96
+ */
97
+ constructor(
98
+ set: DyFM_DataListHandler_Settings<T_DataItem, T_DependencyData, T_DataLoadBy, T_UpdateItemBy>,
99
+ /** is in construct?
100
+ * so the dependent data handlers are not added here, but in the extended class */
101
+ skipDependencyConnections?: boolean,
102
+ ) {
103
+ super(set, skipDependencyConnections);
104
+
105
+ // Initialize dataList BehaviorSubject and Observable
106
+ this.dataList_BS = new BehaviorSubject<T_DataItem[]>([]);
107
+ this.dataList$ = this.dataList_BS.asObservable();
108
+
109
+ // Initialize activeItem BehaviorSubject and Observable
110
+ this.activeItem_BS = new BehaviorSubject<T_DataItem>(null);
111
+ this.activeItem$ = this.activeItem_BS.asObservable();
112
+
113
+ // Subscribe to data changes to update dataList
114
+ this.data$.subscribe(data => {
115
+ const dataList = Array.isArray(data) ? data : [];
116
+ this.dataList_BS.next(dataList);
117
+ });
118
+
119
+ if (set.setItem) {
120
+ this.setItem = set.setItem;
121
+ }
122
+
123
+ if (set.deleteItem) {
124
+ this.deleteItem = set.deleteItem;
125
+ }
126
+
127
+ this.getPostProcesses.push((data: T_DataItem[]) => {
128
+ // check if data is an array
129
+ if (!Array.isArray(data) ) {
130
+ console.error(
131
+ `DYNAMO DataListHandler (${this.name}) ERROR: ` +
132
+ `\nData is not an array!:` +
133
+ '\n\n', data
134
+ );
135
+ throw new Error(`Data is not an array! (${this.name})`);
136
+ }
137
+
138
+ return data;
139
+ });
140
+
141
+ if (!skipDependencyConnections) {
142
+ if (set.dependencyDataHandler) {
143
+ this.addDependencyDataHandler(set.dependencyDataHandler);
144
+ }
145
+
146
+ if (set.dependentDataHandlers) {
147
+ this.addDependentDataHandlers(set.dependentDataHandlers, true);
148
+ }
149
+ }
150
+ }
151
+
152
+ override async reloadData(loadBy?: T_DataLoadBy): Promise<T_DataItem[]> {
153
+ const setActiveItem = !this.activeItem_BS.value || this.activeItem_BS.value?._id !== loadBy;
154
+ const dataList = await super.reloadData(loadBy);
155
+ if (setActiveItem && dataList.length > 0) {
156
+ this.setActiveItem(dataList[0]);
157
+ }
158
+ return dataList;
159
+ }
160
+
161
+ override clear(dontClearDependents?: boolean): void {
162
+ super.clear(dontClearDependents);
163
+ this.activeItem_BS.next(null);
164
+ }
165
+
166
+ /**
167
+ * Adds a new item to the data list.
168
+ *
169
+ * @param item - The item to add to the list
170
+ * @param updateBy - Optional parameter for update tracking
171
+ * @param dontSendUpdate - Optional flag to prevent sending update notification
172
+ */
173
+ async addItem(item: T_DataItem, updateBy?: T_UpdateItemBy, dontSendUpdate?: boolean): Promise<void> {
174
+ const currentData = this.data;
175
+ currentData.push(item);
176
+ if (this.setItem && !dontSendUpdate) {
177
+ await this.setItem(item, updateBy);
178
+ // dont send update because we already sent it to the setItem function
179
+ await this.updateData(this.dataList_BS.value, true);
180
+ } else {
181
+ await this.updateData(this.dataList_BS.value, dontSendUpdate);
182
+ }
183
+ this.activeItem_BS.next(item);
184
+ }
185
+
186
+ /**
187
+ * Removes an item from the data list.
188
+ * The item is identified by its _id property.
189
+ *
190
+ * @param id - The id of the item to remove from the list
191
+ * @param updateBy - Optional parameter for update tracking
192
+ * @param dontSendUpdate - Optional flag to prevent sending update notification
193
+ */
194
+ async removeItem(id: string, updateBy?: T_UpdateItemBy, dontSendUpdate?: boolean): Promise<void> {
195
+ const currentData = this.data;
196
+ const index = currentData.findIndex(i => i._id === id);
197
+ if (index !== -1) {
198
+ currentData.splice(index, 1);
199
+ if (this.deleteItem) {
200
+ await this.deleteItem(id, updateBy);
201
+ await this.updateData(this.dataList_BS.value, true);
202
+ } else {
203
+ await this.updateData(this.dataList_BS.value, dontSendUpdate);
204
+ }
205
+ }
206
+ if (this.activeItem_BS.value?._id === id) {
207
+ this.activeItem_BS.next(null);
208
+ }
209
+ }
210
+
211
+ /**
212
+ * Updates an existing item in the data list.
213
+ * The item is identified by its _id property.
214
+ *
215
+ * @param item - The updated item
216
+ * @param updateBy - Optional parameter for update tracking
217
+ * @param dontSendUpdate - Optional flag to prevent sending update notification
218
+ * @throws Error if the item is not found in the list
219
+ */
220
+ async updateItem(item: T_DataItem, updateBy?: T_UpdateItemBy, dontSendUpdate?: boolean): Promise<void> {
221
+ const currentData = this.data;
222
+ const index = currentData.findIndex(i => i._id === item._id);
223
+ if (index !== -1) {
224
+ currentData[index] = item;
225
+ if (this.setItem) {
226
+ await this.setItem(item, updateBy);
227
+ await this.updateData(this.dataList_BS.value, true);
228
+ } else {
229
+ await this.updateData(this.dataList_BS.value, dontSendUpdate);
230
+ }
231
+ } else {
232
+ throw new DyFM_Error({
233
+ error: new Error(`Item not found! (${this.name})`),
234
+ errorCode: 'DyFM-DLH-UI1',
235
+ additionalContent: {
236
+ item: item,
237
+ },
238
+ });
239
+ }
240
+ }
241
+
242
+ setActiveItem(item: T_DataItem): void {
243
+ this.activeItem_BS.next(item);
244
+ }
245
+ }