@cascateer/core 2.3.28 → 2.3.29

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cascateer/core",
3
- "version": "2.3.28",
3
+ "version": "2.3.29",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/cascateer/core.git"
@@ -25,23 +25,19 @@ export abstract class Serializable<O> {
25
25
  SerializableConstructor<unknown, unknown>
26
26
  > = {};
27
27
 
28
- static fromJSON<T, O>(json: string): T {
29
- try {
30
- const { $ref, value }: SerializerResult<O> = JSON.parse(json),
31
- [url, pointer] = $ref.split(/#\/?/);
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
+ if (url != null && path != null) {
33
+ return import(url).then((module) =>
34
+ (
35
+ get(module, path.split("/")) as SerializableConstructor<T, O>
36
+ ).fromObject(value),
37
+ );
38
+ }
43
39
 
44
- return JSON.parse(json);
40
+ throw new Error(`${json} deserialization failed`);
45
41
  }
46
42
 
47
43
  static toJSON<T, O>(
@@ -59,9 +55,34 @@ export abstract class Serializable<O> {
59
55
  }));
60
56
  }
61
57
 
62
- static parse(text: string) {
63
- return JSON.parse(text, (_, value) =>
64
- Serializable.fromJSON(JSON.stringify(value)),
58
+ static async parse(text: string) {
59
+ const nodes: { key: string; value: any; parent?: any }[] = [];
60
+ const obj = JSON.parse(text, (key, value) => {
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
+ return value;
83
+ }),
84
+ ),
85
+ Promise.resolve(obj),
65
86
  );
66
87
  }
67
88
 
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
- flatMap((event) =>
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
  };