@cascateer/core 2.3.0 → 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/lib/ExtendableDictionary.ts +10 -8
- package/src/multicast.ts +2 -2
- package/src/observable/index.ts +0 -1
- package/src/operators/{concat.ts → accumulate.ts} +1 -1
- package/src/operators/index.ts +1 -1
- package/src/serializable.spec.ts +29 -0
- package/src/serializable.ts +58 -0
- package/src/types.ts +2 -2
- package/src/observable/Future.ts +0 -17
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";
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { Dictionary } from "lodash";
|
|
2
|
-
import { UnaryFunction } from "rxjs";
|
|
3
|
-
import { Future } from "../observable";
|
|
1
|
+
import { Dictionary, once } from "lodash";
|
|
2
|
+
import { AsyncSubject, lastValueFrom, UnaryFunction } from "rxjs";
|
|
4
3
|
import { keys } from "./keys";
|
|
5
4
|
|
|
6
5
|
export type Extend<T, U> = Omit<T, keyof U> & U;
|
|
@@ -8,12 +7,15 @@ export type Extend<T, U> = Omit<T, keyof U> & U;
|
|
|
8
7
|
export class ExtendableDictionary<T, U extends Dictionary<T>> {
|
|
9
8
|
constructor(
|
|
10
9
|
public currentValue: U,
|
|
11
|
-
private value = new
|
|
10
|
+
private value = new AsyncSubject<Dictionary<T>>(),
|
|
12
11
|
) {}
|
|
13
12
|
|
|
14
|
-
complete(): U {
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
complete = once((): U => {
|
|
14
|
+
this.value.next(this.currentValue);
|
|
15
|
+
this.value.complete();
|
|
16
|
+
|
|
17
|
+
return this.currentValue;
|
|
18
|
+
});
|
|
17
19
|
|
|
18
20
|
extend<V extends Dictionary<T>>(
|
|
19
21
|
value: (
|
|
@@ -30,7 +32,7 @@ export class ExtendableDictionary<T, U extends Dictionary<T>> {
|
|
|
30
32
|
...value(this.currentValue)({
|
|
31
33
|
property: (constructor) => {
|
|
32
34
|
const property = constructor(
|
|
33
|
-
this.value.
|
|
35
|
+
lastValueFrom(this.value).then(
|
|
34
36
|
(value) =>
|
|
35
37
|
new Promise<string>((resolve, reject) => {
|
|
36
38
|
for (const key of keys(value)) {
|
package/src/multicast.ts
CHANGED
|
@@ -13,7 +13,7 @@ import {
|
|
|
13
13
|
import { v4 } from "uuid";
|
|
14
14
|
import { property } from "./lib";
|
|
15
15
|
import {
|
|
16
|
-
|
|
16
|
+
accumulate,
|
|
17
17
|
exchangeWith,
|
|
18
18
|
flatMap,
|
|
19
19
|
MulticastActionMessage,
|
|
@@ -93,7 +93,7 @@ self.addEventListener("connect", ({ ports }) => {
|
|
|
93
93
|
map(({ origin, ...message }) => message),
|
|
94
94
|
exchangeWith<MulticastClientMessage, MulticastActionMessage<any>>(port),
|
|
95
95
|
map((message) => ({ ...message, origin: port })),
|
|
96
|
-
|
|
96
|
+
accumulate(),
|
|
97
97
|
flatMap((messages) =>
|
|
98
98
|
thru(
|
|
99
99
|
partition(messages, (message) => message.type === "connect"),
|
package/src/observable/index.ts
CHANGED
package/src/operators/index.ts
CHANGED
|
@@ -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
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { Observable } from "rxjs/internal/Observable";
|
|
|
4
4
|
import { ObservableInput } from "rxjs/internal/types";
|
|
5
5
|
import { memoizeHashed } from "./lib/memoizeHashed";
|
|
6
6
|
import { ProxyObservable } from "./observable";
|
|
7
|
-
import {
|
|
7
|
+
import { accumulate, every, some } from "./operators";
|
|
8
8
|
|
|
9
9
|
export interface Effect<Args, Result> extends UnaryFunction<
|
|
10
10
|
Args,
|
|
@@ -51,7 +51,7 @@ export class ProxyEffectInterceptor extends ReplaySubject<
|
|
|
51
51
|
return (args) =>
|
|
52
52
|
new ProxyObservable(effect(args), () =>
|
|
53
53
|
this.pipe(
|
|
54
|
-
|
|
54
|
+
accumulate(),
|
|
55
55
|
switchMap((sources) =>
|
|
56
56
|
combineLatest(sources.map((source) => source.pending)),
|
|
57
57
|
),
|
package/src/observable/Future.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { once } from "lodash";
|
|
2
|
-
import { AsyncSubject, lastValueFrom, UnaryFunction } from "rxjs";
|
|
3
|
-
|
|
4
|
-
export class Future<T> extends AsyncSubject<T> {
|
|
5
|
-
completeWith = once(<U extends T>(value: U): U => {
|
|
6
|
-
this.next(value);
|
|
7
|
-
this.complete();
|
|
8
|
-
|
|
9
|
-
return value;
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
once<U>(predicate: UnaryFunction<T, U | PromiseLike<U>>): Promise<U> {
|
|
13
|
-
return lastValueFrom(this).then(predicate);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
then = this.once;
|
|
17
|
-
}
|