@cascateer/core 2.3.23 → 2.3.25
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 -40
- package/src/store.ts +4 -5
- package/src/serializable.spec.ts +0 -32
package/package.json
CHANGED
package/src/serializable.ts
CHANGED
|
@@ -25,19 +25,29 @@ export abstract class Serializable<O> {
|
|
|
25
25
|
SerializableConstructor<unknown, unknown>
|
|
26
26
|
> = {};
|
|
27
27
|
|
|
28
|
-
static
|
|
28
|
+
static fromJSON<T, O>(json: string): T {
|
|
29
29
|
const { $ref, value }: SerializerResult<O> = JSON.parse(json),
|
|
30
|
-
[url,
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
)
|
|
30
|
+
[url, pointer] = $ref.split(/#\/?/);
|
|
31
|
+
|
|
32
|
+
console.log({
|
|
33
|
+
url,
|
|
34
|
+
pointer,
|
|
35
|
+
path: pointer?.split("/"),
|
|
36
|
+
name: Serializable.name,
|
|
37
|
+
got: get(Serializable, pointer?.split("/").slice(1) ?? []),
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
if (url === import.meta.url && pointer != null) {
|
|
41
|
+
const path = pointer.split("/");
|
|
42
|
+
|
|
43
|
+
if (path[0] === Serializable.name) {
|
|
44
|
+
return (
|
|
45
|
+
get(Serializable, path.slice(1)) as SerializableConstructor<T, O>
|
|
46
|
+
).fromObject(value);
|
|
47
|
+
}
|
|
38
48
|
}
|
|
39
49
|
|
|
40
|
-
|
|
50
|
+
return JSON.parse(json);
|
|
41
51
|
}
|
|
42
52
|
|
|
43
53
|
static toJSON<T, O>(
|
|
@@ -55,36 +65,9 @@ export abstract class Serializable<O> {
|
|
|
55
65
|
}));
|
|
56
66
|
}
|
|
57
67
|
|
|
58
|
-
static
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
for (const node of nodes) {
|
|
62
|
-
if (value[node.key] === node.value) {
|
|
63
|
-
node.parent = value;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
nodes.push({ key, value });
|
|
68
|
-
|
|
69
|
-
return value;
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
return nodes.reduce(
|
|
73
|
-
(value, node) =>
|
|
74
|
-
value.then(() =>
|
|
75
|
-
Serializable.fromJSON(JSON.stringify(node.value))
|
|
76
|
-
.catch(() => node.value)
|
|
77
|
-
.then((value) => {
|
|
78
|
-
if (node.parent != null) {
|
|
79
|
-
node.parent[node.key] = value;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
console.log(value, node);
|
|
83
|
-
|
|
84
|
-
return value;
|
|
85
|
-
}),
|
|
86
|
-
),
|
|
87
|
-
Promise.resolve(obj),
|
|
68
|
+
static parse(text: string) {
|
|
69
|
+
return JSON.parse(text, (_, value) =>
|
|
70
|
+
Serializable.fromJSON(JSON.stringify(value)),
|
|
88
71
|
);
|
|
89
72
|
}
|
|
90
73
|
|
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
|
};
|
package/src/serializable.spec.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { BrandedSerializer, Serializable } from "./serializable";
|
|
2
|
-
|
|
3
|
-
interface SquareObject {
|
|
4
|
-
x: number;
|
|
5
|
-
y: number;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export interface Square extends SquareObject {}
|
|
9
|
-
|
|
10
|
-
export class Square implements Serializable<SquareObject> {
|
|
11
|
-
constructor({ x, y }: SquareObject) {
|
|
12
|
-
this.x = x;
|
|
13
|
-
this.y = y;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
static fromObject(obj: SquareObject): Square {
|
|
17
|
-
return new Square(obj);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
toObject(): SquareObject {
|
|
21
|
-
return {
|
|
22
|
-
x: this.x,
|
|
23
|
-
y: this.y,
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
toJSON: BrandedSerializer<SquareObject> = Serializable.toJSON(Square, this);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
Serializable.fromJSON<Square, SquareObject>(
|
|
31
|
-
JSON.stringify(new Square({ x: 2, y: 24 })),
|
|
32
|
-
).then(console.log);
|