@tschk/moonshine-svelte 0.4.49 → 0.4.80

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/src/pre.ts CHANGED
@@ -31,6 +31,11 @@ import { flushComponentReferences } from "@tschk/moonshine-dom/component-instanc
31
31
  import { holdHistory, releaseHistory, withHistoryCleanup } from "./history";
32
32
  import { notifyTrackableUnsubscribe } from "./store-demand";
33
33
  import { queueTickWork, removeTickWork } from "./tick-queue";
34
+ import {
35
+ createAbortContext,
36
+ withAbortContext,
37
+ withoutAbortContext,
38
+ } from "./abort-signal";
34
39
  const runeConsumerKey = /* @__PURE__ */ Symbol("moonshine.svelte.consumer");
35
40
  const runeNodeKey = /* @__PURE__ */ Symbol("moonshine.svelte.node");
36
41
  type Node = {
@@ -341,7 +346,7 @@ function setupGroup<T>(
341
346
  const previous = current;
342
347
  current = group;
343
348
  try {
344
- return fn();
349
+ return withoutAbortContext(fn);
345
350
  } finally {
346
351
  current = previous;
347
352
  }
@@ -446,7 +451,14 @@ function ownedJob(
446
451
  runWithOwner(owner, () =>
447
452
  createRoot((stop) => {
448
453
  const scope = getOwner()!;
449
- stopRun = () => runWithOwner(scope, () => withHistoryCleanup(stop));
454
+ const abortCtx = phase === "pre" ? createAbortContext() : undefined;
455
+ if (abortCtx) {
456
+ onCleanup(() => abortCtx.abort());
457
+ }
458
+ stopRun = () => {
459
+ abortCtx?.abort();
460
+ runWithOwner(scope, () => withHistoryCleanup(stop));
461
+ };
450
462
  scope.context = new Map(scope.context);
451
463
  scope.context.set(renderSchedulerKey, carrier(job));
452
464
  const previous = current;
@@ -456,9 +468,11 @@ function ownedJob(
456
468
  found = collectDeps(
457
469
  () => {
458
470
  try {
459
- const cleanup = fn();
471
+ const cleanup = abortCtx
472
+ ? withAbortContext(abortCtx, fn)
473
+ : fn();
460
474
  if (phase === "pre" && typeof cleanup === "function")
461
- onCleanup(cleanup as () => void);
475
+ onCleanup(() => withoutAbortContext(cleanup as () => void));
462
476
  } catch (e) {
463
477
  failed = true;
464
478
  failure = e;
package/src/prop-id.ts ADDED
@@ -0,0 +1,43 @@
1
+ /** Deterministic tree-scoped $props.id() runtime helper. */
2
+ import {
3
+ assertNotCleanupComputation,
4
+ getOwner,
5
+ type Owner,
6
+ } from "@tschk/moonshine-dom/reactive";
7
+ import { peekComponentContext } from "@tschk/moonshine-dom/component";
8
+
9
+ const idCounters = /* @__PURE__ */ new WeakMap<
10
+ Owner,
11
+ { next: number; prefix: string }
12
+ >();
13
+
14
+ export function setRootIdPrefix(owner: Owner, idPrefix?: string): void {
15
+ let root: Owner = owner;
16
+ while (root.parent) root = root.parent;
17
+ const prefix = idPrefix ? `${idPrefix}` : "";
18
+ const existing = idCounters.get(root);
19
+ if (existing) {
20
+ existing.prefix = prefix;
21
+ } else {
22
+ idCounters.set(root, { next: 0, prefix });
23
+ }
24
+ }
25
+
26
+ export function propsId(): string {
27
+ assertNotCleanupComputation();
28
+ const owner = getOwner();
29
+ const frame = peekComponentContext();
30
+ if (!owner || owner.disposed || (frame && !frame.active)) {
31
+ throw new Error(
32
+ "$props.id() must be called during native component initialization",
33
+ );
34
+ }
35
+ let root: Owner = owner;
36
+ while (root.parent) root = root.parent;
37
+ let state = idCounters.get(root);
38
+ if (!state) {
39
+ state = { next: 0, prefix: "" };
40
+ idCounters.set(root, state);
41
+ }
42
+ return `${state.prefix}s-${state.next++}`;
43
+ }
@@ -0,0 +1,173 @@
1
+ /** Independent Svelte 5 svelte/reactivity/window reactive values on the Moonshine graph. */
2
+ import { peekComponentContext } from "@tschk/moonshine-dom/component";
3
+ import { isSSRRendering } from "@tschk/moonshine-dom/ssr-owner";
4
+ import { ReactiveValue } from "./reactivity";
5
+
6
+ function inBrowser(): boolean {
7
+ return (
8
+ typeof window !== "undefined" &&
9
+ !isSSRRendering() &&
10
+ peekComponentContext()?.mode !== "ssr"
11
+ );
12
+ }
13
+
14
+ function onWindow(
15
+ events: string | readonly string[],
16
+ handler: () => void,
17
+ ): () => void {
18
+ if (!inBrowser() || typeof window.addEventListener !== "function") {
19
+ return () => {};
20
+ }
21
+ const list = typeof events === "string" ? [events] : events;
22
+ for (const event of list) {
23
+ window.addEventListener(event, handler, { passive: true });
24
+ }
25
+ return () => {
26
+ if (
27
+ typeof window === "undefined" ||
28
+ typeof window.removeEventListener !== "function"
29
+ ) {
30
+ return;
31
+ }
32
+ for (const event of list) {
33
+ window.removeEventListener(event, handler);
34
+ }
35
+ };
36
+ }
37
+
38
+ function trackScreenPosition(
39
+ read: () => number | undefined,
40
+ ): (update: () => void) => () => void {
41
+ return (update) => {
42
+ if (!inBrowser()) return () => {};
43
+ let prev = read();
44
+ let frameId: number | undefined;
45
+ let stopped = false;
46
+ const check = () => {
47
+ if (stopped) return;
48
+ const next = read();
49
+ if (!Object.is(next, prev)) {
50
+ prev = next;
51
+ update();
52
+ }
53
+ };
54
+ const loop = () => {
55
+ if (stopped) return;
56
+ check();
57
+ if (
58
+ typeof window !== "undefined" &&
59
+ typeof window.requestAnimationFrame === "function"
60
+ ) {
61
+ frameId = window.requestAnimationFrame(loop);
62
+ }
63
+ };
64
+ if (
65
+ typeof window !== "undefined" &&
66
+ typeof window.requestAnimationFrame === "function"
67
+ ) {
68
+ frameId = window.requestAnimationFrame(loop);
69
+ }
70
+ const offResize = onWindow("resize", check);
71
+ return () => {
72
+ stopped = true;
73
+ offResize();
74
+ if (
75
+ frameId !== undefined &&
76
+ typeof window !== "undefined" &&
77
+ typeof window.cancelAnimationFrame === "function"
78
+ ) {
79
+ window.cancelAnimationFrame(frameId);
80
+ }
81
+ };
82
+ };
83
+ }
84
+
85
+ /** Reactive view of `window.innerWidth`; `undefined` on the server. */
86
+ export const innerWidth: ReactiveValue<number | undefined> = new ReactiveValue(
87
+ () => (inBrowser() ? window.innerWidth : undefined),
88
+ (update) => onWindow("resize", update),
89
+ );
90
+
91
+ /** Reactive view of `window.innerHeight`; `undefined` on the server. */
92
+ export const innerHeight: ReactiveValue<number | undefined> = new ReactiveValue(
93
+ () => (inBrowser() ? window.innerHeight : undefined),
94
+ (update) => onWindow("resize", update),
95
+ );
96
+
97
+ /** Reactive view of `window.outerWidth`; `undefined` on the server. */
98
+ export const outerWidth: ReactiveValue<number | undefined> = new ReactiveValue(
99
+ () => (inBrowser() ? window.outerWidth : undefined),
100
+ (update) => onWindow("resize", update),
101
+ );
102
+
103
+ /** Reactive view of `window.outerHeight`; `undefined` on the server. */
104
+ export const outerHeight: ReactiveValue<number | undefined> = new ReactiveValue(
105
+ () => (inBrowser() ? window.outerHeight : undefined),
106
+ (update) => onWindow("resize", update),
107
+ );
108
+
109
+ /** Reactive view of `window.screenLeft`, updated via `requestAnimationFrame`; `undefined` on the server. */
110
+ export const screenLeft: ReactiveValue<number | undefined> = new ReactiveValue(
111
+ () => (inBrowser() ? window.screenLeft : undefined),
112
+ trackScreenPosition(() => (inBrowser() ? window.screenLeft : undefined)),
113
+ );
114
+
115
+ /** Reactive view of `window.screenTop`, updated via `requestAnimationFrame`; `undefined` on the server. */
116
+ export const screenTop: ReactiveValue<number | undefined> = new ReactiveValue(
117
+ () => (inBrowser() ? window.screenTop : undefined),
118
+ trackScreenPosition(() => (inBrowser() ? window.screenTop : undefined)),
119
+ );
120
+
121
+ /** Reactive view of `window.scrollX`; `undefined` on the server. */
122
+ export const scrollX: ReactiveValue<number | undefined> = new ReactiveValue(
123
+ () => (inBrowser() ? window.scrollX : undefined),
124
+ (update) => onWindow("scroll", update),
125
+ );
126
+
127
+ /** Reactive view of `window.scrollY`; `undefined` on the server. */
128
+ export const scrollY: ReactiveValue<number | undefined> = new ReactiveValue(
129
+ () => (inBrowser() ? window.scrollY : undefined),
130
+ (update) => onWindow("scroll", update),
131
+ );
132
+
133
+ /** Reactive view of `navigator.onLine`; `undefined` on the server. */
134
+ export const online: ReactiveValue<boolean | undefined> = new ReactiveValue(
135
+ () =>
136
+ inBrowser() && typeof navigator !== "undefined"
137
+ ? navigator.onLine
138
+ : undefined,
139
+ (update) => onWindow(["online", "offline"], update),
140
+ );
141
+
142
+ /** Reactive view of `window.devicePixelRatio`; `undefined` on the server. */
143
+ export const devicePixelRatio: { readonly current: number | undefined } =
144
+ new ReactiveValue(
145
+ () => (inBrowser() ? window.devicePixelRatio : undefined),
146
+ (update) => {
147
+ if (!inBrowser()) return () => {};
148
+ let cleanupMedia: (() => void) | undefined;
149
+ const listenMedia = () => {
150
+ cleanupMedia?.();
151
+ cleanupMedia = undefined;
152
+ if (typeof window.matchMedia !== "function") return;
153
+ const dpr = window.devicePixelRatio;
154
+ if (typeof dpr !== "number" || !Number.isFinite(dpr)) return;
155
+ const mql = window.matchMedia(`(resolution: ${dpr}dppx)`);
156
+ if (!mql || typeof mql.addEventListener !== "function") return;
157
+ mql.addEventListener("change", onChange, { once: true });
158
+ cleanupMedia = () => {
159
+ mql.removeEventListener("change", onChange);
160
+ };
161
+ };
162
+ const onChange = () => {
163
+ update();
164
+ listenMedia();
165
+ };
166
+ listenMedia();
167
+ const offResize = onWindow("resize", onChange);
168
+ return () => {
169
+ offResize();
170
+ cleanupMedia?.();
171
+ };
172
+ },
173
+ );