@futdevpro/fsm-dynamo 1.14.11 → 1.14.13

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 (76) hide show
  1. package/.vscode/settings.json +11 -0
  2. package/build/_collections/utils/json-error-helper.util.d.ts.map +1 -1
  3. package/build/_collections/utils/json-error-helper.util.js.map +1 -1
  4. package/build/_collections/utils/stack.util.d.ts.map +1 -1
  5. package/build/_collections/utils/stack.util.js +3 -2
  6. package/build/_collections/utils/stack.util.js.map +1 -1
  7. package/build/_models/control-models/service-endpoint-settings-base.control-model.d.ts +22 -1
  8. package/build/_models/control-models/service-endpoint-settings-base.control-model.d.ts.map +1 -1
  9. package/build/_models/control-models/service-endpoint-settings-base.control-model.js +23 -1
  10. package/build/_models/control-models/service-endpoint-settings-base.control-model.js.map +1 -1
  11. package/build/_modules/ai/_modules/open-ai/index.d.ts.map +1 -1
  12. package/build/_modules/ai/_modules/open-ai/index.js +7 -7
  13. package/build/_modules/ai/_modules/open-ai/index.js.map +1 -1
  14. package/build/_modules/crypto/_collections/{crypto-2-non-stable.util.d.ts → crypto-v1.util.d.ts} +1 -1
  15. package/build/_modules/crypto/_collections/crypto-v1.util.d.ts.map +1 -0
  16. package/build/_modules/crypto/_collections/{crypto-2-non-stable.util.js → crypto-v1.util.js} +1 -1
  17. package/build/_modules/crypto/_collections/crypto-v1.util.js.map +1 -0
  18. package/build/_modules/crypto/_collections/{crypto-old.util.d.ts → crypto-v2.util.d.ts} +1 -1
  19. package/build/_modules/crypto/_collections/crypto-v2.util.d.ts.map +1 -0
  20. package/build/_modules/crypto/_collections/{crypto-old.util.js → crypto-v2.util.js} +10 -10
  21. package/build/_modules/crypto/_collections/crypto-v2.util.js.map +1 -0
  22. package/build/_modules/crypto/_collections/crypto-v4.util.d.ts +165 -0
  23. package/build/_modules/crypto/_collections/crypto-v4.util.d.ts.map +1 -0
  24. package/build/_modules/crypto/_collections/crypto-v4.util.js +611 -0
  25. package/build/_modules/crypto/_collections/crypto-v4.util.js.map +1 -0
  26. package/build/_modules/crypto/index.d.ts.map +1 -1
  27. package/build/_modules/crypto/index.js +7 -7
  28. package/build/_modules/crypto/index.js.map +1 -1
  29. package/build/_modules/data-handler/_models/data-handler-settings.control-model.d.ts +73 -0
  30. package/build/_modules/data-handler/_models/data-handler-settings.control-model.d.ts.map +1 -0
  31. package/build/_modules/data-handler/_models/data-handler-settings.control-model.js +83 -0
  32. package/build/_modules/data-handler/_models/data-handler-settings.control-model.js.map +1 -0
  33. package/build/_modules/data-handler/_models/data-handler.control-model.d.ts +136 -0
  34. package/build/_modules/data-handler/_models/data-handler.control-model.d.ts.map +1 -0
  35. package/build/_modules/data-handler/_models/data-handler.control-model.js +333 -0
  36. package/build/_modules/data-handler/_models/data-handler.control-model.js.map +1 -0
  37. package/build/_modules/data-handler/_models/data-list-handler.control-model.d.ts +111 -0
  38. package/build/_modules/data-handler/_models/data-list-handler.control-model.d.ts.map +1 -0
  39. package/build/_modules/data-handler/_models/data-list-handler.control-model.js +217 -0
  40. package/build/_modules/data-handler/_models/data-list-handler.control-model.js.map +1 -0
  41. package/build/_modules/data-handler/_models/data-search-handler.control-model.d.ts +172 -0
  42. package/build/_modules/data-handler/_models/data-search-handler.control-model.d.ts.map +1 -0
  43. package/build/_modules/data-handler/_models/data-search-handler.control-model.js +325 -0
  44. package/build/_modules/data-handler/_models/data-search-handler.control-model.js.map +1 -0
  45. package/build/_modules/data-handler/_models/list-collector-data-handler.control-model.d.ts +116 -0
  46. package/build/_modules/data-handler/_models/list-collector-data-handler.control-model.d.ts.map +1 -0
  47. package/build/_modules/data-handler/_models/list-collector-data-handler.control-model.js +245 -0
  48. package/build/_modules/data-handler/_models/list-collector-data-handler.control-model.js.map +1 -0
  49. package/build/_modules/data-handler/index.d.ts +6 -0
  50. package/build/_modules/data-handler/index.d.ts.map +1 -0
  51. package/build/_modules/data-handler/index.js +10 -0
  52. package/build/_modules/data-handler/index.js.map +1 -0
  53. package/eslint.config.js +4 -0
  54. package/futdevpro-fsm-dynamo-01.14.13.tgz +0 -0
  55. package/package.json +22 -3
  56. package/src/_collections/utils/json-error-helper.util.ts +5 -3
  57. package/src/_collections/utils/stack.util.ts +8 -4
  58. package/src/_models/control-models/service-endpoint-settings-base.control-model.ts +41 -4
  59. package/src/_modules/ai/_modules/open-ai/index.ts +4 -3
  60. package/src/_modules/crypto/_collections/{crypto-old.util.ts → crypto-v2.util.ts} +17 -9
  61. package/src/_modules/crypto/_collections/crypto-v4.util.ts +702 -0
  62. package/src/_modules/crypto/index.ts +4 -3
  63. package/src/_modules/data-handler/_models/data-handler-settings.control-model.ts +110 -0
  64. package/src/_modules/data-handler/_models/data-handler.control-model.ts +459 -0
  65. package/src/_modules/data-handler/_models/data-list-handler.control-model.ts +245 -0
  66. package/src/_modules/data-handler/_models/data-search-handler.control-model.ts +390 -0
  67. package/src/_modules/data-handler/_models/list-collector-data-handler.control-model.ts +274 -0
  68. package/src/_modules/data-handler/index.ts +6 -0
  69. package/src/_modules/usage/_collections/usg-module-settings.const.ts +1 -1
  70. package/.eslintrc.json +0 -155
  71. package/build/_modules/crypto/_collections/crypto-2-non-stable.util.d.ts.map +0 -1
  72. package/build/_modules/crypto/_collections/crypto-2-non-stable.util.js.map +0 -1
  73. package/build/_modules/crypto/_collections/crypto-old.util.d.ts.map +0 -1
  74. package/build/_modules/crypto/_collections/crypto-old.util.js.map +0 -1
  75. package/futdevpro-fsm-dynamo-01.14.11.tgz +0 -0
  76. /package/src/_modules/crypto/_collections/{crypto-2-non-stable.util.ts → crypto-v1.util.ts} +0 -0
