silentium-components 0.0.97 → 0.0.99

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.
@@ -16,9 +16,9 @@ export function Path<
16
16
  R,
17
17
  T extends object | Array<any> = any,
18
18
  K extends string = any,
19
- >($base: MessageType<T>, _keyed: MaybeMessage<K>, def?: MaybeMessage<T>) {
19
+ >($base: MessageType<T>, _keyed: MaybeMessage<K>, def?: MaybeMessage<R>) {
20
20
  const $keyed = ActualMessage(_keyed);
21
- const $def = ActualMessage(def ?? NotSet);
21
+ const $def = ActualMessage((def as any) ?? NotSet);
22
22
  return Applied(All($base, $keyed, $def), ([base, keyed, d]) => {
23
23
  const keys = keyed.split(".");
24
24
  let value: unknown = base;
@@ -1,4 +1,4 @@
1
- import { Applied, Late, Message, Of, Shared } from "silentium";
1
+ import { Applied, Connected, Late, Message, Of, Shared, Void } from "silentium";
2
2
  import { expect, test, vi } from "vitest";
3
3
 
4
4
  import { Router } from "../navigation/Router";
@@ -88,3 +88,124 @@ test("Router destroys previous route messages when switching routes", () => {
88
88
 
89
89
  $router.destroy();
90
90
  });
91
+
92
+ test("Router destroys Connected messages when switching routes", () => {
93
+ const $url = Late<string>("http://domain.com/");
94
+ const $urlPath = Shared(Applied($url, drop("http://domain.com")));
95
+
96
+ // Create mock destroyable messages for main routes and connected messages
97
+ const firstRouteMainDestroy = vi.fn();
98
+ const firstRouteConnectedDestroy = vi.fn();
99
+ const secondRouteMainDestroy = vi.fn();
100
+ const secondRouteConnectedDestroy = vi.fn();
101
+ const defaultMainDestroy = vi.fn();
102
+ const defaultConnectedDestroy = vi.fn();
103
+
104
+ const $fcd = Message<string>((transport) => {
105
+ transport("first-connected-response");
106
+ return firstRouteConnectedDestroy;
107
+ });
108
+ const $firstRoute = () =>
109
+ Connected(
110
+ Message<string>((transport) => {
111
+ transport("first-route-response");
112
+ return firstRouteMainDestroy;
113
+ }),
114
+ $fcd,
115
+ );
116
+
117
+ const $scd = Message<string>((transport) => {
118
+ transport("second-connected-response");
119
+ return secondRouteConnectedDestroy;
120
+ });
121
+ const $secondRoute = () =>
122
+ Connected(
123
+ Message<string>((transport) => {
124
+ transport("second-route-response");
125
+ return secondRouteMainDestroy;
126
+ }),
127
+ $scd,
128
+ );
129
+
130
+ const $dcd = Message<string>((transport) => {
131
+ transport("default-connected-response");
132
+ return defaultConnectedDestroy;
133
+ });
134
+ const $default = () =>
135
+ Connected(
136
+ Message<string>((transport) => {
137
+ transport("default-response");
138
+ return defaultMainDestroy;
139
+ }),
140
+ $dcd,
141
+ );
142
+
143
+ const $router = Router(
144
+ $urlPath,
145
+ Of([
146
+ {
147
+ pattern: "^/first$",
148
+ message: $firstRoute,
149
+ },
150
+ {
151
+ pattern: "^/second$",
152
+ message: $secondRoute,
153
+ },
154
+ ]),
155
+ $default,
156
+ );
157
+
158
+ $fcd.then(Void());
159
+ $scd.then(Void());
160
+ $dcd.then(Void());
161
+
162
+ const g2 = vi.fn();
163
+ $router.then(g2);
164
+
165
+ // Initially no route matches, should use default
166
+ expect(g2).toHaveBeenLastCalledWith("default-response");
167
+ expect(defaultMainDestroy).not.toHaveBeenCalled();
168
+ expect(defaultConnectedDestroy).not.toHaveBeenCalled();
169
+
170
+ // Change to first route
171
+ $url.use("http://domain.com/first");
172
+
173
+ expect(g2).toHaveBeenLastCalledWith("first-route-response");
174
+ // Default Connected messages should be destroyed
175
+ expect(defaultMainDestroy).toHaveBeenCalledTimes(1);
176
+ expect(defaultConnectedDestroy).toHaveBeenCalledTimes(1);
177
+ expect(firstRouteMainDestroy).not.toHaveBeenCalled();
178
+ expect(firstRouteConnectedDestroy).not.toHaveBeenCalled();
179
+
180
+ // Change to second route
181
+ $url.use("http://domain.com/second");
182
+
183
+ expect(g2).toHaveBeenLastCalledWith("second-route-response");
184
+ // First route Connected messages should be destroyed
185
+ expect(firstRouteMainDestroy).toHaveBeenCalledTimes(1);
186
+ expect(firstRouteConnectedDestroy).toHaveBeenCalledTimes(1);
187
+ expect(secondRouteMainDestroy).not.toHaveBeenCalled();
188
+ expect(secondRouteConnectedDestroy).not.toHaveBeenCalled();
189
+
190
+ // Change back to no match (default)
191
+ $url.use("http://domain.com/nomatch");
192
+
193
+ expect(g2).toHaveBeenLastCalledWith("default-response");
194
+ // Second route Connected messages should be destroyed
195
+ expect(secondRouteMainDestroy).toHaveBeenCalledTimes(1);
196
+ expect(secondRouteConnectedDestroy).toHaveBeenCalledTimes(1);
197
+ expect(defaultMainDestroy).toHaveBeenCalledTimes(1); // Still only once
198
+ expect(defaultConnectedDestroy).toHaveBeenCalledTimes(1); // Still only once
199
+
200
+ // Change back to first route
201
+ $url.use("http://domain.com/first");
202
+
203
+ expect(g2).toHaveBeenLastCalledWith("first-route-response");
204
+ // Default destroyed again
205
+ expect(defaultMainDestroy).toHaveBeenCalledTimes(2);
206
+ expect(defaultConnectedDestroy).toHaveBeenCalledTimes(1);
207
+ expect(firstRouteMainDestroy).toHaveBeenCalledTimes(1); // Still only once
208
+ expect(firstRouteConnectedDestroy).toHaveBeenCalledTimes(1); // Still only once
209
+
210
+ $router.destroy();
211
+ });