silentium 0.0.180 → 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.
@@ -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";