@prestizni-software/client-dem 0.2.19 → 0.2.21
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/AutoUpdatedClientObjectClass.ts +45 -42
- package/CHANGELOG.md +4 -0
- package/package.json +1 -1
|
@@ -21,7 +21,7 @@ export type AutoUpdated<T extends Constructor<any>> =
|
|
|
21
21
|
export async function createAutoUpdatedClass<C extends Constructor<any>>(
|
|
22
22
|
classParam: C,
|
|
23
23
|
socket: SocketType,
|
|
24
|
-
data: IsData<InstanceType<C
|
|
24
|
+
data: RefToId<IsData<InstanceType<C>>> | string,
|
|
25
25
|
loggers: LoggersType,
|
|
26
26
|
autoClassers: { [key: string]: AutoUpdateManager<any> },
|
|
27
27
|
emitter: EventTarget
|
|
@@ -29,8 +29,6 @@ export async function createAutoUpdatedClass<C extends Constructor<any>>(
|
|
|
29
29
|
if (typeof data !== "string")
|
|
30
30
|
processIsRefProperties(data, classParam.prototype, undefined, [], loggers);
|
|
31
31
|
const props = Reflect.getMetadata("props", classParam.prototype);
|
|
32
|
-
if (typeof data !== "string")
|
|
33
|
-
checkForMissingRefs<C>(data, props, classParam, autoClassers);
|
|
34
32
|
const instance = new (class extends AutoUpdatedClientObject<C> {})(
|
|
35
33
|
socket,
|
|
36
34
|
data,
|
|
@@ -66,7 +64,6 @@ export abstract class AutoUpdatedClientObject<T extends Constructor<any>> {
|
|
|
66
64
|
protected isLoadingReferences = false;
|
|
67
65
|
public readonly classProp: Constructor<T>;
|
|
68
66
|
private readonly EmitterID = new ObjectId().toHexString();
|
|
69
|
-
protected unpopulatedData: RefToId<IsData<T>>;
|
|
70
67
|
private readonly loadShit = async () => {
|
|
71
68
|
if (this.isLoaded()) {
|
|
72
69
|
try {
|
|
@@ -124,7 +121,6 @@ export abstract class AutoUpdatedClientObject<T extends Constructor<any>> {
|
|
|
124
121
|
this.emitter.dispatchEvent(new Event("loaded" + this.EmitterID));
|
|
125
122
|
return;
|
|
126
123
|
}
|
|
127
|
-
this.unpopulatedData = res.data as any;
|
|
128
124
|
checkForMissingRefs<T>(
|
|
129
125
|
res.data as any,
|
|
130
126
|
properties,
|
|
@@ -137,18 +133,16 @@ export abstract class AutoUpdatedClientObject<T extends Constructor<any>> {
|
|
|
137
133
|
}
|
|
138
134
|
);
|
|
139
135
|
this.data = { _id: data } as IsData<T>;
|
|
140
|
-
this.unpopulatedData = { _id: data } as any;
|
|
141
136
|
} else {
|
|
142
137
|
this.isLoading = true;
|
|
143
138
|
checkForMissingRefs<T>(
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
139
|
+
data as any,
|
|
140
|
+
properties,
|
|
141
|
+
classProperty as any,
|
|
142
|
+
autoClassers
|
|
143
|
+
);
|
|
149
144
|
this.data = data as any;
|
|
150
145
|
|
|
151
|
-
this.unpopulatedData = data;
|
|
152
146
|
if (this.data._id === "") this.handleNewObject(data as any);
|
|
153
147
|
else this.isLoading = false;
|
|
154
148
|
}
|
|
@@ -169,13 +163,12 @@ export abstract class AutoUpdatedClientObject<T extends Constructor<any>> {
|
|
|
169
163
|
this.emitter.dispatchEvent(new Event("loaded" + this.EmitterID));
|
|
170
164
|
return;
|
|
171
165
|
}
|
|
172
|
-
this.unpopulatedData = res.data as any;
|
|
173
166
|
checkForMissingRefs<T>(
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
167
|
+
res.data as any,
|
|
168
|
+
this.properties,
|
|
169
|
+
this.classProp as any,
|
|
170
|
+
this.autoClassers
|
|
171
|
+
);
|
|
179
172
|
this.data = res.data as IsData<T>;
|
|
180
173
|
this.isLoading = false;
|
|
181
174
|
this.emitter.dispatchEvent(new Event("loaded" + this.EmitterID));
|
|
@@ -185,13 +178,12 @@ export abstract class AutoUpdatedClientObject<T extends Constructor<any>> {
|
|
|
185
178
|
public get extractedData(): {
|
|
186
179
|
[K in keyof InstanceType<T>]: InstanceOf<InstanceType<T>>[K];
|
|
187
180
|
} {
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
)
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
) as any as {
|
|
181
|
+
const extracted = Object.fromEntries(
|
|
182
|
+
Object.entries(
|
|
183
|
+
processIsRefProperties(this.data, this.classProp.prototype).newData
|
|
184
|
+
).filter(([k, v]) => typeof v !== "function")
|
|
185
|
+
);
|
|
186
|
+
return structuredClone(extracted) as any as {
|
|
195
187
|
[K in keyof InstanceType<T>]: InstanceOf<InstanceType<T>>[K];
|
|
196
188
|
};
|
|
197
189
|
}
|
|
@@ -250,7 +242,7 @@ export abstract class AutoUpdatedClientObject<T extends Constructor<any>> {
|
|
|
250
242
|
}
|
|
251
243
|
|
|
252
244
|
private generateSettersAndGetters() {
|
|
253
|
-
this.properties
|
|
245
|
+
for (const key of this.properties) {
|
|
254
246
|
if (typeof key !== "string") return;
|
|
255
247
|
|
|
256
248
|
const k = key as keyof IsData<T>;
|
|
@@ -261,8 +253,13 @@ export abstract class AutoUpdatedClientObject<T extends Constructor<any>> {
|
|
|
261
253
|
);
|
|
262
254
|
|
|
263
255
|
Object.defineProperty(this, key, {
|
|
264
|
-
get: () =>
|
|
265
|
-
|
|
256
|
+
get: () => {
|
|
257
|
+
if (isRef)
|
|
258
|
+
return typeof this.data[k] === "string"
|
|
259
|
+
? this.findReference(this.data[k] as string)
|
|
260
|
+
: this.data[k];
|
|
261
|
+
else return this.data[k];
|
|
262
|
+
},
|
|
266
263
|
set: () => {
|
|
267
264
|
throw new Error(
|
|
268
265
|
`Cannot set ${key} this way, use "setValue" function.`
|
|
@@ -271,7 +268,7 @@ export abstract class AutoUpdatedClientObject<T extends Constructor<any>> {
|
|
|
271
268
|
enumerable: true,
|
|
272
269
|
configurable: true,
|
|
273
270
|
});
|
|
274
|
-
}
|
|
271
|
+
}
|
|
275
272
|
}
|
|
276
273
|
|
|
277
274
|
protected findReference(id: string): AutoUpdated<any> | undefined {
|
|
@@ -431,8 +428,9 @@ export abstract class AutoUpdatedClientObject<T extends Constructor<any>> {
|
|
|
431
428
|
export function processIsRefProperties(
|
|
432
429
|
instance: any,
|
|
433
430
|
target: any,
|
|
434
|
-
prefix =
|
|
431
|
+
prefix: string | null = null,
|
|
435
432
|
allProps: string[] = [],
|
|
433
|
+
newData = {} as any,
|
|
436
434
|
loggers?: LoggersType
|
|
437
435
|
) {
|
|
438
436
|
const props: string[] = Reflect.getMetadata("props", target) || [];
|
|
@@ -440,27 +438,31 @@ export function processIsRefProperties(
|
|
|
440
438
|
for (const prop of props) {
|
|
441
439
|
const path = prefix ? `${prefix}.${prop}` : prop;
|
|
442
440
|
allProps.push(path);
|
|
441
|
+
newData[prop] = instance[prop];
|
|
443
442
|
if (Reflect.getMetadata("isRef", target, prop)) {
|
|
444
|
-
// 👇 here’s where you mutate
|
|
445
443
|
(loggers ?? console).debug("Changing isRef:", path);
|
|
446
|
-
|
|
447
|
-
// Example: replace with a proxy or a marker object
|
|
448
|
-
instance[prop] =
|
|
444
|
+
newData[prop] =
|
|
449
445
|
typeof instance[prop] === "string"
|
|
450
446
|
? instance[prop]
|
|
451
|
-
: instance[prop]
|
|
447
|
+
: instance[prop]?._id;
|
|
452
448
|
}
|
|
453
449
|
|
|
454
|
-
// recurse into nested objects
|
|
455
450
|
const type = Reflect.getMetadata("design:type", target, prop);
|
|
456
451
|
if (type?.prototype) {
|
|
457
452
|
const nestedProps = Reflect.getMetadata("props", type.prototype);
|
|
458
453
|
if (nestedProps && instance[prop]) {
|
|
459
|
-
|
|
454
|
+
newData[prop] = processIsRefProperties(
|
|
455
|
+
instance[prop],
|
|
456
|
+
type.prototype,
|
|
457
|
+
path,
|
|
458
|
+
allProps,
|
|
459
|
+
undefined,
|
|
460
|
+
loggers
|
|
461
|
+
).newData;
|
|
460
462
|
}
|
|
461
463
|
}
|
|
462
464
|
}
|
|
463
|
-
return allProps;
|
|
465
|
+
return { allProps, newData };
|
|
464
466
|
}
|
|
465
467
|
|
|
466
468
|
export function getMetadataRecursive(
|
|
@@ -499,15 +501,16 @@ function findMissingObjectReference(
|
|
|
499
501
|
prop: any,
|
|
500
502
|
autoClassers: { [key: string]: AutoUpdateManager<any> }
|
|
501
503
|
) {
|
|
502
|
-
for(const ac of Object.values(autoClassers)){
|
|
503
|
-
for(const obj of ac.objectsAsArray){
|
|
504
|
+
for (const ac of Object.values(autoClassers)) {
|
|
505
|
+
for (const obj of ac.objectsAsArray) {
|
|
504
506
|
const found = Object.values(obj.extractedData).find((value) =>
|
|
505
507
|
Array.isArray(value) ? value.includes(data._id) : value === data._id
|
|
506
|
-
)
|
|
507
|
-
if(found){
|
|
508
|
+
);
|
|
509
|
+
if (found) {
|
|
508
510
|
data[prop] = obj._id;
|
|
509
511
|
return;
|
|
510
512
|
}
|
|
511
513
|
}
|
|
512
514
|
}
|
|
515
|
+
console.log("a");
|
|
513
516
|
}
|
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,10 @@
|
|
|
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.21](https://github.com/Prestizni-Software/client-dem/compare/v0.2.20...v0.2.21) (2025-11-10)
|
|
6
|
+
|
|
7
|
+
### [0.2.20](https://github.com/Prestizni-Software/client-dem/compare/v0.2.19...v0.2.20) (2025-11-10)
|
|
8
|
+
|
|
5
9
|
### [0.2.19](https://github.com/Prestizni-Software/client-dem/compare/v0.2.18...v0.2.19) (2025-11-10)
|
|
6
10
|
|
|
7
11
|
### [0.2.18](https://github.com/Prestizni-Software/client-dem/compare/v0.2.17...v0.2.18) (2025-11-10)
|