@krate/runtime 0.5.0-beta.3 → 0.5.0-rc.10

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.
Files changed (219) hide show
  1. package/dist/action-context.js +1 -1
  2. package/dist/actions.d.ts +7 -0
  3. package/dist/actions.js +34 -10
  4. package/dist/after.d.ts +28 -0
  5. package/dist/after.js +48 -0
  6. package/dist/assets-static.d.ts +10 -1
  7. package/dist/assets-static.js +10 -2
  8. package/dist/base-path.d.ts +7 -0
  9. package/dist/base-path.js +20 -0
  10. package/dist/boundary.js +1 -1
  11. package/dist/builtin-tags.d.ts +12 -0
  12. package/dist/builtin-tags.js +42 -0
  13. package/dist/cache-adapter.js +1 -1
  14. package/dist/cache-scope.d.ts +8 -0
  15. package/dist/cache-scope.js +14 -1
  16. package/dist/cloudflare-workers.d.ts +2 -0
  17. package/dist/cloudflare-workers.js +27 -0
  18. package/dist/context.d.ts +5 -0
  19. package/dist/context.js +28 -17
  20. package/dist/dev-overlay.d.ts +54 -3
  21. package/dist/dev-overlay.js +496 -91
  22. package/dist/dom.d.ts +4 -0
  23. package/dist/dom.js +62 -16
  24. package/dist/effects.js +43 -10
  25. package/dist/events.d.ts +2 -0
  26. package/dist/events.js +18 -0
  27. package/dist/flight-props.d.ts +7 -0
  28. package/dist/flight-props.js +105 -0
  29. package/dist/handler-html.js +4 -4
  30. package/dist/handler-routing.d.ts +2 -23
  31. package/dist/handler-routing.js +9 -190
  32. package/dist/handler-segments.d.ts +6 -0
  33. package/dist/handler-segments.js +18 -4
  34. package/dist/head-tags.d.ts +9 -0
  35. package/dist/head-tags.js +93 -6
  36. package/dist/highlight-bind.js +1 -1
  37. package/dist/highlight.d.ts +3 -2
  38. package/dist/highlight.js +17 -12
  39. package/dist/hints.d.ts +25 -2
  40. package/dist/hints.js +147 -74
  41. package/dist/index.d.ts +2 -0
  42. package/dist/index.js +3 -0
  43. package/dist/island-swap.d.ts +47 -0
  44. package/dist/island-swap.js +635 -0
  45. package/dist/jsx-dev-runtime.d.ts +1 -1
  46. package/dist/jsx-types.d.ts +99 -3
  47. package/dist/krate-core.js +1 -1
  48. package/dist/krate-dev.js +407 -80
  49. package/dist/krate-f-actions.js +1 -1
  50. package/dist/krate-f-classes.js +1 -1
  51. package/dist/krate-f-highlight.js +1 -1
  52. package/dist/krate-f-list.js +1 -1
  53. package/dist/krate-f-navigation.js +1 -1
  54. package/dist/krate-f-router-lite.js +1 -1
  55. package/dist/krate-f-router.js +1 -1
  56. package/dist/krate-features.json +455 -0
  57. package/dist/krate-hydrate-lite.js +5 -5
  58. package/dist/krate-hydrate.js +5 -5
  59. package/dist/krate-island-swap.js +1 -0
  60. package/dist/krate-runtime.js +5 -5
  61. package/dist/krate-suspense.js +1 -1
  62. package/dist/list.d.ts +14 -4
  63. package/dist/list.js +30 -23
  64. package/dist/metadata.js +5 -2
  65. package/dist/navigation-react.d.ts +13 -0
  66. package/dist/navigation-react.js +86 -0
  67. package/dist/navigation.d.ts +19 -0
  68. package/dist/navigation.js +35 -0
  69. package/dist/node-server.js +3 -3
  70. package/dist/og.js +1 -1
  71. package/dist/platform.d.ts +52 -0
  72. package/dist/platform.js +57 -0
  73. package/dist/react-compat.d.ts +39 -0
  74. package/dist/react-compat.js +359 -0
  75. package/dist/react-server.d.ts +63 -0
  76. package/dist/react-server.js +125 -0
  77. package/dist/region-wire.d.ts +10 -0
  78. package/dist/region-wire.js +154 -0
  79. package/dist/request-context.js +1 -1
  80. package/dist/request-extras.d.ts +4 -0
  81. package/dist/request-extras.js +51 -0
  82. package/dist/resp.js +1 -1
  83. package/dist/route-source.d.ts +39 -0
  84. package/dist/route-source.js +532 -0
  85. package/dist/router-lite.js +3 -9
  86. package/dist/router.d.ts +1 -1
  87. package/dist/router.js +675 -100
  88. package/dist/server-handler.d.ts +69 -1
  89. package/dist/server-handler.js +515 -96
  90. package/dist/server-renderer.js +16 -7
  91. package/dist/server.d.ts +73 -16
  92. package/dist/server.js +619 -102
  93. package/dist/sha256.js +9 -9
  94. package/dist/signal.js +47 -20
  95. package/dist/site-config.d.ts +15 -0
  96. package/dist/site-config.js +28 -0
  97. package/dist/slot.d.ts +12 -0
  98. package/dist/slot.js +23 -0
  99. package/dist/ssr-core.d.ts +2 -0
  100. package/dist/ssr-core.js +12 -4
  101. package/dist/suspense.js +34 -2
  102. package/dist/testing.d.ts +62 -0
  103. package/dist/testing.js +90 -0
  104. package/dist/use-id.js +1 -1
  105. package/package.json +28 -3
  106. package/dist/action-context.test.d.ts +0 -1
  107. package/dist/action-context.test.js +0 -72
  108. package/dist/actions.test.d.ts +0 -1
  109. package/dist/actions.test.js +0 -477
  110. package/dist/cache-adapter.test.d.ts +0 -1
  111. package/dist/cache-adapter.test.js +0 -244
  112. package/dist/cache.test.d.ts +0 -1
  113. package/dist/cache.test.js +0 -233
  114. package/dist/class-component.test.d.ts +0 -1
  115. package/dist/class-component.test.js +0 -462
  116. package/dist/class-helpers.test.d.ts +0 -1
  117. package/dist/class-helpers.test.js +0 -169
  118. package/dist/context.test.d.ts +0 -1
  119. package/dist/context.test.js +0 -57
  120. package/dist/derived.test.d.ts +0 -1
  121. package/dist/derived.test.js +0 -140
  122. package/dist/dev-overlay.test.d.ts +0 -1
  123. package/dist/dev-overlay.test.js +0 -152
  124. package/dist/discrete.test.d.ts +0 -1
  125. package/dist/discrete.test.js +0 -100
  126. package/dist/dom.test.d.ts +0 -1
  127. package/dist/dom.test.js +0 -31
  128. package/dist/effect-errors.test.d.ts +0 -1
  129. package/dist/effect-errors.test.js +0 -163
  130. package/dist/effects.test.d.ts +0 -1
  131. package/dist/effects.test.js +0 -485
  132. package/dist/events.test.d.ts +0 -1
  133. package/dist/events.test.js +0 -447
  134. package/dist/handler-html.test.d.ts +0 -1
  135. package/dist/handler-html.test.js +0 -256
  136. package/dist/handler-routing.test.d.ts +0 -1
  137. package/dist/handler-routing.test.js +0 -99
  138. package/dist/handler-segments.test.d.ts +0 -1
  139. package/dist/handler-segments.test.js +0 -217
  140. package/dist/head-tags.test.d.ts +0 -1
  141. package/dist/head-tags.test.js +0 -131
  142. package/dist/highlight-bind.test.d.ts +0 -1
  143. package/dist/highlight-bind.test.js +0 -124
  144. package/dist/highlight.test.d.ts +0 -1
  145. package/dist/highlight.test.js +0 -56
  146. package/dist/lanes.test.d.ts +0 -1
  147. package/dist/lanes.test.js +0 -59
  148. package/dist/list.test.d.ts +0 -1
  149. package/dist/list.test.js +0 -344
  150. package/dist/metadata.test.d.ts +0 -1
  151. package/dist/metadata.test.js +0 -167
  152. package/dist/navigation.test.d.ts +0 -1
  153. package/dist/navigation.test.js +0 -130
  154. package/dist/node-server.test.d.ts +0 -1
  155. package/dist/node-server.test.js +0 -492
  156. package/dist/optimistic-lanes.test.d.ts +0 -1
  157. package/dist/optimistic-lanes.test.js +0 -45
  158. package/dist/patterns.test.d.ts +0 -1
  159. package/dist/patterns.test.js +0 -98
  160. package/dist/portal.test.d.ts +0 -1
  161. package/dist/portal.test.js +0 -99
  162. package/dist/react-effects.test.d.ts +0 -1
  163. package/dist/react-effects.test.js +0 -96
  164. package/dist/react-suspense.test.d.ts +0 -1
  165. package/dist/react-suspense.test.js +0 -194
  166. package/dist/react.test.d.ts +0 -1
  167. package/dist/react.test.js +0 -60
  168. package/dist/react19.test.d.ts +0 -1
  169. package/dist/react19.test.js +0 -176
  170. package/dist/reactivity-core.test.d.ts +0 -1
  171. package/dist/reactivity-core.test.js +0 -152
  172. package/dist/reconcile.test.d.ts +0 -1
  173. package/dist/reconcile.test.js +0 -110
  174. package/dist/refs.test.d.ts +0 -1
  175. package/dist/refs.test.js +0 -257
  176. package/dist/router-layout.test.d.ts +0 -1
  177. package/dist/router-layout.test.js +0 -249
  178. package/dist/router-nav.test.d.ts +0 -1
  179. package/dist/router-nav.test.js +0 -464
  180. package/dist/router-slots.test.d.ts +0 -1
  181. package/dist/router-slots.test.js +0 -144
  182. package/dist/router-speculation.test.d.ts +0 -1
  183. package/dist/router-speculation.test.js +0 -25
  184. package/dist/router-styles.test.d.ts +0 -1
  185. package/dist/router-styles.test.js +0 -98
  186. package/dist/router.test.d.ts +0 -1
  187. package/dist/router.test.js +0 -36
  188. package/dist/server-async.test.d.ts +0 -1
  189. package/dist/server-async.test.js +0 -216
  190. package/dist/server-handler.test.d.ts +0 -1
  191. package/dist/server-handler.test.js +0 -535
  192. package/dist/server-highlight.test.d.ts +0 -1
  193. package/dist/server-highlight.test.js +0 -32
  194. package/dist/server-renderer.test.d.ts +0 -1
  195. package/dist/server-renderer.test.js +0 -266
  196. package/dist/server.test.d.ts +0 -1
  197. package/dist/server.test.js +0 -88
  198. package/dist/sha256.test.d.ts +0 -1
  199. package/dist/sha256.test.js +0 -13
  200. package/dist/signal-lanes.test.d.ts +0 -1
  201. package/dist/signal-lanes.test.js +0 -32
  202. package/dist/signal.test.d.ts +0 -1
  203. package/dist/signal.test.js +0 -68
  204. package/dist/slots.test.d.ts +0 -1
  205. package/dist/slots.test.js +0 -169
  206. package/dist/snapshots.test.d.ts +0 -1
  207. package/dist/snapshots.test.js +0 -208
  208. package/dist/ssr-core-behavior.test.d.ts +0 -1
  209. package/dist/ssr-core-behavior.test.js +0 -620
  210. package/dist/ssr-core.test.d.ts +0 -1
  211. package/dist/ssr-core.test.js +0 -324
  212. package/dist/static-memory.test.d.ts +0 -1
  213. package/dist/static-memory.test.js +0 -26
  214. package/dist/storage.test.d.ts +0 -1
  215. package/dist/storage.test.js +0 -46
  216. package/dist/suspense-actions.test.d.ts +0 -1
  217. package/dist/suspense-actions.test.js +0 -72
  218. package/dist/suspense.test.d.ts +0 -1
  219. package/dist/suspense.test.js +0 -518
