silentium 0.0.152 → 0.0.153
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/CHANGELOG.md +7 -0
- package/package.json +1 -1
- package/src/components/Process.test.ts +114 -0
- package/src/components/Process.ts +28 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
### [0.0.153](https://github.com/silentium-lab/silentium/compare/v0.0.152...v0.0.153) (2025-11-24)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
* **main:** process component added ([c2db805](https://github.com/silentium-lab/silentium/commit/c2db8051db80c248bc0c7be10f15fe843c85478b))
|
|
11
|
+
|
|
5
12
|
### [0.0.152](https://github.com/silentium-lab/silentium/compare/v0.0.151...v0.0.152) (2025-11-24)
|
|
6
13
|
|
|
7
14
|
|
package/package.json
CHANGED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
import { Message } from "base/Message";
|
|
3
|
+
import { Process } from "components/Process";
|
|
4
|
+
import { Late } from "components/Late";
|
|
5
|
+
import { Diagram } from "testing/Diagram";
|
|
6
|
+
import { Of } from "base/Of";
|
|
7
|
+
|
|
8
|
+
describe("Process.test", () => {
|
|
9
|
+
test("basic process", async () => {
|
|
10
|
+
const $base = Of(10); // resolves to 10
|
|
11
|
+
const builder = (v: number) => Of(v * 2); // returns Message that resolves to v*2
|
|
12
|
+
|
|
13
|
+
const $proc = Process($base, builder);
|
|
14
|
+
const result = await $proc;
|
|
15
|
+
expect(result).toBe(20);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test("process with late shared", () => {
|
|
19
|
+
const d = Diagram();
|
|
20
|
+
const $base = Late<number>();
|
|
21
|
+
const builder = (v: number) =>
|
|
22
|
+
Message<number>((resolve) => {
|
|
23
|
+
resolve(v + 1);
|
|
24
|
+
return () => d.resolver(`destroy_${v}`);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const $proc = Process($base, builder);
|
|
28
|
+
|
|
29
|
+
$proc.then((v) => d.resolver(`proc_${v}`));
|
|
30
|
+
|
|
31
|
+
$base.use(5);
|
|
32
|
+
|
|
33
|
+
expect(d.toString()).toBe("proc_6");
|
|
34
|
+
|
|
35
|
+
$proc.destroy();
|
|
36
|
+
|
|
37
|
+
expect(d.toString()).toBe("proc_6|destroy_5");
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("error handling from built message", () => {
|
|
41
|
+
const $base = Of(10);
|
|
42
|
+
const builder = () =>
|
|
43
|
+
Message<number>((resolve, reject) => {
|
|
44
|
+
reject("error from builder");
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const $proc = Process($base, builder);
|
|
48
|
+
|
|
49
|
+
let caught = false;
|
|
50
|
+
$proc.then(() => {
|
|
51
|
+
throw new Error("should not resolve");
|
|
52
|
+
});
|
|
53
|
+
$proc.catch((e) => {
|
|
54
|
+
expect(e).toBe("error from builder");
|
|
55
|
+
caught = true;
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
expect(caught).toBe(true);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("error handling from base", () => {
|
|
62
|
+
const $base = Message<string>((resolve, reject) => {
|
|
63
|
+
reject("base error");
|
|
64
|
+
});
|
|
65
|
+
const builder = (v: string) => Of(`processed_${v}`);
|
|
66
|
+
|
|
67
|
+
const $proc = Process($base, builder);
|
|
68
|
+
|
|
69
|
+
let caught = false;
|
|
70
|
+
$proc.then(() => {
|
|
71
|
+
throw new Error("should not resolve");
|
|
72
|
+
});
|
|
73
|
+
$proc.catch((e) => {
|
|
74
|
+
expect(e).toBe("base error");
|
|
75
|
+
caught = true;
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
expect(caught).toBe(true);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test("chaining multiple processes", async () => {
|
|
82
|
+
const $base = Of(1);
|
|
83
|
+
|
|
84
|
+
// Process 1: add 2
|
|
85
|
+
const $proc1 = Process($base, (v: number) => Of(v + 2));
|
|
86
|
+
|
|
87
|
+
// Process 2: multiply by 3
|
|
88
|
+
const $proc2 = Process($proc1, (v: number) => Of(v * 3));
|
|
89
|
+
|
|
90
|
+
const result = await $proc2;
|
|
91
|
+
expect(result).toBe(9); // (1+2)*3
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test("destruction calls destroy on created messages", () => {
|
|
95
|
+
let isDestroyed = false;
|
|
96
|
+
const $base = Late<number>();
|
|
97
|
+
const builder = (v: number) =>
|
|
98
|
+
Message<number>((resolve) => {
|
|
99
|
+
resolve(v * 2);
|
|
100
|
+
return () => {
|
|
101
|
+
isDestroyed = true;
|
|
102
|
+
};
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
const $proc = Process($base, builder);
|
|
106
|
+
|
|
107
|
+
$base.use(5); // Triggers creation if listened
|
|
108
|
+
$proc.then(() => {}); // Trigger the executor, add to dc
|
|
109
|
+
expect(isDestroyed).toBe(false);
|
|
110
|
+
|
|
111
|
+
$proc.destroy();
|
|
112
|
+
expect(isDestroyed).toBe(true);
|
|
113
|
+
});
|
|
114
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { DestroyContainer } from "base/DestroyContainer";
|
|
2
|
+
import { Message } from "base/Message";
|
|
3
|
+
import { LateShared } from "components/LateShared";
|
|
4
|
+
import { ConstructorType } from "types/ConstructorType";
|
|
5
|
+
import { MessageType } from "types/MessageType";
|
|
6
|
+
|
|
7
|
+
export function Process<T, R = unknown>(
|
|
8
|
+
$base: MessageType<T>,
|
|
9
|
+
builder: ConstructorType<[T], MessageType<R>>,
|
|
10
|
+
) {
|
|
11
|
+
return Message<R>((resolve, reject) => {
|
|
12
|
+
const $res = LateShared<R>();
|
|
13
|
+
const dc = DestroyContainer();
|
|
14
|
+
|
|
15
|
+
$base.then((v) => {
|
|
16
|
+
const $msg = builder(v);
|
|
17
|
+
dc.add($msg);
|
|
18
|
+
$res.chain($msg);
|
|
19
|
+
$msg.catch(reject);
|
|
20
|
+
});
|
|
21
|
+
$base.catch(reject);
|
|
22
|
+
$res.then(resolve);
|
|
23
|
+
|
|
24
|
+
return () => {
|
|
25
|
+
dc.destroy();
|
|
26
|
+
};
|
|
27
|
+
});
|
|
28
|
+
}
|