@sigil-dev/grimoire 0.9.3 → 0.9.5
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/index.ts +37 -35
- package/package.json +9 -12
- package/preload.js +3 -3
- package/server.ts +19 -19
- package/src/client/deps.ts +36 -19
- package/src/client/enhance.ts +102 -98
- package/src/client/head.ts +30 -30
- package/src/client/index.ts +2 -2
- package/src/client/router.ts +174 -68
- package/src/client/scope.ts +22 -22
- package/src/dev/compile-module.ts +215 -163
- package/src/dev/effect-registry.ts +23 -23
- package/src/dev/graph.ts +109 -114
- package/src/dev/hmr-client.ts +202 -185
- package/src/dev/hmr-server.ts +261 -187
- package/src/dev/loader.ts +120 -47
- package/src/dev/paths.ts +14 -14
- package/src/dev/resolve.ts +70 -0
- package/src/dev/runtime-bundle.ts +52 -52
- package/src/dev/scan.ts +78 -0
- package/src/dev/watcher.ts +69 -47
- package/src/env/dynamic-private.ts +19 -19
- package/src/env/dynamic-public.ts +22 -22
- package/src/env/index.ts +25 -25
- package/src/env/plugin.ts +19 -19
- package/src/env/private.ts +5 -5
- package/src/env/public.ts +7 -7
- package/src/env/typegen.ts +61 -61
- package/src/headers.ts +114 -114
- package/src/integrations/vite.ts +73 -73
- package/src/logger/index.ts +4 -4
- package/src/logger/instance.ts +6 -6
- package/src/logger/logger.ts +113 -113
- package/src/logger/request-middleware.ts +24 -24
- package/src/logger/transports.ts +117 -117
- package/src/logger/types.ts +39 -39
- package/src/rendering/head.ts +52 -47
- package/src/rendering/hydrate.ts +159 -138
- package/src/rendering/index.ts +140 -18
- package/src/rendering/ssrPlugin.ts +78 -69
- package/src/routing/manifest-gen.ts +13 -1
- package/src/routing/router.ts +135 -109
- package/src/routing/scanner.ts +196 -141
- package/src/routing/transform-routes.ts +102 -102
- package/src/sentinels/error.ts +52 -52
- package/src/sentinels/fail.ts +41 -41
- package/src/sentinels/redirect.ts +35 -35
- package/src/server/build.ts +250 -243
- package/src/server/context.ts +10 -10
- package/src/server/cookie-utils.ts +66 -66
- package/src/server/coordinator.ts +364 -364
- package/src/server/hooks.ts +114 -114
- package/src/server/index.ts +711 -655
- package/src/server/interactive.ts +56 -56
- package/src/server/plugins.ts +121 -121
- package/src/server/worker.ts +58 -58
- package/src/typegen/index.ts +389 -356
- package/src/types.ts +286 -282
- package/test/client-router.test.ts +295 -44
- package/test/context.test.ts +52 -52
- package/test/env.test.ts +90 -90
- package/test/exports.test.ts +37 -37
- package/test/fail.test.ts +46 -46
- package/test/headers.test.ts +100 -100
- package/test/hmr.test.ts +426 -0
- package/test/hydration.test.ts +119 -119
- package/test/middleware.test.ts +223 -223
- package/test/preload.ts +5 -5
- package/test/redirect-error.test.ts +112 -112
- package/test/rendering.test.ts +719 -615
- package/test/routing.test.ts +197 -81
- package/test/scanning.test.ts +314 -200
- package/test/scope.test.ts +195 -195
- package/test/server.test.ts +360 -249
- package/test/streaming.test.ts +242 -125
- package/test/transform-routes.test.ts +87 -84
- package/test/typegen.test.ts +697 -682
- package/tsconfig.json +10 -10
package/src/client/router.ts
CHANGED
|
@@ -1,18 +1,16 @@
|
|
|
1
1
|
import { popHydrationNodes, pushHydrationNodes } from "@sigil-dev/runtime";
|
|
2
|
+
import {
|
|
3
|
+
clearDeps,
|
|
4
|
+
depends,
|
|
5
|
+
getDeps,
|
|
6
|
+
setDeps,
|
|
7
|
+
startCollecting,
|
|
8
|
+
stopCollecting,
|
|
9
|
+
} from "./deps";
|
|
2
10
|
import { withEffectScope } from "./scope";
|
|
3
11
|
|
|
4
|
-
//
|
|
5
|
-
|
|
6
|
-
let _collectingDeps: Set<string> | null = null;
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Call from inside a load() function to register a dependency key.
|
|
10
|
-
* When invalidate(key) is called, any route that depends on that key
|
|
11
|
-
* will refetch if it's the current route.
|
|
12
|
-
*/
|
|
13
|
-
export function depends(key: string): void {
|
|
14
|
-
_collectingDeps?.add(key);
|
|
15
|
-
}
|
|
12
|
+
// re-exported for app code that imports depends() from the router module
|
|
13
|
+
export { depends };
|
|
16
14
|
|
|
17
15
|
async function refetchCurrent(): Promise<void> {
|
|
18
16
|
if (!lastState) return;
|
|
@@ -22,9 +20,11 @@ async function refetchCurrent(): Promise<void> {
|
|
|
22
20
|
export async function invalidate(...keys: string[]): Promise<void> {
|
|
23
21
|
const current = lastState?.pattern;
|
|
24
22
|
if (!current) return;
|
|
25
|
-
const deps =
|
|
26
|
-
|
|
27
|
-
if (keys.some((k) => deps.has(k)))
|
|
23
|
+
const deps = getDeps(current);
|
|
24
|
+
// no recorded deps → conservative refetch; otherwise only on intersection
|
|
25
|
+
if (!deps || deps.size === 0 || keys.some((k) => deps.has(k))) {
|
|
26
|
+
await refetchCurrent();
|
|
27
|
+
}
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
export async function invalidateAll(): Promise<void> {
|
|
@@ -38,7 +38,9 @@ type PageComponent = ((props: any) => any) & {
|
|
|
38
38
|
};
|
|
39
39
|
/** Manifest entries are lazy (`{ __lazy: () => import(...) }`); plain
|
|
40
40
|
* component functions are also accepted (tests, HMR replacements). */
|
|
41
|
-
type RouteEntry =
|
|
41
|
+
type RouteEntry =
|
|
42
|
+
| PageComponent
|
|
43
|
+
| { __lazy: () => Promise<{ default: PageComponent }> };
|
|
42
44
|
|
|
43
45
|
let routeMap: Record<string, RouteEntry> = {};
|
|
44
46
|
const resolvedRoutes: Record<string, PageComponent> = {};
|
|
@@ -59,11 +61,6 @@ let beforeNavigateCb: ((url: URL) => boolean | undefined) | null = null;
|
|
|
59
61
|
let onNavigateCb: ((url: URL) => void) | null = null;
|
|
60
62
|
let afterNavigateCb: ((url: URL) => void) | null = null;
|
|
61
63
|
|
|
62
|
-
// // HMR
|
|
63
|
-
// (globalThis as any).__grimoire_routes__ = routeMap;
|
|
64
|
-
// (globalThis as any).__grimoire_current_pattern__ = currentPath;
|
|
65
|
-
// (globalThis as any).__grimoire_navigate__ = navigate;
|
|
66
|
-
|
|
67
64
|
export async function hmrRerender(): Promise<void> {
|
|
68
65
|
if (!lastState) {
|
|
69
66
|
location.reload();
|
|
@@ -84,7 +81,29 @@ export async function hmrRerender(): Promise<void> {
|
|
|
84
81
|
document.getElementById("grimoire-root")?.replaceChildren(rootNode);
|
|
85
82
|
}
|
|
86
83
|
|
|
87
|
-
|
|
84
|
+
/**
|
|
85
|
+
* Dev-only surface the HMR client drives. Route entries are replaced rather
|
|
86
|
+
* than mutated in place so a swapped module also wins for later navigations,
|
|
87
|
+
* and the resolved-module cache is dropped with them.
|
|
88
|
+
*/
|
|
89
|
+
export interface HmrControl {
|
|
90
|
+
setRoute(pattern: string, entry: RouteEntry): void;
|
|
91
|
+
setLayout(path: string, component: (props: any) => any): void;
|
|
92
|
+
rerender(): Promise<void>;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const hmrControl: HmrControl = {
|
|
96
|
+
setRoute(pattern, entry) {
|
|
97
|
+
routeMap[pattern] = entry;
|
|
98
|
+
delete resolvedRoutes[pattern];
|
|
99
|
+
},
|
|
100
|
+
setLayout(path, component) {
|
|
101
|
+
const existing = layoutList.find((l) => l.path === path);
|
|
102
|
+
if (existing) existing.component = component;
|
|
103
|
+
else layoutList.push({ path, component });
|
|
104
|
+
},
|
|
105
|
+
rerender: hmrRerender,
|
|
106
|
+
};
|
|
88
107
|
|
|
89
108
|
export function beforeNavigate(cb: (url: URL) => boolean | undefined): void {
|
|
90
109
|
beforeNavigateCb = cb;
|
|
@@ -106,12 +125,15 @@ function saveScrollPosition(): void {
|
|
|
106
125
|
}
|
|
107
126
|
|
|
108
127
|
function restoreScrollPosition(path: string): void {
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
128
|
+
const y = scrollPositions.get(path) ?? 0;
|
|
129
|
+
window.scrollTo(0, y);
|
|
130
|
+
// re-assert after layout settles (late effects/images can shift content)
|
|
131
|
+
requestAnimationFrame(() => window.scrollTo(0, y));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function scrollToTop(): void {
|
|
135
|
+
window.scrollTo(0, 0);
|
|
136
|
+
requestAnimationFrame(() => window.scrollTo(0, 0));
|
|
115
137
|
}
|
|
116
138
|
|
|
117
139
|
// C3: Preloading
|
|
@@ -124,12 +146,20 @@ function setupPreload(): void {
|
|
|
124
146
|
const href = a.getAttribute("href");
|
|
125
147
|
if (!href) return;
|
|
126
148
|
if (/^(https?:\/\/|\/\/|#|mailto:|tel:)/.test(href)) return;
|
|
149
|
+
if (a.getAttribute("data-sigil-preload") === "off") return;
|
|
150
|
+
|
|
151
|
+
// Only prefetch links that resolve to a known client route — external
|
|
152
|
+
// pages and non-page endpoints (API routes, redirects) are skipped.
|
|
153
|
+
try {
|
|
154
|
+
const url = new URL(href, location.origin);
|
|
155
|
+
if (!findPattern(url.pathname)) return;
|
|
156
|
+
} catch {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
127
159
|
|
|
128
|
-
const preloadData = a.hasAttribute("data-sigil-preload-data");
|
|
129
160
|
const preloadCode = a.hasAttribute("data-sigil-preload-code");
|
|
130
|
-
if (!preloadData && !preloadCode) return;
|
|
131
161
|
|
|
132
|
-
|
|
162
|
+
clearTimeout(preloadTimer);
|
|
133
163
|
preloadTimer = setTimeout(() => {
|
|
134
164
|
if (preloadCode) {
|
|
135
165
|
const link = document.createElement("link");
|
|
@@ -137,20 +167,19 @@ function setupPreload(): void {
|
|
|
137
167
|
link.href = href;
|
|
138
168
|
document.head.appendChild(link);
|
|
139
169
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
}
|
|
170
|
+
// data prefetch is the default; -code is the explicit escalation
|
|
171
|
+
fetch(href, {
|
|
172
|
+
headers: { "x-grimoire-navigate": "1" },
|
|
173
|
+
redirect: "manual",
|
|
174
|
+
priority: "low",
|
|
175
|
+
}).catch(() => {});
|
|
147
176
|
}, 80);
|
|
148
177
|
});
|
|
149
178
|
|
|
150
179
|
document.addEventListener("mouseout", (e) => {
|
|
151
180
|
const a = (e.target as Element).closest("a");
|
|
152
181
|
if (!a) return;
|
|
153
|
-
|
|
182
|
+
clearTimeout(preloadTimer);
|
|
154
183
|
});
|
|
155
184
|
}
|
|
156
185
|
|
|
@@ -191,6 +220,7 @@ function buildRouteTree(state: typeof lastState, Page: PageComponent): any {
|
|
|
191
220
|
return LayoutComponent({
|
|
192
221
|
data: currentLayoutData,
|
|
193
222
|
params,
|
|
223
|
+
path: pattern,
|
|
194
224
|
children: childNode,
|
|
195
225
|
});
|
|
196
226
|
};
|
|
@@ -203,16 +233,24 @@ function matchClientPattern(pattern: string, pathname: string): boolean {
|
|
|
203
233
|
// haha pp and ph
|
|
204
234
|
const pp = pattern.split("/").filter(Boolean);
|
|
205
235
|
const ph = pathname.split("/").filter(Boolean);
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
if (
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
236
|
+
|
|
237
|
+
// Depth-first walk; optional segments (:x?) try consuming one segment
|
|
238
|
+
// before consuming zero, mirroring the server's matchPattern.
|
|
239
|
+
function walk(pi: number, pj: number): boolean {
|
|
240
|
+
if (pi === pp.length) return pj === ph.length;
|
|
241
|
+
const part = pp[pi]!;
|
|
242
|
+
if (part.startsWith("*")) return true;
|
|
243
|
+
const optional = part.startsWith(":") && part.endsWith("?");
|
|
244
|
+
if (optional && pj < ph.length && walk(pi + 1, pj + 1)) return true;
|
|
245
|
+
if (!optional) {
|
|
246
|
+
if (pj >= ph.length) return false;
|
|
247
|
+
if (!part.startsWith(":") && part !== ph[pj]) return false;
|
|
248
|
+
return walk(pi + 1, pj + 1);
|
|
249
|
+
}
|
|
250
|
+
return walk(pi + 1, pj);
|
|
214
251
|
}
|
|
215
|
-
|
|
252
|
+
|
|
253
|
+
return walk(0, 0);
|
|
216
254
|
}
|
|
217
255
|
|
|
218
256
|
function findPattern(pathname: string): string | null {
|
|
@@ -272,7 +310,10 @@ export async function navigate(
|
|
|
272
310
|
else document.getElementById("grimoire-root")?.replaceChildren(loadingNode);
|
|
273
311
|
}
|
|
274
312
|
|
|
275
|
-
const res = await fetch(path, {
|
|
313
|
+
const res = await fetch(path, {
|
|
314
|
+
headers: { "x-grimoire-navigate": "1" },
|
|
315
|
+
redirect: "manual",
|
|
316
|
+
});
|
|
276
317
|
if (navId !== navigationId) return;
|
|
277
318
|
const contentType = res.headers.get("content-type") ?? "";
|
|
278
319
|
if (!contentType.includes("application/json")) {
|
|
@@ -281,7 +322,7 @@ export async function navigate(
|
|
|
281
322
|
}
|
|
282
323
|
const json = await res.json();
|
|
283
324
|
if (navId !== navigationId) return;
|
|
284
|
-
const { data, layoutData, params, pattern, head } = json;
|
|
325
|
+
const { data, layoutData, params, pattern, head, deps } = json;
|
|
285
326
|
const Page = await resolveRoute(pattern);
|
|
286
327
|
if (navId !== navigationId) return;
|
|
287
328
|
if (!Page) {
|
|
@@ -292,24 +333,41 @@ export async function navigate(
|
|
|
292
333
|
let mergedData = data;
|
|
293
334
|
if (Page.load) {
|
|
294
335
|
try {
|
|
295
|
-
|
|
336
|
+
startCollecting();
|
|
296
337
|
const clientData = await Page.load({
|
|
297
338
|
params,
|
|
298
339
|
url,
|
|
299
340
|
request: new Request(url),
|
|
341
|
+
// layout loads run server-side; their data arrives in layoutData —
|
|
342
|
+
// parent() merges it outermost-first, mirroring the server context
|
|
343
|
+
parent: async () =>
|
|
344
|
+
Object.assign(
|
|
345
|
+
{},
|
|
346
|
+
...(Array.isArray(layoutData) ? layoutData : []).filter(
|
|
347
|
+
(d) => d && typeof d === "object",
|
|
348
|
+
),
|
|
349
|
+
),
|
|
300
350
|
});
|
|
301
|
-
|
|
302
|
-
|
|
351
|
+
// merge server-collected keys (page/layout server loads) with
|
|
352
|
+
// whatever this client-side load itself registered
|
|
353
|
+
setDeps(pattern, [
|
|
354
|
+
...(Array.isArray(deps) ? deps : []),
|
|
355
|
+
...stopCollecting(),
|
|
356
|
+
]);
|
|
303
357
|
mergedData = { ...clientData, ...data };
|
|
304
|
-
} catch (e:
|
|
305
|
-
|
|
306
|
-
if (e
|
|
307
|
-
window.location.href = e.location;
|
|
358
|
+
} catch (e: unknown) {
|
|
359
|
+
stopCollecting();
|
|
360
|
+
if (e && typeof e === "object" && "location" in e) {
|
|
361
|
+
window.location.href = String((e as { location: unknown }).location);
|
|
308
362
|
return;
|
|
309
363
|
}
|
|
310
364
|
console.error("[grimoire] universal load error:", e);
|
|
311
365
|
}
|
|
312
366
|
if (navId !== navigationId) return;
|
|
367
|
+
} else if (Array.isArray(deps)) {
|
|
368
|
+
// no client-side load to run — the server's collected depends() keys
|
|
369
|
+
// (from +page.server.ts / +layout.server.ts) are the whole story
|
|
370
|
+
setDeps(pattern, deps);
|
|
313
371
|
}
|
|
314
372
|
|
|
315
373
|
// track state for HMR rerender
|
|
@@ -330,7 +388,23 @@ export async function navigate(
|
|
|
330
388
|
popHydrationNodes();
|
|
331
389
|
|
|
332
390
|
updateHead(head);
|
|
333
|
-
|
|
391
|
+
// View transitions: animate the swap when the browser supports it and the
|
|
392
|
+
// user hasn't asked for reduced motion. The callback must stay synchronous.
|
|
393
|
+
const doSwap = () => {
|
|
394
|
+
document.getElementById("grimoire-root")?.replaceChildren(rootNode);
|
|
395
|
+
};
|
|
396
|
+
const doc = document as Document & {
|
|
397
|
+
startViewTransition?: (cb: () => void) => unknown;
|
|
398
|
+
};
|
|
399
|
+
if (
|
|
400
|
+
typeof doc.startViewTransition === "function" &&
|
|
401
|
+
!window.matchMedia("(prefers-reduced-motion: reduce)").matches
|
|
402
|
+
) {
|
|
403
|
+
doc.startViewTransition(doSwap);
|
|
404
|
+
} else {
|
|
405
|
+
doSwap();
|
|
406
|
+
}
|
|
407
|
+
const isRefetch = url.pathname === currentPath;
|
|
334
408
|
currentPath = url.pathname;
|
|
335
409
|
|
|
336
410
|
if (historyMode !== "none") {
|
|
@@ -343,24 +417,59 @@ export async function navigate(
|
|
|
343
417
|
if (url.hash) {
|
|
344
418
|
const target = document.getElementById(url.hash.slice(1));
|
|
345
419
|
if (target) requestAnimationFrame(() => target.scrollIntoView());
|
|
346
|
-
else
|
|
347
|
-
} else {
|
|
420
|
+
else scrollToTop();
|
|
421
|
+
} else if (historyMode === "none") {
|
|
422
|
+
// popstate: the browser already moved through history — restore what
|
|
423
|
+
// we saved for this path (back/forward semantics)
|
|
348
424
|
restoreScrollPosition(url.pathname);
|
|
425
|
+
} else if (!isRefetch) {
|
|
426
|
+
// fresh navigation: the new page starts at the top
|
|
427
|
+
scrollToTop();
|
|
349
428
|
}
|
|
429
|
+
// same-path replace (invalidate/refetch): keep the current scroll
|
|
350
430
|
afterNavigateCb?.(url);
|
|
351
431
|
}
|
|
352
432
|
|
|
433
|
+
export interface RouterExtensions {
|
|
434
|
+
/** [param=matcher] validators from src/params/*.ts (manifest-generated). */
|
|
435
|
+
matchers?: Record<string, (value: string) => boolean>;
|
|
436
|
+
/** Server-collected depends() keys per route pattern (from SSR state). */
|
|
437
|
+
deps?: Record<string, string[]>;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
let paramMatcherFns: Record<string, (value: string) => boolean> = {};
|
|
441
|
+
|
|
353
442
|
export function initRouter(
|
|
354
443
|
routes: Record<string, RouteEntry>,
|
|
355
444
|
layouts: any[] = [],
|
|
356
445
|
initialDispose?: () => void,
|
|
446
|
+
ext?: RouterExtensions,
|
|
357
447
|
) {
|
|
448
|
+
paramMatcherFns = ext?.matchers ?? {};
|
|
358
449
|
routeMap = routes;
|
|
359
450
|
layoutList = layouts;
|
|
360
451
|
disposeCurrentPage = initialDispose ?? null;
|
|
361
452
|
currentPath = location.pathname;
|
|
362
453
|
|
|
363
|
-
//
|
|
454
|
+
// the app owns scroll: the browser's automatic restoration fires after
|
|
455
|
+
// the router's own scroll and clobbers scroll-to-top / saved positions
|
|
456
|
+
if ("scrollRestoration" in history) history.scrollRestoration = "manual";
|
|
457
|
+
// dev HMR surface: available even without SSR state so the client can tell
|
|
458
|
+
// a missing router apart from a missing initial payload
|
|
459
|
+
const globals = globalThis as unknown as {
|
|
460
|
+
__grimoire_current_pattern__?: string;
|
|
461
|
+
__grimoire_routes__?: Record<string, RouteEntry>;
|
|
462
|
+
__grimoire_rerender__?: () => Promise<void>;
|
|
463
|
+
__grimoire_navigate__?: (path: string, replace?: boolean) => void;
|
|
464
|
+
__grimoire_hmr__?: HmrControl;
|
|
465
|
+
};
|
|
466
|
+
globals.__grimoire_routes__ = routeMap;
|
|
467
|
+
globals.__grimoire_rerender__ = hmrRerender;
|
|
468
|
+
globals.__grimoire_hmr__ = hmrControl;
|
|
469
|
+
globals.__grimoire_navigate__ = (path, replace) =>
|
|
470
|
+
navigate(path, replace ?? false);
|
|
471
|
+
|
|
472
|
+
// initial HMR state from SSR
|
|
364
473
|
const stateEl = document.getElementById("__grimoire_state__");
|
|
365
474
|
if (stateEl) {
|
|
366
475
|
const state = JSON.parse(stateEl.textContent!);
|
|
@@ -370,13 +479,10 @@ export function initRouter(
|
|
|
370
479
|
params: state.params,
|
|
371
480
|
pattern: state.pattern,
|
|
372
481
|
};
|
|
373
|
-
(
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
path: string,
|
|
378
|
-
replace?: boolean,
|
|
379
|
-
) => navigate(path, replace ?? false);
|
|
482
|
+
if (Array.isArray(state.deps)) {
|
|
483
|
+
setDeps(state.pattern, state.deps);
|
|
484
|
+
}
|
|
485
|
+
globals.__grimoire_current_pattern__ = state.pattern;
|
|
380
486
|
}
|
|
381
487
|
|
|
382
488
|
saveScrollPosition();
|
package/src/client/scope.ts
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Open an effect scope: run fn() and return a dispose function that tears down
|
|
3
|
-
* every createEffect registered during that call.
|
|
4
|
-
*
|
|
5
|
-
* Communicates with @sigil-dev/runtime's createEffect via globalThis.__sigilEffectScope
|
|
6
|
-
* so this file does not need to import from @sigil-dev/runtime (which would pull the
|
|
7
|
-
* runtime source into Bun.build and break the test sandbox).
|
|
8
|
-
*/
|
|
9
|
-
export function withEffectScope(fn: () => void): () => void {
|
|
10
|
-
const prev = (globalThis as any).__sigilEffectScope;
|
|
11
|
-
const disposers: (() => void)[] = [];
|
|
12
|
-
(globalThis as any).__sigilEffectScope = disposers;
|
|
13
|
-
try {
|
|
14
|
-
fn();
|
|
15
|
-
} finally {
|
|
16
|
-
(globalThis as any).__sigilEffectScope = prev;
|
|
17
|
-
}
|
|
18
|
-
return () => {
|
|
19
|
-
for (const d of disposers) d();
|
|
20
|
-
disposers.length = 0;
|
|
21
|
-
};
|
|
22
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Open an effect scope: run fn() and return a dispose function that tears down
|
|
3
|
+
* every createEffect registered during that call.
|
|
4
|
+
*
|
|
5
|
+
* Communicates with @sigil-dev/runtime's createEffect via globalThis.__sigilEffectScope
|
|
6
|
+
* so this file does not need to import from @sigil-dev/runtime (which would pull the
|
|
7
|
+
* runtime source into Bun.build and break the test sandbox).
|
|
8
|
+
*/
|
|
9
|
+
export function withEffectScope(fn: () => void): () => void {
|
|
10
|
+
const prev = (globalThis as any).__sigilEffectScope;
|
|
11
|
+
const disposers: (() => void)[] = [];
|
|
12
|
+
(globalThis as any).__sigilEffectScope = disposers;
|
|
13
|
+
try {
|
|
14
|
+
fn();
|
|
15
|
+
} finally {
|
|
16
|
+
(globalThis as any).__sigilEffectScope = prev;
|
|
17
|
+
}
|
|
18
|
+
return () => {
|
|
19
|
+
for (const d of disposers) d();
|
|
20
|
+
disposers.length = 0;
|
|
21
|
+
};
|
|
22
|
+
}
|