silentium 0.0.179 → 0.0.181
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 +14 -0
- package/dist/silentium.cjs +30 -0
- package/dist/silentium.cjs.map +1 -1
- package/dist/silentium.d.ts +9 -1
- package/dist/silentium.js +30 -1
- package/dist/silentium.js.map +1 -1
- package/dist/silentium.min.js +1 -1
- package/dist/silentium.min.mjs +1 -1
- package/dist/silentium.min.mjs.map +1 -1
- package/dist/silentium.mjs +30 -1
- package/dist/silentium.mjs.map +1 -1
- package/package.json +1 -1
- package/src/components/Shared.test.ts +28 -1
- package/src/components/Shared.ts +4 -1
- package/src/components/Trackable.test.ts +61 -0
- package/src/components/Trackable.ts +32 -0
- package/src/index.ts +1 -0
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { describe, expect, test } from "vitest";
|
|
1
|
+
import { describe, expect, test, vi } from "vitest";
|
|
2
2
|
import { Late } from "components/Late";
|
|
3
3
|
import { Shared } from "components/Shared";
|
|
4
4
|
import { Diagram } from "testing/Diagram";
|
|
@@ -68,4 +68,31 @@ describe("Shared.test", () => {
|
|
|
68
68
|
|
|
69
69
|
expect(count).toBe(1);
|
|
70
70
|
});
|
|
71
|
+
|
|
72
|
+
test("destroy calls destroy on base message if base is destroyable", () => {
|
|
73
|
+
const destroyMock = vi.fn();
|
|
74
|
+
const baseMock = {
|
|
75
|
+
then: vi.fn(),
|
|
76
|
+
catch: vi.fn(),
|
|
77
|
+
destroy: destroyMock,
|
|
78
|
+
};
|
|
79
|
+
const s = Shared(baseMock);
|
|
80
|
+
|
|
81
|
+
s.destroy();
|
|
82
|
+
|
|
83
|
+
expect(destroyMock).toHaveBeenCalledTimes(1);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test("destroy does not call destroy on base message if base is not destroyable", () => {
|
|
87
|
+
const baseMock = {
|
|
88
|
+
then: vi.fn(),
|
|
89
|
+
catch: vi.fn(),
|
|
90
|
+
// no destroy method
|
|
91
|
+
};
|
|
92
|
+
const s = Shared(baseMock);
|
|
93
|
+
|
|
94
|
+
s.destroy();
|
|
95
|
+
|
|
96
|
+
// No assertion needed, just ensure no error and destroy not called
|
|
97
|
+
});
|
|
71
98
|
});
|
package/src/components/Shared.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { MessageType } from "types/MessageType";
|
|
2
2
|
import { MessageSourceType, SourceType } from "types/SourceType";
|
|
3
3
|
import { ConstructorType } from "types/ConstructorType";
|
|
4
|
-
import { isFilled, isSource } from "helpers/guards";
|
|
4
|
+
import { isDestroyable, isFilled, isSource } from "helpers/guards";
|
|
5
5
|
import { Primitive } from "components/Primitive";
|
|
6
6
|
import { ChainableType } from "types/ChainableType";
|
|
7
7
|
|
|
@@ -56,6 +56,9 @@ export class SharedImpl<T> implements MessageSourceType<T>, ChainableType<T> {
|
|
|
56
56
|
|
|
57
57
|
public destroy() {
|
|
58
58
|
this.resolvers.clear();
|
|
59
|
+
if (isDestroyable(this.$base)) {
|
|
60
|
+
this.$base.destroy();
|
|
61
|
+
}
|
|
59
62
|
return this;
|
|
60
63
|
}
|
|
61
64
|
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Trackable } from "components/Trackable";
|
|
2
|
+
import { Context } from "components/Context";
|
|
3
|
+
import { describe, expect, test, vi } from "vitest";
|
|
4
|
+
|
|
5
|
+
describe("Trackable.test", () => {
|
|
6
|
+
test("sends created action on trackable creation", () => {
|
|
7
|
+
const transportSpy = vi.fn();
|
|
8
|
+
Context.transport.set("trackable", transportSpy);
|
|
9
|
+
|
|
10
|
+
const target = { value: 42 };
|
|
11
|
+
const name = "testComponent";
|
|
12
|
+
|
|
13
|
+
Trackable(name, target);
|
|
14
|
+
|
|
15
|
+
expect(transportSpy).toHaveBeenCalledWith(
|
|
16
|
+
expect.objectContaining({
|
|
17
|
+
transport: "trackable",
|
|
18
|
+
params: { name, action: "created" },
|
|
19
|
+
}),
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
Context.transport.delete("trackable");
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test("sends destroyed action on destroy access", () => {
|
|
26
|
+
const transportSpy = vi.fn();
|
|
27
|
+
Context.transport.set("trackable", transportSpy);
|
|
28
|
+
|
|
29
|
+
const target = { value: 42 };
|
|
30
|
+
const name = "testComponent";
|
|
31
|
+
|
|
32
|
+
const proxy = Trackable(name, target) as any;
|
|
33
|
+
|
|
34
|
+
void proxy.destroy;
|
|
35
|
+
|
|
36
|
+
expect(transportSpy).toHaveBeenCalledWith(
|
|
37
|
+
expect.objectContaining({
|
|
38
|
+
transport: "trackable",
|
|
39
|
+
params: { name, action: "destroyed" },
|
|
40
|
+
}),
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
Context.transport.delete("trackable");
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test("proxy forwards other properties normally", () => {
|
|
47
|
+
const transportSpy = vi.fn();
|
|
48
|
+
Context.transport.set("trackable", transportSpy);
|
|
49
|
+
|
|
50
|
+
const target = { value: 42, method: () => "hello" };
|
|
51
|
+
const name = "testComponent";
|
|
52
|
+
|
|
53
|
+
const proxy = Trackable(name, target) as any;
|
|
54
|
+
|
|
55
|
+
expect(proxy.value).toBe(42);
|
|
56
|
+
expect(proxy.method()).toBe("hello");
|
|
57
|
+
expect(proxy.nonExistent).toBeUndefined();
|
|
58
|
+
|
|
59
|
+
Context.transport.delete("trackable");
|
|
60
|
+
});
|
|
61
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Context } from "components/Context";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Track creation and destruction of components
|
|
5
|
+
* uses Context component to send messages
|
|
6
|
+
* when created sends action=created
|
|
7
|
+
* when destroyed sends action=destroyed
|
|
8
|
+
*/
|
|
9
|
+
export function Trackable(name: string, target: object) {
|
|
10
|
+
Context({
|
|
11
|
+
transport: "trackable",
|
|
12
|
+
params: {
|
|
13
|
+
name,
|
|
14
|
+
action: "created",
|
|
15
|
+
},
|
|
16
|
+
}).then(() => {});
|
|
17
|
+
return new Proxy(target, {
|
|
18
|
+
get(target, prop, receiver) {
|
|
19
|
+
if (prop === "destroy") {
|
|
20
|
+
Context({
|
|
21
|
+
transport: "trackable",
|
|
22
|
+
params: {
|
|
23
|
+
name,
|
|
24
|
+
action: "destroyed",
|
|
25
|
+
},
|
|
26
|
+
}).then(() => {});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return Reflect.get(target, prop, receiver);
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -36,6 +36,7 @@ export * from "./components/Race";
|
|
|
36
36
|
export * from "./components/Sequence";
|
|
37
37
|
export * from "./components/Shared";
|
|
38
38
|
export * from "./components/Stream";
|
|
39
|
+
export * from "./components/Trackable";
|
|
39
40
|
export * from "./helpers/DevTools";
|
|
40
41
|
export * from "./helpers/ensures";
|
|
41
42
|
export * from "./helpers/guards";
|