@tschk/moonshine-dom 0.0.0-stage → 0.4.49
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/LICENSE +15 -0
- package/README.md +467 -2
- package/package.json +62 -4
- package/src/attributes.ts +56 -0
- package/src/binding.ts +430 -0
- package/src/cleanup-order.ts +16 -0
- package/src/component-instance.ts +291 -0
- package/src/component.ts +187 -0
- package/src/control.ts +469 -0
- package/src/custom-element.ts +54 -0
- package/src/dom.ts +870 -0
- package/src/error.ts +59 -0
- package/src/framework-lazy.ts +20 -0
- package/src/framework-native.ts +141 -0
- package/src/framework-source.ts +28 -0
- package/src/head-ssr.ts +108 -0
- package/src/head.ts +34 -0
- package/src/html.ts +54 -0
- package/src/jsx-types.d.ts +19 -0
- package/src/platform.d.ts +4 -0
- package/src/platform.js +4 -0
- package/src/reactive.ts +693 -0
- package/src/render-scheduler.ts +17 -0
- package/src/resource-ssr.ts +73 -0
- package/src/resource.ts +195 -0
- package/src/rotating-text.ts +38 -0
- package/src/ssr-attempt.ts +64 -0
- package/src/ssr-owner.ts +38 -0
- package/src/ssr.ts +874 -0
- package/src/store.ts +527 -0
- package/src/style.ts +30 -0
- package/src/template-retention.ts +4 -0
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/** Synchronous server resources: sources are read, fetchers never run. */
|
|
2
|
+
import { peekComponentContext } from "./component";
|
|
3
|
+
import { untrack, type Accessor } from "./reactive";
|
|
4
|
+
import type {
|
|
5
|
+
Resource,
|
|
6
|
+
ResourceFetcher,
|
|
7
|
+
ResourceOptions,
|
|
8
|
+
ResourceSource,
|
|
9
|
+
ResourceState,
|
|
10
|
+
} from "./resource";
|
|
11
|
+
import { isSSRRendering } from "./ssr-owner";
|
|
12
|
+
|
|
13
|
+
export type ServerResourceActions<T, R = unknown> = {
|
|
14
|
+
mutate: (value: T | undefined) => T | undefined;
|
|
15
|
+
refetch: (info?: R) => boolean;
|
|
16
|
+
};
|
|
17
|
+
export type ServerResourceReturn<T, R = unknown> = [
|
|
18
|
+
Resource<T>,
|
|
19
|
+
ServerResourceActions<T, R>,
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
export function createResource<T, R = unknown>(
|
|
23
|
+
fetcher: ResourceFetcher<true, T, R>,
|
|
24
|
+
options?: ResourceOptions<T>,
|
|
25
|
+
): ServerResourceReturn<T, R>;
|
|
26
|
+
export function createResource<T, S, R = unknown>(
|
|
27
|
+
source: ResourceSource<S>,
|
|
28
|
+
fetcher: ResourceFetcher<S, T, R>,
|
|
29
|
+
options?: ResourceOptions<T>,
|
|
30
|
+
): ServerResourceReturn<T, R>;
|
|
31
|
+
export function createResource<T, S, R = unknown>(
|
|
32
|
+
sourceOrFetcher: ResourceSource<S> | ResourceFetcher<true, T, R>,
|
|
33
|
+
fetcherOrOptions?: ResourceFetcher<S, T, R> | ResourceOptions<T>,
|
|
34
|
+
suppliedOptions?: ResourceOptions<T>,
|
|
35
|
+
): ServerResourceReturn<T, R> {
|
|
36
|
+
if (!isSSRRendering() && peekComponentContext()?.mode !== "ssr")
|
|
37
|
+
throw new Error(
|
|
38
|
+
"Server createResource requires an active SSR render or SSR component context",
|
|
39
|
+
);
|
|
40
|
+
const hasSource = typeof fetcherOrOptions === "function";
|
|
41
|
+
const options = (hasSource ? suppliedOptions : fetcherOrOptions) as
|
|
42
|
+
ResourceOptions<T> | undefined;
|
|
43
|
+
for (const key of Object.keys(options ?? {})) {
|
|
44
|
+
if (key !== "initialValue" && key !== "name")
|
|
45
|
+
throw new TypeError(`Unsupported native resource option: ${key}`);
|
|
46
|
+
}
|
|
47
|
+
const source = hasSource ? sourceOrFetcher : true;
|
|
48
|
+
const seeded = options != null && "initialValue" in options;
|
|
49
|
+
const state: ResourceState = seeded ? "ready" : "unresolved";
|
|
50
|
+
let value = seeded ? options?.initialValue : undefined;
|
|
51
|
+
let loading = false;
|
|
52
|
+
|
|
53
|
+
function load(): boolean {
|
|
54
|
+
return (loading = !!(typeof source === "function"
|
|
55
|
+
? untrack(source as Accessor<unknown>)
|
|
56
|
+
: source));
|
|
57
|
+
}
|
|
58
|
+
load();
|
|
59
|
+
const read = (() => value) as Resource<T>;
|
|
60
|
+
Object.defineProperties(read, {
|
|
61
|
+
state: { value: state },
|
|
62
|
+
loading: { get: () => loading },
|
|
63
|
+
error: { value: undefined },
|
|
64
|
+
latest: { get: read },
|
|
65
|
+
});
|
|
66
|
+
return [
|
|
67
|
+
read,
|
|
68
|
+
{
|
|
69
|
+
mutate: (next) => (value = next),
|
|
70
|
+
refetch: load,
|
|
71
|
+
},
|
|
72
|
+
];
|
|
73
|
+
}
|
package/src/resource.ts
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
/** Client resources without Suspense, hydration, or asynchronous SSR. */
|
|
2
|
+
import { peekComponentContext } from "./component";
|
|
3
|
+
import {
|
|
4
|
+
batch,
|
|
5
|
+
createComputed,
|
|
6
|
+
createMemo,
|
|
7
|
+
createSignal,
|
|
8
|
+
getOwner,
|
|
9
|
+
runWithOwner,
|
|
10
|
+
onCleanup,
|
|
11
|
+
untrack,
|
|
12
|
+
type Accessor,
|
|
13
|
+
type Setter,
|
|
14
|
+
} from "./reactive";
|
|
15
|
+
import { isSSRRendering } from "./ssr-owner";
|
|
16
|
+
|
|
17
|
+
export type ResourceState =
|
|
18
|
+
"unresolved" | "pending" | "ready" | "refreshing" | "errored";
|
|
19
|
+
export type Resource<T> = Accessor<T | undefined> & {
|
|
20
|
+
readonly state: ResourceState;
|
|
21
|
+
readonly loading: boolean;
|
|
22
|
+
readonly error: Error | undefined;
|
|
23
|
+
readonly latest: T | undefined;
|
|
24
|
+
};
|
|
25
|
+
export type ResourceSource<S> =
|
|
26
|
+
S | false | null | undefined | Accessor<S | false | null | undefined>;
|
|
27
|
+
export type ResourceFetcher<S, T, R = unknown> = (
|
|
28
|
+
source: S,
|
|
29
|
+
info: { value: T | undefined; refetching: R | boolean },
|
|
30
|
+
) => T | PromiseLike<T>;
|
|
31
|
+
export type ResourceOptions<T> = { initialValue?: T; name?: string };
|
|
32
|
+
export type ResourceActions<T, R = unknown> = {
|
|
33
|
+
mutate: Setter<T | undefined>;
|
|
34
|
+
refetch: (info?: R) => T | Promise<T | undefined> | undefined;
|
|
35
|
+
};
|
|
36
|
+
export type ResourceReturn<T, R = unknown> = [
|
|
37
|
+
Resource<T>,
|
|
38
|
+
ResourceActions<T, R>,
|
|
39
|
+
];
|
|
40
|
+
|
|
41
|
+
export function createResource<T, R = unknown>(
|
|
42
|
+
fetcher: ResourceFetcher<true, T, R>,
|
|
43
|
+
options?: ResourceOptions<T>,
|
|
44
|
+
): ResourceReturn<T, R>;
|
|
45
|
+
export function createResource<T, S, R = unknown>(
|
|
46
|
+
source: ResourceSource<S>,
|
|
47
|
+
fetcher: ResourceFetcher<S, T, R>,
|
|
48
|
+
options?: ResourceOptions<T>,
|
|
49
|
+
): ResourceReturn<T, R>;
|
|
50
|
+
export function createResource<T, S, R = unknown>(
|
|
51
|
+
sourceOrFetcher: ResourceSource<S> | ResourceFetcher<true, T, R>,
|
|
52
|
+
fetcherOrOptions?: ResourceFetcher<S, T, R> | ResourceOptions<T>,
|
|
53
|
+
suppliedOptions?: ResourceOptions<T>,
|
|
54
|
+
): ResourceReturn<T, R> {
|
|
55
|
+
if (isSSRRendering() || peekComponentContext()?.mode === "ssr")
|
|
56
|
+
throw new Error(
|
|
57
|
+
"Client createResource is unavailable during SSR; use createResource from the server resource module",
|
|
58
|
+
);
|
|
59
|
+
const hasSource = typeof fetcherOrOptions === "function";
|
|
60
|
+
const source = (hasSource ? sourceOrFetcher : true) as ResourceSource<S>;
|
|
61
|
+
const fetcher = (
|
|
62
|
+
hasSource ? fetcherOrOptions : sourceOrFetcher
|
|
63
|
+
) as ResourceFetcher<S, T, R>;
|
|
64
|
+
const options = (hasSource ? suppliedOptions : fetcherOrOptions) as
|
|
65
|
+
ResourceOptions<T> | undefined;
|
|
66
|
+
for (const key of Object.keys(options ?? {})) {
|
|
67
|
+
if (key !== "initialValue" && key !== "name")
|
|
68
|
+
throw new TypeError(`Unsupported native resource option: ${key}`);
|
|
69
|
+
}
|
|
70
|
+
const [value, setValue] = createSignal<T | undefined>(options?.initialValue);
|
|
71
|
+
const [error, setError] = createSignal<Error | undefined>(undefined);
|
|
72
|
+
let resolved = options != null && "initialValue" in options;
|
|
73
|
+
const [state, setState] = createSignal<ResourceState>(
|
|
74
|
+
resolved ? "ready" : "unresolved",
|
|
75
|
+
);
|
|
76
|
+
let generation = 0;
|
|
77
|
+
let disposed = false;
|
|
78
|
+
onCleanup(() => {
|
|
79
|
+
disposed = true;
|
|
80
|
+
generation++;
|
|
81
|
+
});
|
|
82
|
+
const dynamic =
|
|
83
|
+
typeof source === "function"
|
|
84
|
+
? createMemo(source as Accessor<S | false | null | undefined>)
|
|
85
|
+
: undefined;
|
|
86
|
+
let fetchOwner = getOwner();
|
|
87
|
+
|
|
88
|
+
function publish(token: number, result: T | undefined, failure?: Error) {
|
|
89
|
+
if (disposed || token !== generation) return;
|
|
90
|
+
resolved = true;
|
|
91
|
+
batch(() => {
|
|
92
|
+
if (!failure) setValue(() => result);
|
|
93
|
+
setError(failure);
|
|
94
|
+
setState(failure ? "errored" : "ready");
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
function load(
|
|
98
|
+
refetching: R | boolean,
|
|
99
|
+
): T | Promise<T | undefined> | undefined {
|
|
100
|
+
if (disposed) return untrack(value);
|
|
101
|
+
const key = dynamic ? dynamic() : source;
|
|
102
|
+
const token = ++generation;
|
|
103
|
+
if (key == null || key === false) {
|
|
104
|
+
batch(() => {
|
|
105
|
+
setError(undefined);
|
|
106
|
+
setState(resolved ? "ready" : "unresolved");
|
|
107
|
+
});
|
|
108
|
+
return undefined;
|
|
109
|
+
}
|
|
110
|
+
let result: T | PromiseLike<T>;
|
|
111
|
+
let asynchronous: boolean;
|
|
112
|
+
try {
|
|
113
|
+
result = untrack(() => fetcher(key as S, { value: value(), refetching }));
|
|
114
|
+
asynchronous =
|
|
115
|
+
result != null &&
|
|
116
|
+
(typeof result === "object" || typeof result === "function") &&
|
|
117
|
+
typeof (result as PromiseLike<T>).then === "function";
|
|
118
|
+
} catch (failure) {
|
|
119
|
+
publish(
|
|
120
|
+
token,
|
|
121
|
+
undefined,
|
|
122
|
+
failure instanceof Error ? failure : new Error(String(failure)),
|
|
123
|
+
);
|
|
124
|
+
return undefined;
|
|
125
|
+
}
|
|
126
|
+
if (!asynchronous) {
|
|
127
|
+
publish(token, result as T);
|
|
128
|
+
return result as T;
|
|
129
|
+
}
|
|
130
|
+
const pending = Promise.resolve(result).then(
|
|
131
|
+
(next) => {
|
|
132
|
+
publish(token, next);
|
|
133
|
+
return next;
|
|
134
|
+
},
|
|
135
|
+
(failure) => {
|
|
136
|
+
publish(
|
|
137
|
+
token,
|
|
138
|
+
undefined,
|
|
139
|
+
failure instanceof Error ? failure : new Error(String(failure)),
|
|
140
|
+
);
|
|
141
|
+
return undefined;
|
|
142
|
+
},
|
|
143
|
+
);
|
|
144
|
+
if (!disposed && token === generation)
|
|
145
|
+
batch(() => {
|
|
146
|
+
setState(resolved ? "refreshing" : "pending");
|
|
147
|
+
});
|
|
148
|
+
return pending;
|
|
149
|
+
}
|
|
150
|
+
const read = (() => {
|
|
151
|
+
const current = value();
|
|
152
|
+
const failure = error();
|
|
153
|
+
if (failure) {
|
|
154
|
+
const pending = state();
|
|
155
|
+
if (pending !== "pending" && pending !== "refreshing") throw failure;
|
|
156
|
+
}
|
|
157
|
+
return current;
|
|
158
|
+
}) as Resource<T>;
|
|
159
|
+
Object.defineProperties(read, {
|
|
160
|
+
state: { get: state },
|
|
161
|
+
loading: {
|
|
162
|
+
get: () => {
|
|
163
|
+
const current = state();
|
|
164
|
+
return current === "pending" || current === "refreshing";
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
error: { get: error },
|
|
168
|
+
latest: { get: read },
|
|
169
|
+
});
|
|
170
|
+
if (dynamic) {
|
|
171
|
+
// Subscribe before the first fetcher can change its own source.
|
|
172
|
+
let watching = false;
|
|
173
|
+
createComputed(() => {
|
|
174
|
+
fetchOwner = getOwner();
|
|
175
|
+
dynamic();
|
|
176
|
+
if (watching) void load(false);
|
|
177
|
+
});
|
|
178
|
+
watching = true;
|
|
179
|
+
runWithOwner(fetchOwner, () =>
|
|
180
|
+
untrack(() => {
|
|
181
|
+
void load(false);
|
|
182
|
+
}),
|
|
183
|
+
);
|
|
184
|
+
} else void load(false);
|
|
185
|
+
return [
|
|
186
|
+
read,
|
|
187
|
+
{
|
|
188
|
+
mutate: (next) => (disposed ? untrack(value) : setValue(next)),
|
|
189
|
+
refetch: (info) =>
|
|
190
|
+
runWithOwner(fetchOwner, () =>
|
|
191
|
+
untrack(() => load(info === undefined ? true : info)),
|
|
192
|
+
),
|
|
193
|
+
},
|
|
194
|
+
];
|
|
195
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { createSignal, getOwner, onCleanup } from "./reactive";
|
|
2
|
+
|
|
3
|
+
/** Owned DOM text rotation; reduced motion keeps the first phrase. */
|
|
4
|
+
export function createRotatingText(
|
|
5
|
+
phrases: readonly string[],
|
|
6
|
+
intervalMs: number,
|
|
7
|
+
): () => string {
|
|
8
|
+
if (phrases.length < 2) return () => phrases[0] ?? "";
|
|
9
|
+
const owner = getOwner();
|
|
10
|
+
if (!owner || owner.disposed)
|
|
11
|
+
throw new Error("Rotating text requires a live native owner");
|
|
12
|
+
const [index, setIndex] = createSignal(0);
|
|
13
|
+
const motion = globalThis.matchMedia?.("(prefers-reduced-motion: reduce)");
|
|
14
|
+
let timer: ReturnType<typeof globalThis.setInterval> | undefined;
|
|
15
|
+
const syncMotion = () => {
|
|
16
|
+
if (owner.disposed) return;
|
|
17
|
+
if (motion?.matches) {
|
|
18
|
+
if (timer !== undefined) globalThis.clearInterval(timer);
|
|
19
|
+
timer = undefined;
|
|
20
|
+
setIndex(0);
|
|
21
|
+
} else if (timer === undefined) {
|
|
22
|
+
timer = globalThis.setInterval(
|
|
23
|
+
() => {
|
|
24
|
+
if (!owner.disposed)
|
|
25
|
+
setIndex((value) => (value + 1) % phrases.length);
|
|
26
|
+
},
|
|
27
|
+
Math.max(1, Math.min(2147483647, intervalMs || 1)),
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
onCleanup(() => {
|
|
32
|
+
motion?.removeEventListener("change", syncMotion);
|
|
33
|
+
if (timer !== undefined) globalThis.clearInterval(timer);
|
|
34
|
+
});
|
|
35
|
+
motion?.addEventListener("change", syncMotion);
|
|
36
|
+
syncMotion();
|
|
37
|
+
return () => phrases[index()] ?? "";
|
|
38
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/** Optional SSR render-attempt lifecycle; no serializer or browser dependency. */
|
|
2
|
+
import { getOwner } from "./reactive";
|
|
3
|
+
const attemptKey = /* @__PURE__ */ Symbol("moonshine.ssr.attempt");
|
|
4
|
+
export type SSRRenderAttempt = {
|
|
5
|
+
status: "pending" | "accepted" | "discarded";
|
|
6
|
+
parent?: SSRRenderAttempt;
|
|
7
|
+
};
|
|
8
|
+
let active = false;
|
|
9
|
+
let currentAttempt: SSRRenderAttempt | undefined;
|
|
10
|
+
/** Keep serialization attempts current when lexical component owners are restored. */
|
|
11
|
+
export function withSSRRenderAttempt<T>(
|
|
12
|
+
attempt: SSRRenderAttempt | undefined,
|
|
13
|
+
read: () => T,
|
|
14
|
+
): T {
|
|
15
|
+
const previous = currentAttempt;
|
|
16
|
+
const wasActive = active;
|
|
17
|
+
active = true;
|
|
18
|
+
currentAttempt = attempt;
|
|
19
|
+
try {
|
|
20
|
+
return read();
|
|
21
|
+
} finally {
|
|
22
|
+
currentAttempt = previous;
|
|
23
|
+
active = wasActive;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export function captureSSRRenderAttempt(): SSRRenderAttempt | undefined {
|
|
27
|
+
return getOwner()?.context?.get(attemptKey) as SSRRenderAttempt | undefined;
|
|
28
|
+
}
|
|
29
|
+
/** Publishing follows the active buffer while lifecycle capture remains lexical. */
|
|
30
|
+
export function captureActiveSSRRenderAttempt(): SSRRenderAttempt | undefined {
|
|
31
|
+
const lexical = captureSSRRenderAttempt();
|
|
32
|
+
if (!active) return lexical;
|
|
33
|
+
if (!currentAttempt) return undefined;
|
|
34
|
+
for (let attempt = lexical; attempt; attempt = attempt.parent)
|
|
35
|
+
if (attempt === currentAttempt) return lexical;
|
|
36
|
+
return currentAttempt;
|
|
37
|
+
}
|
|
38
|
+
/** A new request must not inherit its caller's pending lifecycle attempt. */
|
|
39
|
+
export function resetSSRRenderAttempt(): void {
|
|
40
|
+
const owner = getOwner();
|
|
41
|
+
if (owner?.context?.has(attemptKey)) {
|
|
42
|
+
owner.context = new Map(owner.context);
|
|
43
|
+
owner.context.delete(attemptKey);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
export function createSSRRenderAttempt(): SSRRenderAttempt {
|
|
47
|
+
const owner = getOwner();
|
|
48
|
+
if (!owner || owner.disposed)
|
|
49
|
+
throw new Error("SSR attempts require a live native owner");
|
|
50
|
+
const attempt: SSRRenderAttempt = {
|
|
51
|
+
status: "pending",
|
|
52
|
+
parent: captureActiveSSRRenderAttempt(),
|
|
53
|
+
};
|
|
54
|
+
owner.context = new Map(owner.context);
|
|
55
|
+
owner.context.set(attemptKey, attempt);
|
|
56
|
+
return attempt;
|
|
57
|
+
}
|
|
58
|
+
export function isSSRRenderAccepted(
|
|
59
|
+
attempt: SSRRenderAttempt | undefined,
|
|
60
|
+
): boolean {
|
|
61
|
+
for (let current = attempt; current; current = current.parent)
|
|
62
|
+
if (current.status !== "accepted") return false;
|
|
63
|
+
return true;
|
|
64
|
+
}
|
package/src/ssr-owner.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/** Lightweight SSR context and ownership; importing it does not load the serializer. */
|
|
2
|
+
import { getOwner, type Owner } from "./reactive";
|
|
3
|
+
import type { SSRChild } from "./ssr";
|
|
4
|
+
|
|
5
|
+
let rendering = 0;
|
|
6
|
+
let renderScope: object | undefined;
|
|
7
|
+
export function getSSRRenderScope(): object | undefined {
|
|
8
|
+
return renderScope;
|
|
9
|
+
}
|
|
10
|
+
/** shortcut: synchronous SSR only; use request-local context for async rendering. */
|
|
11
|
+
export function withSSRRendering<T>(fn: () => T): T {
|
|
12
|
+
const previous = renderScope;
|
|
13
|
+
renderScope = {};
|
|
14
|
+
rendering++;
|
|
15
|
+
try {
|
|
16
|
+
return fn();
|
|
17
|
+
} finally {
|
|
18
|
+
rendering--;
|
|
19
|
+
renderScope = previous;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export function isSSRRendering(): boolean {
|
|
23
|
+
return rendering > 0;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export const ssrOwner = /* @__PURE__ */ Symbol("moonshine SSR owner");
|
|
27
|
+
export type OwnedSSRChild = {
|
|
28
|
+
readonly [ssrOwner]: Owner;
|
|
29
|
+
readonly child: SSRChild;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/** Keep deferred descendants in the current scope through complete serialization. */
|
|
33
|
+
export function scopeSSRChild(child: SSRChild): OwnedSSRChild {
|
|
34
|
+
const owner = getOwner();
|
|
35
|
+
if (!owner || owner.disposed)
|
|
36
|
+
throw new Error("SSR child scope requires a live native owner");
|
|
37
|
+
return { [ssrOwner]: owner, child };
|
|
38
|
+
}
|