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