@prestizni-software/client-dem 0.2.58 → 0.2.59
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AutoUpdateClientManagerClass.ts +161 -0
- package/AutoUpdateManagerClass.ts +87 -0
- package/AutoUpdatedClientObjectClass.ts +674 -0
- package/CHANGELOG.md +2 -0
- package/CommonTypes.ts +105 -0
- package/client.ts +11 -0
- package/{client.js → dist/client.js} +1 -1
- package/{client.js.map → dist/client.js.map} +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +12 -7
- package/tsconfig.json +113 -0
- /package/{AutoUpdateClientManagerClass.d.ts → dist/AutoUpdateClientManagerClass.d.ts} +0 -0
- /package/{AutoUpdateClientManagerClass.js → dist/AutoUpdateClientManagerClass.js} +0 -0
- /package/{AutoUpdateClientManagerClass.js.map → dist/AutoUpdateClientManagerClass.js.map} +0 -0
- /package/{AutoUpdateManagerClass.d.ts → dist/AutoUpdateManagerClass.d.ts} +0 -0
- /package/{AutoUpdateManagerClass.js → dist/AutoUpdateManagerClass.js} +0 -0
- /package/{AutoUpdateManagerClass.js.map → dist/AutoUpdateManagerClass.js.map} +0 -0
- /package/{AutoUpdatedClientObjectClass.d.ts → dist/AutoUpdatedClientObjectClass.d.ts} +0 -0
- /package/{AutoUpdatedClientObjectClass.js → dist/AutoUpdatedClientObjectClass.js} +0 -0
- /package/{AutoUpdatedClientObjectClass.js.map → dist/AutoUpdatedClientObjectClass.js.map} +0 -0
- /package/{CommonTypes.d.ts → dist/CommonTypes.d.ts} +0 -0
- /package/{CommonTypes.js → dist/CommonTypes.js} +0 -0
- /package/{CommonTypes.js.map → dist/CommonTypes.js.map} +0 -0
- /package/{client.d.ts → dist/client.d.ts} +0 -0
|
@@ -0,0 +1,674 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
import {
|
|
3
|
+
Constructor,
|
|
4
|
+
EventEmitter3,
|
|
5
|
+
InstanceOf,
|
|
6
|
+
IsData,
|
|
7
|
+
LoggersType,
|
|
8
|
+
LoggersTypeInternal,
|
|
9
|
+
Paths,
|
|
10
|
+
PathValueOf,
|
|
11
|
+
ServerResponse,
|
|
12
|
+
ServerUpdateRequest,
|
|
13
|
+
} from "./CommonTypes.js";
|
|
14
|
+
import { AutoUpdateManager } from "./AutoUpdateManagerClass.js";
|
|
15
|
+
import { ObjectId } from "bson";
|
|
16
|
+
import { AutoUpdateClientManager } from "./AutoUpdateClientManagerClass.js";
|
|
17
|
+
import { Socket } from "socket.io-client";
|
|
18
|
+
type SocketType = Socket<any, any>;
|
|
19
|
+
export type AutoUpdated<T extends Constructor<any>> =
|
|
20
|
+
AutoUpdatedClientObject<T> & InstanceOf<T>;
|
|
21
|
+
export async function createAutoUpdatedClass<C extends Constructor<any>>(
|
|
22
|
+
classParam: C,
|
|
23
|
+
socket: SocketType,
|
|
24
|
+
data: IsData<InstanceType<C>> | string,
|
|
25
|
+
loggers: LoggersType,
|
|
26
|
+
autoClassers: AutoUpdateClientManager<any>,
|
|
27
|
+
emitter: EventEmitter3,
|
|
28
|
+
token: string
|
|
29
|
+
): Promise<AutoUpdated<C>> {
|
|
30
|
+
if (typeof data !== "string") {
|
|
31
|
+
checkForMissingRefs<C>(data as any, [], classParam, autoClassers);
|
|
32
|
+
processIsRefProperties(data, classParam.prototype, undefined, [], loggers);
|
|
33
|
+
}
|
|
34
|
+
const props = Reflect.getMetadata("props", classParam.prototype);
|
|
35
|
+
const instance = new (class extends AutoUpdatedClientObject<C> {})(
|
|
36
|
+
socket,
|
|
37
|
+
data,
|
|
38
|
+
loggers,
|
|
39
|
+
props,
|
|
40
|
+
classParam.name,
|
|
41
|
+
classParam,
|
|
42
|
+
autoClassers,
|
|
43
|
+
emitter,
|
|
44
|
+
token
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
await instance.isLoadedAsync();
|
|
48
|
+
|
|
49
|
+
return instance as any;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export abstract class AutoUpdatedClientObject<T extends Constructor<any>> {
|
|
53
|
+
protected readonly socket: SocketType;
|
|
54
|
+
//protected updates: string[] = [];
|
|
55
|
+
protected data: IsData<T>;
|
|
56
|
+
protected readonly isServer: boolean = false;
|
|
57
|
+
protected readonly loggers: LoggersTypeInternal = {
|
|
58
|
+
info: () => {},
|
|
59
|
+
debug: () => {},
|
|
60
|
+
error: () => {},
|
|
61
|
+
warn: () => {},
|
|
62
|
+
};
|
|
63
|
+
protected isLoading = false;
|
|
64
|
+
protected readonly emitter: EventEmitter3;
|
|
65
|
+
protected readonly properties: (keyof T)[];
|
|
66
|
+
protected readonly className: string;
|
|
67
|
+
protected autoClasser: AutoUpdateManager<any>;
|
|
68
|
+
protected isLoadingReferences = false;
|
|
69
|
+
public readonly classProp: Constructor<T>;
|
|
70
|
+
private readonly EmitterID = new ObjectId().toHexString();
|
|
71
|
+
private readonly token: string;
|
|
72
|
+
private readonly loadShit = async () => {
|
|
73
|
+
if (this.isLoaded()) {
|
|
74
|
+
try {
|
|
75
|
+
await this.loadForceReferences();
|
|
76
|
+
} catch (error) {
|
|
77
|
+
this.loggers.error(error);
|
|
78
|
+
}
|
|
79
|
+
this.isLoadingReferences = false;
|
|
80
|
+
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
this.emitter.on("loaded" + this.EmitterID, async () => {
|
|
84
|
+
try {
|
|
85
|
+
await this.loadForceReferences();
|
|
86
|
+
} catch (error) {
|
|
87
|
+
this.loggers.error(error);
|
|
88
|
+
}
|
|
89
|
+
this.isLoadingReferences = false;
|
|
90
|
+
});
|
|
91
|
+
};
|
|
92
|
+
constructor(
|
|
93
|
+
socket: SocketType,
|
|
94
|
+
data: string | IsData<T>,
|
|
95
|
+
loggers: LoggersType,
|
|
96
|
+
properties: (keyof T)[],
|
|
97
|
+
className: string,
|
|
98
|
+
classProperty: Constructor<T>,
|
|
99
|
+
autoClasser: AutoUpdateManager<any>,
|
|
100
|
+
emitter: EventEmitter3,
|
|
101
|
+
token: string = ""
|
|
102
|
+
) {
|
|
103
|
+
this.token = token;
|
|
104
|
+
this.emitter = emitter;
|
|
105
|
+
this.classProp = classProperty;
|
|
106
|
+
this.isLoadingReferences = true;
|
|
107
|
+
this.isLoading = true;
|
|
108
|
+
this.autoClasser = autoClasser;
|
|
109
|
+
this.className = className;
|
|
110
|
+
this.properties = properties;
|
|
111
|
+
this.loggers.debug = loggers.debug;
|
|
112
|
+
this.loggers.info = loggers.info;
|
|
113
|
+
this.loggers.error = loggers.error;
|
|
114
|
+
this.loggers.warn = loggers.warn ?? loggers.info;
|
|
115
|
+
this.socket = socket;
|
|
116
|
+
if (typeof data === "string") {
|
|
117
|
+
if (data === "")
|
|
118
|
+
throw new Error(
|
|
119
|
+
"Cannot create a new AutoUpdatedClientClass with an empty string for ID."
|
|
120
|
+
);
|
|
121
|
+
this.socket.emit(
|
|
122
|
+
"get" + this.className + data,
|
|
123
|
+
null,
|
|
124
|
+
(res: ServerResponse<T>) => {
|
|
125
|
+
if (!res.success) {
|
|
126
|
+
this.isLoading = false;
|
|
127
|
+
this.loggers.error("Could not load data from server:", res.message);
|
|
128
|
+
this.emitter.emit("loaded" + this.EmitterID);
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
checkForMissingRefs<T>(
|
|
132
|
+
res.data as any,
|
|
133
|
+
properties,
|
|
134
|
+
classProperty as any,
|
|
135
|
+
autoClasser
|
|
136
|
+
);
|
|
137
|
+
this.data = res.data as IsData<T>;
|
|
138
|
+
this.isLoading = false;
|
|
139
|
+
this.emitter.emit("loaded" + this.EmitterID);
|
|
140
|
+
}
|
|
141
|
+
);
|
|
142
|
+
this.data = { _id: data } as IsData<T>;
|
|
143
|
+
} else {
|
|
144
|
+
this.isLoading = true;
|
|
145
|
+
checkForMissingRefs<T>(
|
|
146
|
+
data as any,
|
|
147
|
+
properties,
|
|
148
|
+
classProperty as any,
|
|
149
|
+
autoClasser
|
|
150
|
+
);
|
|
151
|
+
this.data = data as any;
|
|
152
|
+
|
|
153
|
+
if (!this.data._id || this.data._id === "") this.handleNewObject(data as any);
|
|
154
|
+
else {
|
|
155
|
+
this.isLoading = false;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
if (!this.isServer) this.openSockets();
|
|
159
|
+
this.generateSettersAndGetters();
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
protected handleNewObject(data: IsData<T>) {
|
|
163
|
+
this.isLoading = true;
|
|
164
|
+
if (!this.className)
|
|
165
|
+
throw new Error(
|
|
166
|
+
"Cannot create a new AutoUpdatedClientClass without a class name."
|
|
167
|
+
);
|
|
168
|
+
this.socket.emit("new" + this.className, data, (res: ServerResponse<T>) => {
|
|
169
|
+
if (!res.success) {
|
|
170
|
+
this.isLoading = false;
|
|
171
|
+
this.loggers.error("Could not create data on server:", res.message);
|
|
172
|
+
this.emitter.emit("loaded" + this.EmitterID);
|
|
173
|
+
throw new Error("Error creating new object: " + res.message);
|
|
174
|
+
}
|
|
175
|
+
checkForMissingRefs<T>(
|
|
176
|
+
res.data as any,
|
|
177
|
+
this.properties,
|
|
178
|
+
this.classProp as any,
|
|
179
|
+
this.autoClasser
|
|
180
|
+
);
|
|
181
|
+
this.data = res.data as IsData<T>;
|
|
182
|
+
this.isLoading = false;
|
|
183
|
+
this.emitter.emit("loaded" + this.EmitterID);
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
public get extractedData(): {
|
|
188
|
+
[K in keyof InstanceType<T>]: InstanceOf<InstanceType<T>>[K];
|
|
189
|
+
} {
|
|
190
|
+
const extracted = processIsRefProperties(
|
|
191
|
+
this.data,
|
|
192
|
+
this.classProp.prototype,
|
|
193
|
+
null,
|
|
194
|
+
[],
|
|
195
|
+
{},
|
|
196
|
+
this.loggers
|
|
197
|
+
).newData;
|
|
198
|
+
|
|
199
|
+
return structuredClone(extracted) as {
|
|
200
|
+
[K in keyof InstanceType<T>]: InstanceOf<InstanceType<T>>[K];
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
public isLoaded(): boolean {
|
|
205
|
+
return !this.isLoading;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
public async isLoadedAsync(): Promise<boolean> {
|
|
209
|
+
await this.loadShit();
|
|
210
|
+
return this.isLoading
|
|
211
|
+
? new Promise((resolve) => {
|
|
212
|
+
this.emitter.on("loaded" + this.EmitterID, async () => {
|
|
213
|
+
resolve(this.isLoading === false);
|
|
214
|
+
});
|
|
215
|
+
})
|
|
216
|
+
: true;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
private openSockets() {
|
|
220
|
+
this.loggers.debug(`[${this.data._id}] Opening socket listeners`);
|
|
221
|
+
|
|
222
|
+
this.socket.on(
|
|
223
|
+
"update" + this.className + this.data._id.toString(),
|
|
224
|
+
async (update: ServerUpdateRequest<T>) => {
|
|
225
|
+
await this.handleUpdateRequest(update);
|
|
226
|
+
}
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
private async handleUpdateRequest(
|
|
231
|
+
update: ServerUpdateRequest<T>
|
|
232
|
+
): Promise<ServerResponse<undefined>> {
|
|
233
|
+
try {
|
|
234
|
+
const path = update.key.split(".");
|
|
235
|
+
let dataRef: any = this.data;
|
|
236
|
+
for (let i = 0; i < path.length - 1; i++) {
|
|
237
|
+
if (!dataRef[path[i]]) dataRef[path[i]] = {};
|
|
238
|
+
dataRef = dataRef[path[i]];
|
|
239
|
+
}
|
|
240
|
+
dataRef[path.at(-1)!] = update.value;
|
|
241
|
+
|
|
242
|
+
this.loggers.debug(
|
|
243
|
+
`[${this.data._id}] Applied patch ${update.key} set to ${update.value}`
|
|
244
|
+
);
|
|
245
|
+
|
|
246
|
+
// Return success with the applied patch
|
|
247
|
+
return { success: true, data: undefined, message: "" };
|
|
248
|
+
} catch (error) {
|
|
249
|
+
this.loggers.error(`[${this.data._id}] Error applying patch:`, error);
|
|
250
|
+
return {
|
|
251
|
+
success: false,
|
|
252
|
+
message: "Error applying update: " + (error as Error).message,
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
private generateSettersAndGetters() {
|
|
258
|
+
for (const key of this.properties) {
|
|
259
|
+
if (typeof key !== "string") return;
|
|
260
|
+
|
|
261
|
+
const k = key as keyof IsData<T>;
|
|
262
|
+
const isRef = getMetadataRecursive(
|
|
263
|
+
"isRef",
|
|
264
|
+
this.classProp.prototype,
|
|
265
|
+
key
|
|
266
|
+
);
|
|
267
|
+
|
|
268
|
+
Object.defineProperty(this, key, {
|
|
269
|
+
get: () => {
|
|
270
|
+
if (isRef)
|
|
271
|
+
return typeof this.data[k] === "string"
|
|
272
|
+
? this.findReference(this.data[k] as string)
|
|
273
|
+
: this.data[k];
|
|
274
|
+
else return this.data[k];
|
|
275
|
+
},
|
|
276
|
+
set: () => {
|
|
277
|
+
throw new Error(
|
|
278
|
+
`Cannot set ${key} this way, use "setValue" function.`
|
|
279
|
+
);
|
|
280
|
+
},
|
|
281
|
+
enumerable: true,
|
|
282
|
+
configurable: true,
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
protected findReference(id: string): AutoUpdated<any> | undefined {
|
|
288
|
+
for (const classer of Object.values(this.autoClasser.classers)) {
|
|
289
|
+
const result = classer.getObject(id);
|
|
290
|
+
if (result) return result;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
public setValue<K extends Paths<InstanceOf<T>>>(
|
|
295
|
+
key: K,
|
|
296
|
+
val: PathValueOf<T, K>
|
|
297
|
+
): Promise<{ success: boolean; msg: string }> {
|
|
298
|
+
return this.setValue__(key, val);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
protected async setValue__(
|
|
302
|
+
key: any,
|
|
303
|
+
val: any
|
|
304
|
+
): Promise<{ success: boolean; msg: string }> {
|
|
305
|
+
let message = "Setting value " + key + " of " + this.className;
|
|
306
|
+
let value = val;
|
|
307
|
+
this.loggers.debug(
|
|
308
|
+
`[${(this.data as any)._id}] Setting ${key} to ${value}`
|
|
309
|
+
);
|
|
310
|
+
try {
|
|
311
|
+
if (value instanceof AutoUpdatedClientObject)
|
|
312
|
+
value = (value as any).extractedData._id;
|
|
313
|
+
const path = key.split(".");
|
|
314
|
+
let obj = this.data as any;
|
|
315
|
+
let lastClass = this as any;
|
|
316
|
+
let lastPath = key as string;
|
|
317
|
+
for (let i = 0; i < path.length - 1; i++) {
|
|
318
|
+
if (
|
|
319
|
+
typeof obj[path[i]] !== "object" ||
|
|
320
|
+
obj[path[i]] instanceof ObjectId
|
|
321
|
+
) {
|
|
322
|
+
let temp;
|
|
323
|
+
try {
|
|
324
|
+
temp = this.resolveReference(
|
|
325
|
+
obj[path[i]]?.toString()
|
|
326
|
+
) as AutoUpdated<any>;
|
|
327
|
+
} catch (error: any) {
|
|
328
|
+
message +=
|
|
329
|
+
"\n Error: likely undefined property on path: " +
|
|
330
|
+
path +
|
|
331
|
+
" on index: " +
|
|
332
|
+
i +
|
|
333
|
+
" with error: " +
|
|
334
|
+
error.message;
|
|
335
|
+
}
|
|
336
|
+
if (!temp) {
|
|
337
|
+
message +=
|
|
338
|
+
"\nLikely undefined property " +
|
|
339
|
+
path[i] +
|
|
340
|
+
" on path: " +
|
|
341
|
+
path +
|
|
342
|
+
" at index: " +
|
|
343
|
+
i;
|
|
344
|
+
this.loggers.warn(
|
|
345
|
+
"Failed to set value for " + this.className + "\n" + message
|
|
346
|
+
);
|
|
347
|
+
return {
|
|
348
|
+
success: false,
|
|
349
|
+
msg: message,
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
lastClass = temp;
|
|
353
|
+
lastPath = path.slice(i + 1).join(".");
|
|
354
|
+
return await lastClass.setValue(lastPath, value);
|
|
355
|
+
} else obj = obj[path[i]];
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
if (lastClass !== this || lastPath !== (key as any)) {
|
|
359
|
+
message +=
|
|
360
|
+
"\n What the actual fuckity fuck error on path: " +
|
|
361
|
+
path +
|
|
362
|
+
" on index: " +
|
|
363
|
+
(path.length - 1);
|
|
364
|
+
this.loggers.error(
|
|
365
|
+
"Failed to set value for " + this.className + "\n" + message
|
|
366
|
+
);
|
|
367
|
+
return {
|
|
368
|
+
success: false,
|
|
369
|
+
msg: message,
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
let success;
|
|
374
|
+
try {
|
|
375
|
+
const res = await this.setValueInternal(lastPath, value);
|
|
376
|
+
success = res.success;
|
|
377
|
+
message += "\nReport from inner setValue function: \n " + res.message;
|
|
378
|
+
} catch (error: any) {
|
|
379
|
+
success = false;
|
|
380
|
+
message += "\nError from inner setValue function: \n " + error.message;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
if (!success) {
|
|
384
|
+
this.loggers.warn(
|
|
385
|
+
"Failed to set value for " + this.className + "\n" + message
|
|
386
|
+
);
|
|
387
|
+
return { success: false, msg: message };
|
|
388
|
+
}
|
|
389
|
+
const pathArr = lastPath.split(".");
|
|
390
|
+
if (pathArr.length === 1) {
|
|
391
|
+
(this.data as any)[key as any] = value;
|
|
392
|
+
await this.checkAutoStatusChange();
|
|
393
|
+
return {
|
|
394
|
+
success: true,
|
|
395
|
+
msg: "Successfully set " + key + " to " + value,
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
const pathMinusLast = pathArr.splice(0, 1);
|
|
399
|
+
let ref = this as any;
|
|
400
|
+
for (const p of pathMinusLast) {
|
|
401
|
+
ref = ref[p];
|
|
402
|
+
}
|
|
403
|
+
ref[pathArr.at(-1)!] = value;
|
|
404
|
+
await this.checkAutoStatusChange();
|
|
405
|
+
return {
|
|
406
|
+
success: true,
|
|
407
|
+
msg: "Successfully set " + key + " to " + value,
|
|
408
|
+
};
|
|
409
|
+
} catch (error: any) {
|
|
410
|
+
this.loggers.error(
|
|
411
|
+
"An error occurred setting value for " +
|
|
412
|
+
this.className +
|
|
413
|
+
"\n" +
|
|
414
|
+
message +
|
|
415
|
+
"\n Random error here: " +
|
|
416
|
+
error.message +
|
|
417
|
+
"\n" +
|
|
418
|
+
error.stack
|
|
419
|
+
);
|
|
420
|
+
this.loggers.error(error);
|
|
421
|
+
return {
|
|
422
|
+
success: false,
|
|
423
|
+
msg:
|
|
424
|
+
message +
|
|
425
|
+
"\n Random error here: " +
|
|
426
|
+
error.message +
|
|
427
|
+
"\n" +
|
|
428
|
+
error.stack,
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
public getValue(key: Paths<T>) {
|
|
434
|
+
let value: any;
|
|
435
|
+
|
|
436
|
+
for (const part of key.split(".")) {
|
|
437
|
+
try {
|
|
438
|
+
if (value) value = value[part];
|
|
439
|
+
else value = (this.data as any)[part];
|
|
440
|
+
} catch (error: any) {
|
|
441
|
+
this.loggers.error(
|
|
442
|
+
"Error getting value for " +
|
|
443
|
+
this.className +
|
|
444
|
+
" on key " +
|
|
445
|
+
key +
|
|
446
|
+
" on index " +
|
|
447
|
+
part +
|
|
448
|
+
":",
|
|
449
|
+
error.message
|
|
450
|
+
);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
return value;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
protected async setValueInternal(
|
|
457
|
+
key: string,
|
|
458
|
+
value: any
|
|
459
|
+
): Promise<{ success: boolean; message: string }> {
|
|
460
|
+
const update: ServerUpdateRequest<T> = this.makeUpdate(key, value);
|
|
461
|
+
const promise = new Promise<{ success: boolean; message: string }>(
|
|
462
|
+
(resolve) => {
|
|
463
|
+
try {
|
|
464
|
+
this.socket.emit(
|
|
465
|
+
"update" + this.className + this.data._id,
|
|
466
|
+
update,
|
|
467
|
+
(res: ServerResponse<T>) => {
|
|
468
|
+
resolve({ success: res.success, message: res.message });
|
|
469
|
+
}
|
|
470
|
+
);
|
|
471
|
+
} catch (error: any) {
|
|
472
|
+
this.loggers.error("Error sending update:", error);
|
|
473
|
+
resolve({ success: false, message: error.message });
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
);
|
|
477
|
+
return promise;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
protected makeUpdate(key: string, value: any): any {
|
|
481
|
+
try {
|
|
482
|
+
const id = this.data._id.toString();
|
|
483
|
+
return { _id: id, key, value } as any;
|
|
484
|
+
} catch (error: any) {
|
|
485
|
+
this.loggers.error(
|
|
486
|
+
"Probably missing the fucking identifier ['_id'] again " + error.message
|
|
487
|
+
);
|
|
488
|
+
throw error;
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
protected async checkAutoStatusChange() {
|
|
493
|
+
return;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
// return a properly typed AutoUpdatedClientClass (or null)
|
|
497
|
+
// inside AutoUpdatedClientClass
|
|
498
|
+
protected resolveReference(id: string): AutoUpdatedClientObject<any> | null {
|
|
499
|
+
if (!this.autoClasser) throw new Error("No autoClasser");
|
|
500
|
+
for (const autoClasser of Object.values(this.autoClasser.classers)) {
|
|
501
|
+
const data = autoClasser.getObject(id);
|
|
502
|
+
if (data) return data;
|
|
503
|
+
}
|
|
504
|
+
return null;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
private async loadForceReferences(
|
|
508
|
+
obj: any = this.data,
|
|
509
|
+
proto: any = this.classProp.prototype
|
|
510
|
+
) {
|
|
511
|
+
const props: string[] = Reflect.getMetadata("props", proto) || [];
|
|
512
|
+
|
|
513
|
+
for (const key of props) {
|
|
514
|
+
const isRef = Reflect.getMetadata("isRef", proto, key);
|
|
515
|
+
|
|
516
|
+
if (isRef) {
|
|
517
|
+
await this.handleLoadOnForced(obj, key);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
// If the property itself is a nested object, check deeper
|
|
521
|
+
if (obj[key] && typeof obj[key] === "object") {
|
|
522
|
+
const nestedProto = Object.getPrototypeOf(obj[key]);
|
|
523
|
+
if (nestedProto) {
|
|
524
|
+
await this.loadForceReferences(obj[key], nestedProto);
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
private async handleLoadOnForced(obj: any, key: string) {
|
|
531
|
+
if (!this.autoClasser) throw new Error("No autoClassers");
|
|
532
|
+
const refId = obj[key];
|
|
533
|
+
if (refId) {
|
|
534
|
+
for (const classer of Object.values(this.autoClasser.classers)) {
|
|
535
|
+
const result = classer.getObject(refId);
|
|
536
|
+
if (result) {
|
|
537
|
+
obj[key] = result;
|
|
538
|
+
|
|
539
|
+
// Recursively load refs inside the resolved object
|
|
540
|
+
if (typeof result.loadForceReferences === "function") {
|
|
541
|
+
await result.loadForceReferences();
|
|
542
|
+
}
|
|
543
|
+
break;
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
public async destroy(): Promise<void> {
|
|
549
|
+
this.socket.emit("delete" + this.className, this.data._id);
|
|
550
|
+
|
|
551
|
+
this.socket.removeAllListeners("update" + this.className + this.data._id);
|
|
552
|
+
this.socket.removeAllListeners("delete" + this.className + this.data._id);
|
|
553
|
+
this.wipeSelf();
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
protected wipeSelf() {
|
|
557
|
+
for (const key of Object.keys(this.data)) {
|
|
558
|
+
delete (this.data as any)[key];
|
|
559
|
+
}
|
|
560
|
+
this.loggers.info(`[${this.data._id}] ${this.className} object wiped`);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
export function processIsRefProperties(
|
|
565
|
+
instance: any,
|
|
566
|
+
target: any,
|
|
567
|
+
prefix: string | null = null,
|
|
568
|
+
allProps: string[] = [],
|
|
569
|
+
newData = {} as any,
|
|
570
|
+
loggers = console as LoggersType
|
|
571
|
+
) {
|
|
572
|
+
const props: string[] = Reflect.getMetadata("props", target) || [];
|
|
573
|
+
|
|
574
|
+
for (const prop of props) {
|
|
575
|
+
const path = prefix ? `${prefix}.${prop}` : prop;
|
|
576
|
+
allProps.push(path);
|
|
577
|
+
newData[prop] = ObjectId.isValid(instance[prop])
|
|
578
|
+
? instance[prop]?.toString()
|
|
579
|
+
: instance[prop];
|
|
580
|
+
if (Reflect.getMetadata("isRef", target, prop)) {
|
|
581
|
+
if (Array.isArray(instance[prop]))
|
|
582
|
+
newData[prop] = instance[prop].map((item: any) =>
|
|
583
|
+
typeof item === "string"
|
|
584
|
+
? item
|
|
585
|
+
: ObjectId.isValid(item)
|
|
586
|
+
? item?.toString()
|
|
587
|
+
: item?._id
|
|
588
|
+
);
|
|
589
|
+
else
|
|
590
|
+
newData[prop] =
|
|
591
|
+
typeof instance[prop] === "string"
|
|
592
|
+
? instance[prop]
|
|
593
|
+
: ObjectId.isValid(instance[prop])
|
|
594
|
+
? instance[prop]?.toString()
|
|
595
|
+
: instance[prop]?._id;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
const type = Reflect.getMetadata("design:type", target, prop);
|
|
599
|
+
if (type?.prototype) {
|
|
600
|
+
const nestedProps = Reflect.getMetadata("props", type.prototype);
|
|
601
|
+
if (nestedProps && instance[prop]) {
|
|
602
|
+
newData[prop] = processIsRefProperties(
|
|
603
|
+
instance[prop],
|
|
604
|
+
type.prototype,
|
|
605
|
+
path,
|
|
606
|
+
allProps,
|
|
607
|
+
undefined,
|
|
608
|
+
loggers
|
|
609
|
+
).newData;
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
return { allProps, newData };
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
export function getMetadataRecursive(
|
|
617
|
+
metaKey: string,
|
|
618
|
+
proto: any,
|
|
619
|
+
prop: string
|
|
620
|
+
) {
|
|
621
|
+
while (proto) {
|
|
622
|
+
const meta = Reflect.getMetadata(metaKey, proto, prop);
|
|
623
|
+
if (meta) return meta;
|
|
624
|
+
proto = Object.getPrototypeOf(proto);
|
|
625
|
+
}
|
|
626
|
+
return undefined;
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
function checkForMissingRefs<C extends Constructor<any>>(
|
|
630
|
+
data: IsData<InstanceType<C>>,
|
|
631
|
+
props: any,
|
|
632
|
+
classParam: C,
|
|
633
|
+
autoClassers: AutoUpdateManager<any>
|
|
634
|
+
) {
|
|
635
|
+
if (typeof data !== "string") {
|
|
636
|
+
const entryKeys = Object.keys(data);
|
|
637
|
+
for (const prop of props) {
|
|
638
|
+
const pointer = getMetadataRecursive(
|
|
639
|
+
"isRef",
|
|
640
|
+
classParam.prototype,
|
|
641
|
+
prop.toString()
|
|
642
|
+
);
|
|
643
|
+
if (!entryKeys.includes(prop.toString()) && pointer) {
|
|
644
|
+
findMissingObjectReference(data, prop, autoClassers.classers, pointer);
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
function findMissingObjectReference(
|
|
650
|
+
data: any,
|
|
651
|
+
prop: any,
|
|
652
|
+
autoClassers: { [key: string]: AutoUpdateManager<any> },
|
|
653
|
+
acName: string
|
|
654
|
+
) {
|
|
655
|
+
let foundAnAC = false;
|
|
656
|
+
for (const ac of Object.values(autoClassers)) {
|
|
657
|
+
if (ac.className !== acName) {
|
|
658
|
+
continue;
|
|
659
|
+
}
|
|
660
|
+
foundAnAC = true;
|
|
661
|
+
for (const obj of ac.objectsAsArray) {
|
|
662
|
+
const found = Object.values(obj.extractedData).find((value) =>
|
|
663
|
+
Array.isArray(value) ? value.includes(data._id) : value === data._id
|
|
664
|
+
);
|
|
665
|
+
if (found) {
|
|
666
|
+
data[prop] = obj._id;
|
|
667
|
+
return;
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
if (!foundAnAC) {
|
|
672
|
+
throw new Error(`No AutoUpdateManager found for class ${acName}`);
|
|
673
|
+
}
|
|
674
|
+
}
|
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
### [0.2.59](https://github.com/Prestizni-Software/client-dem/compare/v0.2.58...v0.2.59) (2025-11-21)
|
|
6
|
+
|
|
5
7
|
### [0.2.58](https://github.com/Prestizni-Software/client-dem/compare/v0.2.57...v0.2.58) (2025-11-21)
|
|
6
8
|
|
|
7
9
|
### [0.2.57](https://github.com/Prestizni-Software/client-dem/compare/v0.2.56...v0.2.57) (2025-11-20)
|