@@ -1,13 +1,14 @@
1
1
 
2
2
 
3
3
 
4
- /* try {
5
- require.resolve('crypto-js');
4
+ try {
5
+ /* require.resolve('crypto-js'); */
6
+ import('crypto-js');
6
7
  } catch {
7
8
  console.log(
8
9
  '❌ crypto-js package not found, please install it with: pnpm add crypto-js'
9
10
  );
10
- } */
11
+ }
11
12
 
12
13
 
13
14
  export * from './_collections/crypto.util';
@@ -0,0 +1,110 @@
1
+
2
+ import { DyFM_Metadata } from '../../../_models/data-models/metadata.data-model';
3
+ import { DyFM_SearchQuery } from '../../../_models/interfaces/search-query.interface';
4
+ import { DyFM_DataHandler, DyFM_DataHandlerDataLoadPostProcess } from './data-handler.control-model';
5
+
6
+ /**
7
+ * DyFM_DataHandler_Settings
8
+ * @param apiService any service can be used as apiService
9
+ * as long as the function, you pass in getDataFunctionName exists,
10
+ * uses (or not uses an ID as input) and returns the data as Promise<Type>
11
+ * @param getDataFunctionName the function name on the apiService,
12
+ * that will be used as get api call
13
+ *
14
+ * @param successfulReloadCallBack you can create direct callback for successful reloads
15
+ * by passing it here
16
+ *
17
+ * @param getPostPreprocessLoad post processes can be implemented to ensure special conditions on data
18
+ *
19
+ * @param defaultValue you can set default value that will be used before any load
20
+ * @param noId this flag needs to be true, when using api function without id
21
+ *
22
+ * @example
23
+ * personalInformationDH = new DynamoDataHandler<PersonalInformation>({
24
+ * apiService: this.accountApiService,
25
+ * getDataFunctionName: 'getPersonalInformation',
26
+ * });
27
+ */
28
+ export class DyFM_DataHandler_Settings<
29
+ T extends DyFM_Metadata | DyFM_Metadata[],
30
+ T_Dependency extends DyFM_Metadata = any,
31
+ T_LoadBy = string
32
+ > {
33
+ name?: string;
34
+
35
+ /** this will be used to get the data from the API
36
+ * while creating arrow function for this, dont forget to use async n await! */
37
+ get?: (loadBy?: T_LoadBy) => Promise<any>;
38
+ /** this will be used to set the data to the data handler
39
+ * while creating arrow function for this, dont forget to use async n await! */
40
+ set?: (data: T) => Promise<T>;
41
+ /* patch?: (data: T) => Promise<T>; */
42
+
43
+ // TODO
44
+ /* directApiSetup?: { set, get,}; */
45
+ // AVAGY
46
+ /* apiCallSettings?: DyNX_ApiCall_Settings; */
47
+
48
+ /* successfulLoadCallBack?: (data: T) => void; */
49
+
50
+ /**
51
+ * post processes will be called as pipes after data is loaded by get function,
52
+ * but before data is set to data handler by data_$.set (after get, before Signal triggers)
53
+ */
54
+ getPostProcess?: DyFM_DataHandlerDataLoadPostProcess<T_LoadBy>;
55
+
56
+ dependencyDataHandler?: DyFM_DataHandler<T_Dependency>;
57
+ dependentDataHandlers?: DyFM_DataHandler<any>[];
58
+
59
+ defaultValue?: T;
60
+ defaultLoadBy?: T_LoadBy;
61
+ noId?: boolean;
62
+
63
+ debugLog?: boolean;
64
+ }
65
+
66
+ /**
67
+ * DyFM_DataListHandler_Settings
68
+ */
69
+ export class DyFM_DataListHandler_Settings<
70
+ T_DataItem extends DyFM_Metadata,
71
+ T_DependencyData extends DyFM_Metadata = any,
72
+ T_DataLoadBy = string,
73
+ T_UpdateItemBy = string,
74
+ > extends DyFM_DataHandler_Settings<T_DataItem[], T_DependencyData, T_DataLoadBy> {
75
+ /** this will be used to send the item update to the API
76
+ * while creating arrow function for this, dont forget to use async n await! */
77
+ setItem?: (item: T_DataItem, updateBy?: T_UpdateItemBy) => Promise<T_DataItem>;
78
+ /** this will be used to send the item delete to the API
79
+ * while creating arrow function for this, dont forget to use async n await! */
80
+ deleteItem?: (id: string, updateBy?: T_UpdateItemBy) => Promise<void>;
81
+ }
82
+
83
+ export class DyFM_DataSearchHandler_Settings<
84
+ T_DataItem extends DyFM_Metadata,
85
+ T_DataLoadBy extends DyFM_SearchQuery<any> = DyFM_SearchQuery<T_DataItem>,
86
+ /* T_Result extends DyFM_SearchResult<any> = DyFM_SearchResult<T_DataItem>, */
87
+ T_DependencyData extends DyFM_Metadata = any,
88
+ > extends DyFM_DataHandler_Settings<T_DataItem[], T_DependencyData, T_DataLoadBy> {
89
+ defaultQuery?: T_DataLoadBy;
90
+ defaultPageSize?: number;
91
+ cacheSize?: number;
92
+ queryUpdateDebounceTime?: number;
93
+ searchOnInit?: boolean;
94
+ }
95
+
96
+ export class DyFM_ListCollectorDataHandler_Settings<
97
+ T_ListCollectorData extends DyFM_Metadata,
98
+ T_DataItem extends DyFM_Metadata,
99
+ T_DependencyData extends DyFM_Metadata = any,
100
+ T_DataLoadBy = string,
101
+ > extends DyFM_DataHandler_Settings<T_ListCollectorData, T_DependencyData, T_DataLoadBy> {
102
+ /** this will be used to send the item update to the API
103
+ * while creating arrow function for this, dont forget to use async n await! */
104
+ setItem?: (item: T_DataItem, collectorId?: string) => Promise<T_DataItem>;
105
+ /** this will be used to send the item delete to the API
106
+ * while creating arrow function for this, dont forget to use async n await! */
107
+ deleteItem?: (id: string, collectorId?: string) => Promise<void>;
108
+
109
+ listKey: string;
110
+ }
@@ -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
+ }