@cascateer/core 2.3.28 → 2.3.30
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/package.json +1 -1
- package/src/serializable.ts +44 -19
- package/src/store.ts +4 -3
package/package.json
CHANGED
package/src/serializable.ts
CHANGED
|
@@ -25,23 +25,21 @@ export abstract class Serializable<O> {
|
|
|
25
25
|
SerializableConstructor<unknown, unknown>
|
|
26
26
|
> = {};
|
|
27
27
|
|
|
28
|
-
static fromJSON<T, O>(json: string): T {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
if (url === import.meta.url && pointer != null) {
|
|
34
|
-
const path = pointer.split("/");
|
|
35
|
-
|
|
36
|
-
if (path[0] === Serializable.name) {
|
|
37
|
-
return (
|
|
38
|
-
get(Serializable, path.slice(1)) as SerializableConstructor<T, O>
|
|
39
|
-
).fromObject(value);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
} catch {}
|
|
28
|
+
static async fromJSON<T, O>(json: string): Promise<T> {
|
|
29
|
+
const { $ref, value }: SerializerResult<O> = JSON.parse(json),
|
|
30
|
+
[url, path] = $ref.split(/#\/?/);
|
|
31
|
+
|
|
32
|
+
console.log("fromJSON", url, this);
|
|
43
33
|
|
|
44
|
-
|
|
34
|
+
if (url != null && path != null) {
|
|
35
|
+
return import(url).then((module) =>
|
|
36
|
+
(
|
|
37
|
+
get(module, path.split("/")) as SerializableConstructor<T, O>
|
|
38
|
+
).fromObject(value),
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
throw new Error(`${json} deserialization failed`);
|
|
45
43
|
}
|
|
46
44
|
|
|
47
45
|
static toJSON<T, O>(
|
|
@@ -53,15 +51,42 @@ export abstract class Serializable<O> {
|
|
|
53
51
|
|
|
54
52
|
this[importMap][id] = ctor;
|
|
55
53
|
|
|
54
|
+
console.log("toJSON", import.meta.url, this);
|
|
55
|
+
|
|
56
56
|
return identity<BrandedSerializer<O>>(() => ({
|
|
57
57
|
value: value.toObject(),
|
|
58
58
|
$ref: [`${import.meta.url}#`, this.name, importMap, id].join("/"),
|
|
59
59
|
}));
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
static parse(text: string) {
|
|
63
|
-
|
|
64
|
-
|
|
62
|
+
static async parse(text: string) {
|
|
63
|
+
const nodes: { key: string; value: any; parent?: any }[] = [];
|
|
64
|
+
const obj = JSON.parse(text, (key, value) => {
|
|
65
|
+
for (const node of nodes) {
|
|
66
|
+
if (value[node.key] === node.value) {
|
|
67
|
+
node.parent = value;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
nodes.push({ key, value });
|
|
72
|
+
|
|
73
|
+
return value;
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
return nodes.reduce(
|
|
77
|
+
(value, node) =>
|
|
78
|
+
value.then(() =>
|
|
79
|
+
Serializable.fromJSON(JSON.stringify(node.value))
|
|
80
|
+
.catch(() => node.value)
|
|
81
|
+
.then((value) => {
|
|
82
|
+
if (node.parent != null) {
|
|
83
|
+
node.parent[node.key] = value;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return value;
|
|
87
|
+
}),
|
|
88
|
+
),
|
|
89
|
+
Promise.resolve(obj),
|
|
65
90
|
);
|
|
66
91
|
}
|
|
67
92
|
|
package/src/store.ts
CHANGED
|
@@ -171,14 +171,15 @@ export class StoreProvider<Data> extends ExtendableStoreAdapter<
|
|
|
171
171
|
>();
|
|
172
172
|
const seedActions: Observable<MulticastAction<Data, "seedAction">> =
|
|
173
173
|
actions.pipe(
|
|
174
|
-
|
|
174
|
+
mergeMap(async (event) =>
|
|
175
175
|
event.type === "seedAction"
|
|
176
176
|
? {
|
|
177
177
|
...event,
|
|
178
|
-
predicate: constant(Serializable.parse(event.data.seed)),
|
|
178
|
+
predicate: constant(await Serializable.parse(event.data.seed)),
|
|
179
179
|
}
|
|
180
180
|
: [],
|
|
181
181
|
),
|
|
182
|
+
flatMap(identity),
|
|
182
183
|
);
|
|
183
184
|
|
|
184
185
|
const callbacks = new Map<string, UnaryFunction<unknown, void>>();
|
|
@@ -212,7 +213,7 @@ export class StoreProvider<Data> extends ExtendableStoreAdapter<
|
|
|
212
213
|
return {
|
|
213
214
|
...event,
|
|
214
215
|
predicate: transform(
|
|
215
|
-
Serializable.parse(event.data.args ?? null),
|
|
216
|
+
await Serializable.parse(event.data.args ?? null),
|
|
216
217
|
),
|
|
217
218
|
callback: callbacks.get(event.id),
|
|
218
219
|
};
|