@cascateer/core 2.3.1 → 2.3.2
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 +2 -1
- package/src/index.ts +1 -0
- package/src/serializable.spec.ts +29 -0
- package/src/serializable.ts +58 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cascateer/core",
|
|
3
|
-
"version": "2.3.
|
|
3
|
+
"version": "2.3.2",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "https://github.com/cascateer/core.git"
|
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
"lodash": "^4.17.21",
|
|
27
27
|
"object-hash": "^3.0.0",
|
|
28
28
|
"rxjs": "^7.8.2",
|
|
29
|
+
"ts-brand": "^0.2.0",
|
|
29
30
|
"uuid": "^13.0.0"
|
|
30
31
|
}
|
|
31
32
|
}
|
package/src/index.ts
CHANGED
|
@@ -3,6 +3,7 @@ export { App } from "./app";
|
|
|
3
3
|
export { createComponent } from "./component";
|
|
4
4
|
export { defineCustomProperties } from "./dom";
|
|
5
5
|
export { createElement, createFragment } from "./jsx-runtime";
|
|
6
|
+
export { BrandedSerializer, Serializable } from "./serializable";
|
|
6
7
|
export { createSlice } from "./slice";
|
|
7
8
|
export { type StoreEffect } from "./store";
|
|
8
9
|
export { type TerminalEffect } from "./terminal";
|
|
@@ -0,0 +1,29 @@
|
|
|
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 { x: this.x, y: this.y };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
toJSON: BrandedSerializer<SquareObject> = Serializable.toJSON(Square, this);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
Serializable.fromJSON<Square, SquareObject>(
|
|
28
|
+
JSON.stringify(new Square({ x: 2, y: 24 })),
|
|
29
|
+
).then(console.log);
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { Dictionary, get, isObject, isString } from "lodash";
|
|
2
|
+
import { Brand, identity } from "ts-brand";
|
|
3
|
+
import { v4 } from "uuid";
|
|
4
|
+
|
|
5
|
+
export interface Serializer<O> {
|
|
6
|
+
(): O & { $ref: string };
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
enum SerializerBrand {}
|
|
10
|
+
|
|
11
|
+
export type BrandedSerializer<O> = Brand<Serializer<O>, SerializerBrand>;
|
|
12
|
+
|
|
13
|
+
interface SerializableConstructor<T, O> {
|
|
14
|
+
name: string;
|
|
15
|
+
fromObject(obj: O): T;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export abstract class Serializable<O> {
|
|
19
|
+
static readonly importMap: Dictionary<
|
|
20
|
+
SerializableConstructor<unknown, unknown>
|
|
21
|
+
> = {};
|
|
22
|
+
|
|
23
|
+
static async fromJSON<T, O>(value: string): Promise<T> {
|
|
24
|
+
const obj: O = JSON.parse(value);
|
|
25
|
+
|
|
26
|
+
if (isObject(obj) && "$ref" in obj && isString(obj.$ref)) {
|
|
27
|
+
const [url, path] = obj.$ref.split(/#\/?/);
|
|
28
|
+
|
|
29
|
+
if (url != null && path != null) {
|
|
30
|
+
return import(url).then((module) =>
|
|
31
|
+
(
|
|
32
|
+
get(module, path.split("/")) as SerializableConstructor<T, O>
|
|
33
|
+
).fromObject(obj),
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
throw new Error(`${value} deserialization failed`);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
static toJSON<T, O>(
|
|
42
|
+
ctor: SerializableConstructor<T, O>,
|
|
43
|
+
value: Serializable<O>,
|
|
44
|
+
): BrandedSerializer<O> {
|
|
45
|
+
const importMap = "importMap" satisfies keyof typeof Serializable;
|
|
46
|
+
const id = v4();
|
|
47
|
+
|
|
48
|
+
this[importMap][id] = ctor;
|
|
49
|
+
|
|
50
|
+
return identity<BrandedSerializer<O>>(() => ({
|
|
51
|
+
...value.toObject(),
|
|
52
|
+
$ref: [`${import.meta.url}#`, this.name, importMap, id].join("/"),
|
|
53
|
+
}));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
abstract toObject(): O;
|
|
57
|
+
abstract toJSON: BrandedSerializer<O>;
|
|
58
|
+
}
|