@@ -1,518 +0,0 @@
1
- import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2
- import { createSignal, createEffect } from './signal.js';
3
- import { createContext } from './context.js';
4
- import { createResource } from './resource.js';
5
- import { __krateBoundary, __krateLazy, __krateLazyPayload, __krateRead, __krateTransitionStarter, __krateUse, createBoundary, createDeferredValue, createExternalStore, getBoundary, isInTransition, readValue, registerBoundary, resetBoundaries, runAsyncTransition, setFallbackThrottle, setLazyChunkImporter, setSuspenseErrorHandler, startTransition, trackThenable, isSuspenseSignal, } from './suspense.js';
6
- async function flush(n = 20) {
7
- for (let i = 0; i < n; i++)
8
- await Promise.resolve();
9
- }
10
- function deferred() {
11
- let resolve;
12
- let reject;
13
- const promise = new Promise((res, rej) => {
14
- resolve = res;
15
- reject = rej;
16
- });
17
- return { promise, resolve, reject };
18
- }
19
- // A boundary whose visible state is recorded in `shown`.
20
- function testBoundary(id = 'b', initialContent = true) {
21
- const shown = [];
22
- const b = createBoundary(id, (content) => shown.push(content ? 'content' : 'fallback'), { initialContent });
23
- return { b, shown };
24
- }
25
- // Reveal throttling has its own test; elsewhere reveals are immediate.
26
- beforeEach(() => setFallbackThrottle(0));
27
- afterEach(() => {
28
- setSuspenseErrorHandler(null);
29
- resetBoundaries();
30
- vi.useRealTimers();
31
- });
32
- describe('thenables and use()', () => {
33
- it('caches a resolved promise by identity', async () => {
34
- const p = Promise.resolve(7);
35
- let thrown;
36
- try {
37
- readValue(p);
38
- }
39
- catch (e) {
40
- thrown = e;
41
- }
42
- expect(isSuspenseSignal(thrown)).toBe(true);
43
- await flush();
44
- expect(readValue(p)).toBe(7);
45
- expect(readValue(p)).toBe(7);
46
- });
47
- it('throws the rejection reason', async () => {
48
- const p = Promise.reject(new Error('boom'));
49
- trackThenable(p);
50
- p.catch(() => { });
51
- await flush();
52
- expect(() => readValue(p)).toThrow('boom');
53
- });
54
- it('reads contexts and passes plain values through', () => {
55
- const Ctx = createContext('dflt');
56
- expect(readValue(Ctx)).toBe('dflt');
57
- // The marker wins even for a context that also looks like a thenable.
58
- const odd = Object.assign(createContext('marked'), { then() { } });
59
- expect(readValue(odd)).toBe('marked');
60
- expect(readValue(3)).toBe(3);
61
- });
62
- it('honours React-style pre-resolved thenables', () => {
63
- const t = { status: 'fulfilled', value: 'x', then() { } };
64
- expect(readValue(t)).toBe('x');
65
- });
66
- });
67
- describe('boundaries', () => {
68
- it('shows the fallback while suspended and reveals after settling', async () => {
69
- const { b, shown } = testBoundary();
70
- const d = deferred();
71
- b.suspend(d.promise);
72
- expect(shown).toEqual(['fallback']);
73
- expect(b.showingContent()).toBe(false);
74
- d.resolve();
75
- await flush();
76
- expect(shown).toEqual(['fallback', 'content']);
77
- });
78
- it('reveals siblings together', async () => {
79
- const { b, shown } = testBoundary();
80
- const a = deferred();
81
- const c = deferred();
82
- b.suspend(a.promise);
83
- b.suspend(c.promise);
84
- a.resolve();
85
- await flush();
86
- expect(b.showingContent()).toBe(false);
87
- c.resolve();
88
- await flush();
89
- expect(shown).toEqual(['fallback', 'content']);
90
- });
91
- it('keeps revealed content during a transition and waits for it', async () => {
92
- const { b, shown } = testBoundary();
93
- const d = deferred();
94
- const tr = runAsyncTransition(() => {
95
- b.suspend(d.promise, isInTransition());
96
- });
97
- expect(shown).toEqual([]);
98
- await flush();
99
- expect(tr.pending()).toBe(true);
100
- d.resolve();
101
- await flush();
102
- expect(tr.pending()).toBe(false);
103
- expect(shown).toEqual([]);
104
- });
105
- });
106
- describe('component runs with use()', () => {
107
- it('suspends the boundary until the promise resolves, then renders with the value', async () => {
108
- const { b, shown } = testBoundary('x', false);
109
- const d = deferred();
110
- const rendered = [];
111
- // Stub the compiled boundary lookup with the test controller.
112
- runIn(b, () => {
113
- const user = __krateUse(() => __krateRead(d.promise));
114
- createEffect(() => rendered.push(user().name));
115
- });
116
- expect(rendered).toEqual([]);
117
- expect(shown).toEqual([]);
118
- d.resolve({ name: 'Ada' });
119
- await flush();
120
- expect(rendered).toEqual(['Ada']);
121
- expect(shown).toEqual(['content']);
122
- });
123
- it('reuses an uncached promise by position on retry', async () => {
124
- let created = 0;
125
- const resolvers = [];
126
- const make = () => {
127
- created++;
128
- return new Promise((r) => resolvers.push(r));
129
- };
130
- let value = null;
131
- __krateBoundary(null, () => {
132
- value = __krateUse(() => __krateRead(make()));
133
- });
134
- expect(created).toBe(1);
135
- resolvers[0](5);
136
- await flush();
137
- // The retry evaluated make() again but kept the first promise.
138
- expect(created).toBe(3);
139
- expect(value()).toBe(5);
140
- });
141
- it('allows conditional and repeated use() in one expression', async () => {
142
- const a = Promise.resolve('a');
143
- const c = Promise.resolve('c');
144
- const [flag, setFlag] = createSignal(true);
145
- let v = null;
146
- __krateBoundary(null, () => {
147
- v = __krateUse(() => (flag() ? __krateRead(a) : __krateRead(c)));
148
- });
149
- await flush();
150
- expect(v()).toBe('a');
151
- setFlag(false);
152
- await flush();
153
- expect(v()).toBe('c');
154
- });
155
- it('routes a rejected promise to the error handler', async () => {
156
- const errors = [];
157
- setSuspenseErrorHandler((e) => {
158
- errors.push(e);
159
- return true;
160
- });
161
- const d = deferred();
162
- __krateBoundary(null, () => {
163
- __krateUse(() => __krateRead(d.promise));
164
- });
165
- d.reject(new Error('nope'));
166
- await flush();
167
- expect(errors.map((e) => e.message)).toEqual(['nope']);
168
- });
169
- it('suspends nested boundaries independently (innermost wins)', async () => {
170
- const outer = testBoundary('outer', false);
171
- const inner = testBoundary('inner', false);
172
- const d = deferred();
173
- runIn(outer.b, () => { });
174
- runIn(inner.b, () => {
175
- __krateUse(() => __krateRead(d.promise));
176
- });
177
- await flush();
178
- expect(outer.shown).toEqual(['content']);
179
- expect(inner.shown).toEqual([]);
180
- d.resolve('ok');
181
- await flush();
182
- expect(inner.shown).toEqual(['content']);
183
- });
184
- it('shows the fallback again for a non-transition update and keeps content in a transition', async () => {
185
- const { b, shown } = testBoundary('r', false);
186
- const first = Promise.resolve('one');
187
- const [p, setP] = createSignal(first);
188
- let text = null;
189
- runIn(b, () => {
190
- text = __krateUse(() => __krateRead(p()));
191
- });
192
- await flush();
193
- expect(shown).toEqual(['content']);
194
- expect(text()).toBe('one');
195
- // Urgent update: the fallback comes back while the new promise is pending.
196
- const second = deferred();
197
- setP(second.promise);
198
- await flush();
199
- expect(shown).toEqual(['content', 'fallback']);
200
- expect(text()).toBe('one');
201
- second.resolve('two');
202
- await flush();
203
- expect(shown).toEqual(['content', 'fallback', 'content']);
204
- expect(text()).toBe('two');
205
- // Transition update: the old content stays and the transition waits.
206
- const third = deferred();
207
- const [pending, setPending] = createSignal(false);
208
- const start = __krateTransitionStarter(setPending);
209
- start(() => setP(third.promise));
210
- await flush();
211
- expect(pending()).toBe(true);
212
- expect(shown).toEqual(['content', 'fallback', 'content']);
213
- expect(text()).toBe('two');
214
- third.resolve('three');
215
- await flush();
216
- expect(text()).toBe('three');
217
- expect(pending()).toBe(false);
218
- expect(shown).toEqual(['content', 'fallback', 'content']);
219
- });
220
- });
221
- // Runs fn as a compiled component inside a test boundary.
222
- function runIn(b, fn) {
223
- registerBoundary(b);
224
- __krateBoundary(b.id, fn);
225
- }
226
- describe('resources under Suspense', () => {
227
- it('suspends the creating component boundary while a read resource loads', async () => {
228
- const { b, shown } = testBoundary('res', true);
229
- const d = deferred();
230
- let read = null;
231
- runIn(b, () => {
232
- const [user] = createResource(() => 1, () => d.promise);
233
- read = user;
234
- createEffect(() => user());
235
- });
236
- expect(shown).toEqual(['fallback']);
237
- d.resolve('Ada');
238
- await flush();
239
- expect(read()).toBe('Ada');
240
- expect(shown).toEqual(['fallback', 'content']);
241
- });
242
- });
243
- describe('async transitions', () => {
244
- beforeEach(() => vi.useFakeTimers());
245
- it('keeps isPending true until an async action settles, including work after await', async () => {
246
- const [pending, setPending] = createSignal(false);
247
- const start = __krateTransitionStarter(setPending);
248
- const [count, setCount] = createSignal(0);
249
- start(async () => {
250
- await new Promise((r) => setTimeout(r, 100));
251
- setCount(1);
252
- await new Promise((r) => setTimeout(r, 100));
253
- });
254
- expect(pending()).toBe(true);
255
- await vi.advanceTimersByTimeAsync(150);
256
- expect(count()).toBe(1);
257
- expect(pending()).toBe(true);
258
- await vi.advanceTimersByTimeAsync(100);
259
- expect(pending()).toBe(false);
260
- });
261
- it('stays pending until all overlapping transitions settle', async () => {
262
- const [pending, setPending] = createSignal(false);
263
- const start = __krateTransitionStarter(setPending);
264
- start(() => new Promise((r) => setTimeout(r, 100)));
265
- start(() => new Promise((r) => setTimeout(r, 300)));
266
- await vi.advanceTimersByTimeAsync(150);
267
- expect(pending()).toBe(true);
268
- await vi.advanceTimersByTimeAsync(200);
269
- expect(pending()).toBe(false);
270
- });
271
- it('settles a synchronous transition after its effect flush', async () => {
272
- const [pending, setPending] = createSignal(false);
273
- const start = __krateTransitionStarter(setPending);
274
- const [v, setV] = createSignal(0);
275
- let seenInTransition = false;
276
- createEffect(() => {
277
- if (v() > 0)
278
- seenInTransition = isInTransition();
279
- });
280
- start(() => setV(1));
281
- expect(pending()).toBe(true);
282
- await flush();
283
- expect(seenInTransition).toBe(true);
284
- expect(pending()).toBe(false);
285
- expect(isInTransition()).toBe(false);
286
- });
287
- it('top-level startTransition supports async functions', async () => {
288
- const [v, setV] = createSignal('a');
289
- startTransition(async () => {
290
- await new Promise((r) => setTimeout(r, 50));
291
- setV('b');
292
- });
293
- await vi.advanceTimersByTimeAsync(60);
294
- expect(v()).toBe('b');
295
- });
296
- it('runAsyncTransition exposes pending and a promise and reports errors', async () => {
297
- const errors = [];
298
- setSuspenseErrorHandler((e) => {
299
- errors.push(e);
300
- return true;
301
- });
302
- const ok = runAsyncTransition(() => new Promise((r) => setTimeout(r, 10)));
303
- const bad = runAsyncTransition(async () => {
304
- throw new Error('fail');
305
- });
306
- expect(ok.pending()).toBe(true);
307
- await vi.advanceTimersByTimeAsync(20);
308
- await expect(ok.promise).resolves.toBeUndefined();
309
- await expect(bad.promise).rejects.toThrow('fail');
310
- expect(bad.pending()).toBe(false);
311
- expect(errors).toHaveLength(1);
312
- });
313
- });
314
- describe('deferred values', () => {
315
- beforeEach(() => vi.useFakeTimers());
316
- it('lags behind an urgent update and catches up in a later task', async () => {
317
- const [q, setQ] = createSignal('a');
318
- const d = createDeferredValue(q);
319
- expect(d()).toBe('a');
320
- setQ('ab');
321
- await flush();
322
- expect(d()).toBe('a');
323
- setQ('abc');
324
- await flush();
325
- await vi.advanceTimersByTimeAsync(1);
326
- expect(d()).toBe('abc');
327
- });
328
- it('starts from initialValue and then switches to the value', async () => {
329
- const [q] = createSignal('real');
330
- const d = createDeferredValue(q, 'initial');
331
- expect(d()).toBe('initial');
332
- await vi.advanceTimersByTimeAsync(1);
333
- expect(d()).toBe('real');
334
- });
335
- it('does not defer updates made inside a transition', async () => {
336
- const [q, setQ] = createSignal(1);
337
- const d = createDeferredValue(q);
338
- startTransition(() => setQ(2));
339
- await flush();
340
- expect(d()).toBe(2);
341
- });
342
- });
343
- describe('external stores', () => {
344
- it('subscribes, tracks snapshots by Object.is and catches changes before subscribe', async () => {
345
- let state = { n: 1 };
346
- const listeners = new Set();
347
- let subscribed = 0;
348
- let unsubscribed = 0;
349
- const subscribe = (fn) => {
350
- subscribed++;
351
- // A change between the first read and the subscription.
352
- state = { n: 2 };
353
- listeners.add(fn);
354
- return () => {
355
- unsubscribed++;
356
- listeners.delete(fn);
357
- };
358
- };
359
- const snap = createExternalStore(subscribe, () => state);
360
- expect(subscribed).toBe(1);
361
- expect(snap().n).toBe(2);
362
- const before = snap();
363
- listeners.forEach((fn) => fn());
364
- expect(snap()).toBe(before);
365
- state = { n: 3 };
366
- listeners.forEach((fn) => fn());
367
- expect(snap().n).toBe(3);
368
- const { disposeAll } = await import('./signal.js');
369
- disposeAll();
370
- expect(unsubscribed).toBe(1);
371
- });
372
- it('stores function snapshots without calling them', () => {
373
- const fn = () => 1;
374
- const snap = createExternalStore(() => () => { }, () => fn);
375
- expect(snap()).toBe(fn);
376
- });
377
- });
378
- describe('lazy chunks', () => {
379
- it('holds the boundary until the chunk loads', async () => {
380
- const d = deferred();
381
- setLazyChunkImporter(() => d.promise);
382
- installFindSlot('<!--suspense:L--><!--k:sL--><p>fallback</p><!--/k:sL--><!--/suspense:L--><template data-kc="sL" data-kb="c"><p>content</p></template>');
383
- const b = getBoundary('L');
384
- __krateLazy('/chunk-a.js', 'L');
385
- await flush();
386
- expect(b.showingContent()).toBe(false);
387
- d.resolve({});
388
- await flush();
389
- expect(b.showingContent()).toBe(true);
390
- expect(document.body.textContent).toContain('content');
391
- expect(document.body.querySelector('#root > p')?.textContent).toBe('content');
392
- });
393
- it('reports a failed chunk to the error handler', async () => {
394
- const errors = [];
395
- setSuspenseErrorHandler((e) => {
396
- errors.push(e);
397
- return true;
398
- });
399
- setLazyChunkImporter(() => Promise.reject(new Error('404')));
400
- __krateLazy('/missing.js', null);
401
- await flush();
402
- expect(errors[0].message).toBe('404');
403
- });
404
- });
405
- describe('reveal throttling', () => {
406
- it('keeps a fallback at least the throttle time before revealing', async () => {
407
- vi.useFakeTimers();
408
- setFallbackThrottle(300);
409
- const { b, shown } = testBoundary('th', true);
410
- const d = deferred();
411
- b.suspend(d.promise);
412
- expect(shown).toEqual(['fallback']);
413
- d.resolve();
414
- await vi.advanceTimersByTimeAsync(100);
415
- expect(shown).toEqual(['fallback']);
416
- await vi.advanceTimersByTimeAsync(250);
417
- expect(shown).toEqual(['fallback', 'content']);
418
- });
419
- });
420
- describe('lazy loaders', () => {
421
- it('calls a promise loader once and suspends the run until it resolves', async () => {
422
- const { b, shown } = testBoundary('lp', false);
423
- let calls = 0;
424
- const d = deferred();
425
- const loader = () => {
426
- calls++;
427
- return d.promise;
428
- };
429
- let ran = 0;
430
- runIn(b, () => {
431
- __krateRead(__krateLazyPayload(loader));
432
- ran++;
433
- });
434
- await flush();
435
- expect(ran).toBe(0);
436
- d.resolve({ default: () => null });
437
- await flush();
438
- expect(ran).toBe(1);
439
- expect(calls).toBe(1);
440
- expect(shown).toEqual(['content']);
441
- });
442
- it('rejects a loader that does not resolve to a component', async () => {
443
- const errors = [];
444
- setSuspenseErrorHandler((e) => {
445
- errors.push(e);
446
- return true;
447
- });
448
- __krateBoundary(null, () => {
449
- __krateRead(__krateLazyPayload(() => Promise.resolve({ default: 1 })));
450
- });
451
- await flush();
452
- expect(String(errors[0].message)).toContain('default export');
453
- });
454
- it('starts loading a chunk only when its boundary is connected', async () => {
455
- let imported = 0;
456
- setLazyChunkImporter(() => {
457
- imported++;
458
- return Promise.resolve({});
459
- });
460
- installFindSlot('');
461
- const tpl = document.createElement('template');
462
- tpl.innerHTML = '<!--suspense:H--><!--k:sH--><p>wait</p><!--/k:sH--><!--/suspense:H-->';
463
- const start = tpl.content.childNodes[1];
464
- globalThis.findSlot = () => start;
465
- __krateLazy('/hidden-chunk.js', 'H');
466
- await flush();
467
- expect(imported).toBe(0);
468
- document.getElementById('root').appendChild(tpl.content);
469
- await new Promise((r) => setTimeout(r, 0));
470
- await flush();
471
- expect(imported).toBe(1);
472
- });
473
- });
474
- describe('compiled boundary refs', () => {
475
- it('attaches refs of content revealed from its template', async () => {
476
- const enter = vi.fn();
477
- globalThis.krateRefsEnter = enter;
478
- installFindSlot('<!--suspense:R--><!--k:sR--><p>wait</p><!--/k:sR--><!--/suspense:R--><template data-kc="sR" data-kb="c"><form></form></template>');
479
- const b = getBoundary('R');
480
- b.hold();
481
- b.release();
482
- await flush();
483
- expect(document.querySelector('#root form')).not.toBeNull();
484
- expect(enter).toHaveBeenCalled();
485
- delete globalThis.krateRefsEnter;
486
- });
487
- });
488
- describe('compiled boundary markers', () => {
489
- it('swaps the fallback and content around the boundary slot', async () => {
490
- installFindSlot('<!--suspense:M--><!--k:sM--><p>content</p><!--/k:sM--><!--/suspense:M--><template data-kc="sM" data-kb="a"><p>wait</p></template>');
491
- const b = getBoundary('M');
492
- expect(b.showingContent()).toBe(true);
493
- const d = deferred();
494
- b.suspend(d.promise);
495
- // As in React: revealed content is hidden in place, the fallback follows.
496
- const ps = Array.from(document.querySelectorAll('#root > p'));
497
- expect(ps.map((p) => p.textContent)).toEqual(['content', 'wait']);
498
- expect(ps[0].style.getPropertyValue('display')).toBe('none');
499
- expect(ps[0].style.getPropertyPriority('display')).toBe('important');
500
- d.resolve();
501
- await flush();
502
- const after = Array.from(document.querySelectorAll('#root > p'));
503
- expect(after.map((p) => p.textContent)).toEqual(['content']);
504
- expect(after[0]).toBe(ps[0]);
505
- expect(after[0].style.getPropertyValue('display')).toBe('');
506
- });
507
- });
508
- function installFindSlot(html) {
509
- document.body.innerHTML = '<div id="root">' + html + '</div>';
510
- globalThis.findSlot = (id) => {
511
- const w = document.createTreeWalker(document.body, NodeFilter.SHOW_COMMENT);
512
- while (w.nextNode()) {
513
- if (w.currentNode.nodeValue === 'k:' + id)
514
- return w.currentNode;
515
- }
516
- return null;
517
- };
518
- }