what-core 0.13.11 → 0.15.0
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 +21 -0
- package/README.md +64 -38
- package/a11y.d.ts +128 -0
- package/data.d.ts +294 -0
- package/dist/a11y.js +510 -0
- package/dist/a11y.min.js +413 -0
- package/dist/actions.js +158 -0
- package/dist/actions.min.js +112 -0
- package/dist/animation.js +481 -0
- package/dist/animation.min.js +295 -0
- package/dist/async.js +180 -0
- package/dist/async.min.js +134 -0
- package/dist/components.js +577 -0
- package/dist/components.min.js +413 -0
- package/dist/data.js +1212 -0
- package/dist/data.min.js +918 -0
- package/dist/diagnostics.js +187 -0
- package/dist/diagnostics.min.js +134 -0
- package/dist/dom.js +796 -0
- package/dist/dom.min.js +603 -0
- package/dist/entries/a11y.js +43 -0
- package/dist/entries/a11y.min.js +43 -0
- package/dist/entries/data.js +29 -0
- package/dist/entries/data.min.js +29 -0
- package/dist/entries/dom.js +45 -0
- package/dist/entries/dom.min.js +45 -0
- package/dist/entries/form.js +31 -0
- package/dist/entries/form.min.js +31 -0
- package/dist/entries/internal.js +123 -0
- package/dist/entries/internal.min.js +123 -0
- package/dist/entries/motion.js +23 -0
- package/dist/entries/motion.min.js +23 -0
- package/dist/entries/ui.js +25 -0
- package/dist/entries/ui.min.js +25 -0
- package/dist/env.js +5 -0
- package/dist/env.min.js +5 -0
- package/dist/errors.js +1518 -0
- package/dist/errors.min.js +548 -0
- package/dist/form.js +936 -0
- package/dist/form.min.js +703 -0
- package/dist/guardrails.js +84 -0
- package/dist/guardrails.min.js +55 -0
- package/dist/h.js +232 -0
- package/dist/h.min.js +171 -0
- package/dist/head.js +241 -0
- package/dist/head.min.js +207 -0
- package/dist/helpers.js +126 -0
- package/dist/helpers.min.js +98 -0
- package/dist/hooks.js +255 -0
- package/dist/hooks.min.js +153 -0
- package/dist/hot.js +215 -0
- package/dist/hot.min.js +24 -0
- package/dist/hydration-data.js +495 -0
- package/dist/hydration-data.min.js +371 -0
- package/dist/import-changes.js +273 -0
- package/dist/import-changes.min.js +272 -0
- package/dist/index.js +95 -0
- package/dist/index.min.js +95 -227
- package/dist/jsx-dev-runtime.js +10 -0
- package/dist/jsx-dev-runtime.min.js +10 -1
- package/dist/jsx-runtime.js +27 -0
- package/dist/jsx-runtime.min.js +15 -1
- package/dist/live.js +54 -0
- package/dist/live.min.js +49 -0
- package/dist/markup-rules.js +250 -0
- package/dist/markup-rules.min.js +213 -0
- package/dist/reactive.js +3334 -0
- package/dist/reactive.min.js +2033 -0
- package/dist/render.js +4746 -0
- package/dist/render.min.js +2797 -1
- package/dist/scheduler.js +225 -0
- package/dist/scheduler.min.js +162 -0
- package/dist/selector.js +11 -0
- package/dist/selector.min.js +10 -0
- package/dist/server-context.js +148 -0
- package/dist/server-context.min.js +121 -0
- package/dist/skeleton.js +342 -0
- package/dist/skeleton.min.js +310 -0
- package/dist/store.js +72 -0
- package/dist/store.min.js +50 -0
- package/dist/testing.js +720 -0
- package/dist/testing.min.js +458 -1
- package/dist/vnode-adoption.js +19 -0
- package/dist/vnode-adoption.min.js +19 -0
- package/dist/vnode-renderer.js +61 -0
- package/dist/vnode-renderer.min.js +47 -0
- package/dom.d.ts +44 -0
- package/form.d.ts +218 -0
- package/hot.d.ts +35 -0
- package/index.d.ts +421 -736
- package/internal.d.ts +284 -0
- package/jsx-runtime.d.ts +160 -137
- package/markup-rules.d.ts +83 -0
- package/motion.d.ts +156 -0
- package/package.json +117 -3
- package/render.d.ts +278 -29
- package/src/a11y.js +64 -59
- package/src/actions.js +226 -0
- package/src/animation.js +105 -57
- package/src/async.js +259 -0
- package/src/components.js +495 -144
- package/src/data.js +1410 -1159
- package/src/diagnostics.js +263 -0
- package/src/dom.js +1159 -872
- package/src/entries/a11y.js +24 -0
- package/src/entries/data.js +17 -0
- package/src/entries/dom.js +29 -0
- package/src/entries/form.js +16 -0
- package/src/entries/internal.js +63 -0
- package/src/entries/motion.js +13 -0
- package/src/entries/ui.js +13 -0
- package/src/env-development.js +12 -0
- package/src/env.js +33 -0
- package/src/errors.js +1144 -168
- package/src/form.js +723 -225
- package/src/guardrails.js +38 -126
- package/src/h.js +227 -152
- package/src/head.js +189 -19
- package/src/helpers.js +47 -75
- package/src/hooks.js +233 -248
- package/src/hot.js +316 -0
- package/src/hydration-data.js +859 -16
- package/src/import-changes.js +156 -0
- package/src/index.js +38 -188
- package/src/jsx-dev-runtime.js +7 -14
- package/src/jsx-runtime.js +25 -9
- package/src/live.js +80 -0
- package/src/markup-rules.js +387 -0
- package/src/reactive.js +4400 -862
- package/src/render.js +6502 -2570
- package/src/scheduler.js +56 -21
- package/src/selector.js +31 -0
- package/src/server-context.js +252 -22
- package/src/skeleton.js +22 -12
- package/src/store.js +17 -31
- package/src/testing.js +389 -63
- package/src/vnode-adoption.js +48 -0
- package/src/vnode-renderer.js +114 -0
- package/testing.d.ts +81 -1
- package/ui.d.ts +20 -0
- package/dist/chunk-6YOUZ6T4.min.js +0 -1
- package/dist/chunk-FN6HXME3.min.js +0 -11
- package/dist/chunk-VKZY7JPN.min.js +0 -1
- package/src/agent-context.js +0 -127
- package/src/warnings.js +0 -193
package/src/scheduler.js
CHANGED
|
@@ -110,26 +110,55 @@ export function mutate(fn) {
|
|
|
110
110
|
}
|
|
111
111
|
|
|
112
112
|
// --- useScheduledEffect ---
|
|
113
|
-
//
|
|
113
|
+
// An effect split across a frame: `readFn` runs in the read phase and
|
|
114
|
+
// `writeFn(data)` with what it returned in the write phase. It runs again
|
|
115
|
+
// when a signal `readFn` read changes: in the next frame's read phase, once
|
|
116
|
+
// however many changes came before it. Disposed with its owner, or by the
|
|
117
|
+
// function it returns; a frame still to come then does nothing.
|
|
114
118
|
|
|
115
|
-
import { effect } from './reactive.js';
|
|
119
|
+
import { effect, getOwner, runWithOwner, onCleanup } from './reactive.js';
|
|
116
120
|
|
|
117
121
|
export function useScheduledEffect(readFn, writeFn) {
|
|
118
|
-
const
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
122
|
+
const owner = getOwner();
|
|
123
|
+
let disposed = false;
|
|
124
|
+
/** @type {(() => void) | null} */
|
|
125
|
+
let cancel = null;
|
|
126
|
+
/** @type {() => void} */
|
|
127
|
+
let tracking = noop;
|
|
128
|
+
const schedule = () => {
|
|
129
|
+
if (cancel === null && !disposed) cancel = scheduleRead(read);
|
|
130
|
+
};
|
|
131
|
+
// The read is the first run of an effect made for it, which tracks what
|
|
132
|
+
// readFn reads; that effect's next run is a change, and only schedules.
|
|
133
|
+
const read = () => {
|
|
134
|
+
cancel = null;
|
|
135
|
+
tracking();
|
|
136
|
+
let first = true;
|
|
137
|
+
let data;
|
|
138
|
+
tracking = runWithOwner(owner, () => effect(() => {
|
|
139
|
+
if (!first) {
|
|
140
|
+
schedule();
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
first = false;
|
|
144
|
+
data = readFn();
|
|
145
|
+
}));
|
|
146
|
+
if (writeFn) scheduleWrite(() => { if (!disposed) writeFn(data); });
|
|
147
|
+
};
|
|
148
|
+
const dispose = () => {
|
|
149
|
+
disposed = true;
|
|
150
|
+
if (cancel !== null) cancel();
|
|
151
|
+
cancel = null;
|
|
152
|
+
tracking();
|
|
153
|
+
tracking = noop;
|
|
154
|
+
};
|
|
155
|
+
if (owner !== null) onCleanup(dispose);
|
|
156
|
+
schedule();
|
|
157
|
+
return dispose;
|
|
131
158
|
}
|
|
132
159
|
|
|
160
|
+
function noop() {}
|
|
161
|
+
|
|
133
162
|
// --- Animation frame helper ---
|
|
134
163
|
// Like requestAnimationFrame but returns a cancellable promise.
|
|
135
164
|
|
|
@@ -183,20 +212,26 @@ export function onResize(element, callback) {
|
|
|
183
212
|
sharedResizeObserver = new ResizeObserver(entries => {
|
|
184
213
|
scheduleRead(() => {
|
|
185
214
|
for (const entry of entries) {
|
|
186
|
-
const
|
|
187
|
-
if (cb)
|
|
188
|
-
cb(entry.contentRect);
|
|
189
|
-
}
|
|
215
|
+
const callbacks = resizeObservers.get(entry.target);
|
|
216
|
+
if (callbacks) for (const cb of [...callbacks]) cb(entry.contentRect);
|
|
190
217
|
}
|
|
191
218
|
});
|
|
192
219
|
});
|
|
193
220
|
}
|
|
194
221
|
|
|
222
|
+
// Every callback registered for an element is called; the element stays
|
|
223
|
+
// observed until the last one is removed.
|
|
195
224
|
const observer = sharedResizeObserver;
|
|
196
|
-
resizeObservers.
|
|
197
|
-
|
|
225
|
+
let callbacks = resizeObservers.get(element);
|
|
226
|
+
if (!callbacks) {
|
|
227
|
+
resizeObservers.set(element, callbacks = new Set());
|
|
228
|
+
observer.observe(element);
|
|
229
|
+
}
|
|
230
|
+
const entry = (rect) => callback(rect);
|
|
231
|
+
callbacks.add(entry);
|
|
198
232
|
|
|
199
233
|
return () => {
|
|
234
|
+
if (!callbacks.delete(entry) || callbacks.size) return;
|
|
200
235
|
resizeObservers.delete(element);
|
|
201
236
|
observer.unobserve(element);
|
|
202
237
|
};
|
package/src/selector.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// What Framework - createSelector
|
|
2
|
+
//
|
|
3
|
+
// createSelector(source) returns isSelected(key), which is `key === source()`.
|
|
4
|
+
// A binding that reads isSelected(key) depends on that key rather than on
|
|
5
|
+
// `source`, so when the source moves from a to b only the bindings that asked
|
|
6
|
+
// about a or b re-run: one or two re-runs per change instead of one per reader.
|
|
7
|
+
// This is what keeps `class={selected() === row.id ? 'danger' : ''}` cheap in a
|
|
8
|
+
// long list, and the compiler rewrites exactly that shape into a selector inside
|
|
9
|
+
// keyed .map() rows (see the auto-selector in what-compiler).
|
|
10
|
+
//
|
|
11
|
+
// The engine keeps one small key node per key that a tracked read asked about
|
|
12
|
+
// (reactive.js, _createSelector) and drops it when its last reader goes, so
|
|
13
|
+
// nothing is kept for keys whose rows are gone. A write that reaches the source
|
|
14
|
+
// brings the selector up to date before it returns and notifies the old and
|
|
15
|
+
// the new key, so every read, including one through a computed, sees the new
|
|
16
|
+
// answer right after the write.
|
|
17
|
+
//
|
|
18
|
+
// API credit: Solid's createSelector (MIT, Ryan Carniato). Solid finds the
|
|
19
|
+
// readers to notify by scanning every key on each change; this looks up the two
|
|
20
|
+
// keys the change involves.
|
|
21
|
+
|
|
22
|
+
import { _createSelector, __DEV__ } from './reactive.js';
|
|
23
|
+
|
|
24
|
+
export function createSelector(source) {
|
|
25
|
+
if (typeof source !== 'function') {
|
|
26
|
+
throw new TypeError(__DEV__
|
|
27
|
+
? '[what] createSelector(source) needs a signal or accessor function, e.g. createSelector(selectedId).'
|
|
28
|
+
: '[what] createSelector: source must be a function');
|
|
29
|
+
}
|
|
30
|
+
return _createSelector(source);
|
|
31
|
+
}
|
package/src/server-context.js
CHANGED
|
@@ -4,21 +4,111 @@
|
|
|
4
4
|
// the head sink, the current page's loader data, and the resource cache.
|
|
5
5
|
//
|
|
6
6
|
// Concurrency model:
|
|
7
|
-
// -
|
|
8
|
-
//
|
|
9
|
-
//
|
|
10
|
-
//
|
|
7
|
+
// - A renderer runs each synchronous slice of a render inside
|
|
8
|
+
// runWithServerContext, which sets a module variable for exactly that
|
|
9
|
+
// slice. JavaScript does not interleave inside a synchronous call, so no
|
|
10
|
+
// other request can observe it, and the hot path is a variable read.
|
|
11
|
+
// - Code that resumes after an `await` (an async component, a loader) runs
|
|
12
|
+
// outside any slice. When an AsyncLocalStorage-compatible provider is
|
|
13
|
+
// installed (what-server does so on hosts that have one), runWithServerContext
|
|
14
|
+
// also enters it, so that code still finds its own request's context.
|
|
11
15
|
//
|
|
12
16
|
// getServerContext() returns null on the client and outside of any render, so
|
|
13
17
|
// `typeof document === 'undefined'` guards keep behaving correctly.
|
|
18
|
+
//
|
|
19
|
+
// Reactive ownership: a render context may carry `owner`, a reactive root the
|
|
20
|
+
// renderer created for that render. runWithServerContext makes it the current
|
|
21
|
+
// owner for the slice, so an effect() or computed() a component body creates
|
|
22
|
+
// belongs to the render and is disposed when the renderer releases it, instead
|
|
23
|
+
// of staying subscribed to module-scope signals and re-running inside a later
|
|
24
|
+
// request's render. Code that resumes after an `await` (an async component)
|
|
25
|
+
// runs with no owner current; with an async context provider installed, what
|
|
26
|
+
// it creates still goes to its own render's root, so nothing a finished
|
|
27
|
+
// request created can run later (a root already released disposes it at once).
|
|
28
|
+
// Another copy of what-core in the process (a component bundled with its own
|
|
29
|
+
// framework, another export tier) cannot hold work in the render's root, which
|
|
30
|
+
// is the renderer's copy's: what it creates goes to a root of its own for that
|
|
31
|
+
// render, which the render's release disposes too (renderOwner below).
|
|
32
|
+
|
|
33
|
+
import { runWithOwner, flushSync, _detached, _engine, _setOwnerFallback, _isDisposed, _serverStoreKey as STORE, __DEV__ } from './reactive.js';
|
|
34
|
+
import { _installServerSeeding } from './hydration-data.js';
|
|
35
|
+
import { _thrownMessage } from './diagnostics.js';
|
|
36
|
+
|
|
37
|
+
// The process's render context, its async context provider and whether it is
|
|
38
|
+
// a server, kept once for every tier of what-core the process loaded: the
|
|
39
|
+
// development and production builds (dist/*.js, dist/*.min.js) and the source
|
|
40
|
+
// each hold a copy of this module, and a component bundled from one tier that
|
|
41
|
+
// a server of another renders still reads that render's context (its
|
|
42
|
+
// response, its loader data). Keyed by Symbol.for, so one realm has one; the
|
|
43
|
+
// key names the store's shape (`.v1`), so a copy that keeps another shape
|
|
44
|
+
// keeps another store (STORE, reactive.js _serverStoreKey).
|
|
45
|
+
/** @typedef {{ getStore: () => any, run: (store: any, fn: () => any) => any }} AsyncStorage */
|
|
46
|
+
/** @type {{ current: any, storage: AsyncStorage | null, frames: AsyncStorage | null, server: boolean, withDocument: boolean, adopt?: typeof adopt, refuse?: typeof refuse } | null} */
|
|
47
|
+
let shared = null;
|
|
48
|
+
// Made on first use, so importing the module writes no global. On first use
|
|
49
|
+
// this copy's engine also starts asking the active render for the root that
|
|
50
|
+
// holds what it creates with no owner current (renderOwner).
|
|
51
|
+
function store() {
|
|
52
|
+
if (shared === null) _setOwnerFallback(renderOwner);
|
|
53
|
+
return shared ??= /** @type {any} */ (globalThis)[STORE] ??= { current: null, storage: null, frames: null, server: false, withDocument: false };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// What only a server render needs, installed by what can start one
|
|
57
|
+
// (__installServerContextStorage, setServerContext, runWithServerContext), so
|
|
58
|
+
// a browser bundle carries none of it: the server store seeding
|
|
59
|
+
// (hydration-data.js), and on the store what another copy of the engine
|
|
60
|
+
// takes from a render it never joined (adopt, refuse). Returns the store.
|
|
61
|
+
function serving() {
|
|
62
|
+
_installServerSeeding();
|
|
63
|
+
const state = store();
|
|
64
|
+
state.adopt = adopt;
|
|
65
|
+
state.refuse = refuse;
|
|
66
|
+
return state;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// What a copy of the engine that never joined the render throws when it
|
|
70
|
+
// makes an async value in one (hydration-data.js _seedFor): that copy never
|
|
71
|
+
// reaches the render's resources, so the value would start over on every
|
|
72
|
+
// retry of the render and never reach the client. Said, not guessed.
|
|
73
|
+
function refuse() {
|
|
74
|
+
if (getServerContext() != null) {
|
|
75
|
+
// ERROR_CODES.FOREIGN_ENGINE_IN_SSR
|
|
76
|
+
throw Object.assign(new Error(__DEV__
|
|
77
|
+
? _thrownMessage('ERR_FOREIGN_ENGINE_IN_SSR', 'An async value was made by a second copy of what-core during a server render. Bundle one copy of what-framework.')
|
|
78
|
+
: '[what] An async value was made by a second copy of what-core during a server render. Bundle one copy of what-framework.'), { code: 'ERR_FOREIGN_ENGINE_IN_SSR' });
|
|
79
|
+
}
|
|
80
|
+
}
|
|
14
81
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
82
|
+
// What a copy of the engine that never reached this module takes as its owner
|
|
83
|
+
// lookup (reactive.js ownerNow), from the store of a render that started: the
|
|
84
|
+
// active render's root for that copy.
|
|
85
|
+
function adopt(setFallback, engine) {
|
|
86
|
+
setFallback(() => {
|
|
87
|
+
const ctx = getServerContext();
|
|
88
|
+
return ctx != null && typeof ctx.rootFor === 'function' ? ctx.rootFor(engine) : null;
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// The owner of work this copy of the engine creates with no owner current, in
|
|
93
|
+
// a server render: the frame of the component whose code it is (code an async
|
|
94
|
+
// component runs after an `await`, which _runComponentAsync made findable), so
|
|
95
|
+
// what it reads by context (a Router's state, useContext) and what it creates
|
|
96
|
+
// are its component's; else the render's root, the render's own or one of
|
|
97
|
+
// this copy's for it.
|
|
98
|
+
function renderOwner() {
|
|
99
|
+
const frames = /** @type {any} */ (shared).frames;
|
|
100
|
+
const resumed = frames !== null ? frames.getStore() : undefined;
|
|
101
|
+
if (resumed != null && resumed.engine === _engine && !_isDisposed(resumed.frame)) return resumed.frame;
|
|
102
|
+
const ctx = getServerContext();
|
|
103
|
+
return ctx != null && typeof ctx.rootFor === 'function' ? ctx.rootFor(_engine) : null;
|
|
104
|
+
}
|
|
105
|
+
// Whether this copy of the module has wired the provider into its tier.
|
|
106
|
+
let installed = false;
|
|
19
107
|
|
|
20
108
|
/**
|
|
21
|
-
* Install the async context
|
|
109
|
+
* Install the async context providers used by the server renderer: `storage`
|
|
110
|
+
* carries a render's context past an `await`, `frames` an async component's
|
|
111
|
+
* frame (_runComponentAsync).
|
|
22
112
|
*
|
|
23
113
|
* This intentionally accepts the small AsyncLocalStorage surface instead of
|
|
24
114
|
* importing `node:async_hooks` from what-core: the core bundle remains browser
|
|
@@ -26,13 +116,82 @@ let _asyncContextStorage = null;
|
|
|
26
116
|
*
|
|
27
117
|
* @internal
|
|
28
118
|
*/
|
|
29
|
-
export function __installServerContextStorage(storage) {
|
|
30
|
-
if (
|
|
119
|
+
export function __installServerContextStorage(storage, frames) {
|
|
120
|
+
if (installed) return;
|
|
121
|
+
installed = true;
|
|
122
|
+
const state = serving();
|
|
123
|
+
if (state.storage === null) {
|
|
124
|
+
state.storage = storage;
|
|
125
|
+
state.frames = frames ?? null;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Development: a server component's frame knows while its function runs
|
|
130
|
+
// (`_inBody`). The server renders the component's bindings once, in that
|
|
131
|
+
// frame, after the function has returned: a pending() read there is the read
|
|
132
|
+
// the client's binding makes, tracked (reactive.js devUntrackedPending).
|
|
133
|
+
function inBody(frame, fn, arg) {
|
|
134
|
+
frame._inBody = true;
|
|
135
|
+
try {
|
|
136
|
+
return fn(arg);
|
|
137
|
+
} finally {
|
|
138
|
+
frame._inBody = false;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Call `fn(arg)`, which runs a server component's function, in its `frame` so
|
|
144
|
+
* that the code it runs after an `await` still has the frame as its owner
|
|
145
|
+
* (renderOwner).
|
|
146
|
+
*
|
|
147
|
+
* @internal
|
|
148
|
+
*/
|
|
149
|
+
export function _runComponentAsync(frame, fn, arg) {
|
|
150
|
+
const frames = store().frames;
|
|
151
|
+
if (__DEV__) return frames === null ? inBody(frame, fn, arg) : frames.run({ engine: _engine, frame }, inBody, frame, fn, arg);
|
|
152
|
+
return frames === null ? fn(arg) : frames.run({ engine: _engine, frame }, fn, arg);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Call `fn(arg)`, which runs a server component's function, in the
|
|
157
|
+
* component's `frame`, synchronously (renderToString).
|
|
158
|
+
*
|
|
159
|
+
* @internal
|
|
160
|
+
*/
|
|
161
|
+
export function _runComponentSync(frame, fn, arg) {
|
|
162
|
+
return __DEV__ ? inBody(frame, fn, arg) : fn(arg);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Whether code outside every render runs on a server, where a query cache
|
|
166
|
+
// it shared could hand one request's data to another. what-server's
|
|
167
|
+
// ensureServerRuntime marks the process. It is a server unless it had a
|
|
168
|
+
// document when marked (a test that renders both ways in one process) AND
|
|
169
|
+
// has AsyncLocalStorage: only then does everything a request runs keep its
|
|
170
|
+
// render context past an `await`, so code outside every context is no
|
|
171
|
+
// request's. Without AsyncLocalStorage a render's code after an `await` runs
|
|
172
|
+
// outside its context, and a DOM shim (jsdom, happy-dom, a Workers polyfill)
|
|
173
|
+
// must not make that code share one cache.
|
|
174
|
+
/** @internal what-server: this process renders on the server. */
|
|
175
|
+
export function __markServerProcess() {
|
|
176
|
+
const state = store();
|
|
177
|
+
state.server = true;
|
|
178
|
+
state.withDocument = typeof document !== 'undefined';
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** @internal Whether code outside a render runs on a server (see above). */
|
|
182
|
+
export function _isServerProcess() {
|
|
183
|
+
const state = shared ?? /** @type {any} */ (globalThis)[STORE];
|
|
184
|
+
return state != null && state.server && (!state.withDocument || state.storage === null);
|
|
31
185
|
}
|
|
32
186
|
|
|
33
187
|
/** @returns the active render context, or null on the client / outside a render. */
|
|
188
|
+
// Read without making the store (see isServerRender below): a browser that no
|
|
189
|
+
// render touched has none, and no context.
|
|
34
190
|
export function getServerContext() {
|
|
35
|
-
|
|
191
|
+
const state = shared ?? /** @type {any} */ (globalThis)[STORE];
|
|
192
|
+
if (state == null) return null;
|
|
193
|
+
if (state.current != null) return state.current;
|
|
194
|
+
return state.storage ? state.storage.getStore() ?? null : null;
|
|
36
195
|
}
|
|
37
196
|
|
|
38
197
|
/**
|
|
@@ -43,6 +202,14 @@ export function getServerContext() {
|
|
|
43
202
|
* two answers diverge under every DOM shim (jsdom, happy-dom, a Workers
|
|
44
203
|
* polyfill) and in any test that renders both ways in one process.
|
|
45
204
|
*/
|
|
205
|
+
// With a document, it reads the store as getServerContext() does, which does
|
|
206
|
+
// not make it: a page that no server render ever touched has none, and is
|
|
207
|
+
// not rendering. So a browser bundle whose hooks ask (useEffect, onMount),
|
|
208
|
+
// or whose router reads the render (getServerContext, _isServerProcess),
|
|
209
|
+
// does not carry the store and the owner lookup it installs (store(),
|
|
210
|
+
// renderOwner); a copy of the engine that a render reaches later still finds
|
|
211
|
+
// its root through the store's `adopt` (reactive.js ownerNow), and the copy
|
|
212
|
+
// that renders installs its own when it starts (serving()).
|
|
46
213
|
export function isServerRender() {
|
|
47
214
|
return typeof document === 'undefined' || getServerContext() != null;
|
|
48
215
|
}
|
|
@@ -52,26 +219,89 @@ export function isServerRender() {
|
|
|
52
219
|
* manually (runWithServerContext does this for you).
|
|
53
220
|
*/
|
|
54
221
|
export function setServerContext(ctx) {
|
|
55
|
-
const
|
|
56
|
-
|
|
222
|
+
const state = serving();
|
|
223
|
+
const prev = state.current;
|
|
224
|
+
state.current = ctx;
|
|
57
225
|
return prev;
|
|
58
226
|
}
|
|
59
227
|
|
|
60
228
|
/**
|
|
61
229
|
* Run `fn` with `ctx` as the active context, restoring the previous context
|
|
62
|
-
* afterwards (even if `fn` throws). Returns whatever `fn` returns.
|
|
63
|
-
*
|
|
230
|
+
* afterwards (even if `fn` throws). Returns whatever `fn` returns. Work `fn`
|
|
231
|
+
* starts that resumes after an `await` keeps `ctx` only when an async context
|
|
232
|
+
* provider is installed.
|
|
64
233
|
*/
|
|
65
234
|
export function runWithServerContext(ctx, fn) {
|
|
66
|
-
|
|
67
|
-
|
|
235
|
+
const state = serving();
|
|
236
|
+
const prev = state.current;
|
|
237
|
+
state.current = ctx;
|
|
238
|
+
const run = ctx && ctx.owner ? () => runWithOwner(ctx.owner, fn) : fn;
|
|
239
|
+
try {
|
|
240
|
+
return state.storage ? state.storage.run(ctx, run) : run();
|
|
241
|
+
} finally {
|
|
242
|
+
state.current = prev;
|
|
68
243
|
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Run the effect re-runs that are waiting, outside every render context.
|
|
248
|
+
*
|
|
249
|
+
* A write one render makes to a module-scope signal queues the re-runs of the
|
|
250
|
+
* effects other renders still in flight created under it. Left to the reactive
|
|
251
|
+
* flush, they run in a microtask that inherits the writing render's async
|
|
252
|
+
* context, and an effect from one request would read the other request's
|
|
253
|
+
* response and loader data. A renderer calls this after each synchronous slice,
|
|
254
|
+
* so they run with no request visible to them.
|
|
255
|
+
*
|
|
256
|
+
* @internal
|
|
257
|
+
*/
|
|
258
|
+
export function __flushOutsideServerContext() {
|
|
259
|
+
__outsideServerContext(flushSync);
|
|
260
|
+
}
|
|
69
261
|
|
|
70
|
-
|
|
71
|
-
|
|
262
|
+
/**
|
|
263
|
+
* Run `fn` outside every render: no render context visible and no owner
|
|
264
|
+
* current, not even the one an active async context would supply. A renderer
|
|
265
|
+
* makes each render's own root this way, so it is independent of any render
|
|
266
|
+
* that started it.
|
|
267
|
+
*
|
|
268
|
+
* @internal
|
|
269
|
+
*/
|
|
270
|
+
export function __outsideServerContext(fn) {
|
|
271
|
+
const state = store();
|
|
272
|
+
const prev = state.current;
|
|
273
|
+
const storage = state.storage;
|
|
274
|
+
const frames = state.frames;
|
|
275
|
+
state.current = null;
|
|
276
|
+
const outside = frames ? () => frames.run(null, fn) : fn;
|
|
72
277
|
try {
|
|
73
|
-
return
|
|
278
|
+
return _detached(storage ? () => storage.run(null, outside) : outside);
|
|
74
279
|
} finally {
|
|
75
|
-
|
|
280
|
+
state.current = prev;
|
|
76
281
|
}
|
|
77
282
|
}
|
|
283
|
+
|
|
284
|
+
// What getResponse() hands out where there is no server render (the client, a
|
|
285
|
+
// test): every call is accepted and does nothing, so a component can set its
|
|
286
|
+
// status without checking where it runs.
|
|
287
|
+
const NO_RESPONSE = /* @__PURE__ */ Object.freeze({
|
|
288
|
+
status: 200,
|
|
289
|
+
location: null,
|
|
290
|
+
committed: false,
|
|
291
|
+
setStatus() {},
|
|
292
|
+
setHeader() {},
|
|
293
|
+
redirect() {},
|
|
294
|
+
redirectExternal() {},
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* The response of the server render in progress: `setStatus(code)`,
|
|
299
|
+
* `setHeader(name, value, { append })`, `redirect(path, status)` (same-origin
|
|
300
|
+
* paths only) and `redirectExternal(url, status)`. They take effect until the
|
|
301
|
+
* shell is sent; a redirect after that is sent to the browser as a script. On
|
|
302
|
+
* the client it returns an inert object.
|
|
303
|
+
*/
|
|
304
|
+
export function getResponse() {
|
|
305
|
+
const ctx = getServerContext();
|
|
306
|
+
return (ctx && ctx.response) || NO_RESPONSE;
|
|
307
|
+
}
|
package/src/skeleton.js
CHANGED
|
@@ -80,7 +80,7 @@ function injectStyles() {
|
|
|
80
80
|
* @param {number|string} [props.height]
|
|
81
81
|
* @param {'shimmer'|'pulse'|'wave'} [props.variant]
|
|
82
82
|
* @param {boolean} [props.circle]
|
|
83
|
-
* @param {string} [props.class]
|
|
83
|
+
* @param {string | (() => string)} [props.class]
|
|
84
84
|
* @param {Record<string, any>} [props.style]
|
|
85
85
|
* @param {number} [props.count]
|
|
86
86
|
*/
|
|
@@ -92,11 +92,14 @@ export function Skeleton({
|
|
|
92
92
|
class: className,
|
|
93
93
|
style: customStyle,
|
|
94
94
|
count = 1,
|
|
95
|
-
}) {
|
|
95
|
+
} = {}) {
|
|
96
96
|
injectStyles();
|
|
97
97
|
|
|
98
98
|
const baseClass = `what-skeleton ${variant === 'pulse' ? 'what-skeleton-pulse' : ''} ${variant === 'wave' ? 'what-skeleton-wave' : ''}`;
|
|
99
|
-
const
|
|
99
|
+
const withClass = (extra) => (extra ? `${baseClass} ${extra}` : baseClass);
|
|
100
|
+
// A reactive `class` (a signal or a getter) stays reactive: it is read in
|
|
101
|
+
// the binding, never coerced to a string here.
|
|
102
|
+
const finalClass = typeof className === 'function' ? () => withClass(className()) : withClass(className);
|
|
100
103
|
|
|
101
104
|
const style = {
|
|
102
105
|
width: typeof width === 'number' ? `${width}px` : width,
|
|
@@ -127,7 +130,7 @@ export function SkeletonText({
|
|
|
127
130
|
lineHeight = 16,
|
|
128
131
|
gap = 8,
|
|
129
132
|
variant = 'shimmer',
|
|
130
|
-
}) {
|
|
133
|
+
} = {}) {
|
|
131
134
|
injectStyles();
|
|
132
135
|
|
|
133
136
|
return h('div', { class: 'what-skeleton-text', 'aria-hidden': 'true' },
|
|
@@ -155,7 +158,7 @@ export function SkeletonText({
|
|
|
155
158
|
export function SkeletonAvatar({
|
|
156
159
|
size = 40,
|
|
157
160
|
variant = 'shimmer',
|
|
158
|
-
}) {
|
|
161
|
+
} = {}) {
|
|
159
162
|
return Skeleton({
|
|
160
163
|
width: size,
|
|
161
164
|
height: size,
|
|
@@ -170,7 +173,7 @@ export function SkeletonCard({
|
|
|
170
173
|
imageHeight = 200,
|
|
171
174
|
lines = 3,
|
|
172
175
|
variant = 'shimmer',
|
|
173
|
-
}) {
|
|
176
|
+
} = {}) {
|
|
174
177
|
injectStyles();
|
|
175
178
|
|
|
176
179
|
return h('div', { class: 'what-skeleton-card', 'aria-hidden': 'true' },
|
|
@@ -195,7 +198,7 @@ export function SkeletonTable({
|
|
|
195
198
|
rows = 5,
|
|
196
199
|
columns = 4,
|
|
197
200
|
variant = 'shimmer',
|
|
198
|
-
}) {
|
|
201
|
+
} = {}) {
|
|
199
202
|
injectStyles();
|
|
200
203
|
|
|
201
204
|
return h('div', { class: 'what-skeleton-table', 'aria-hidden': 'true' },
|
|
@@ -234,11 +237,12 @@ export function SkeletonTable({
|
|
|
234
237
|
// --- Island Skeleton ---
|
|
235
238
|
// Specific skeleton for island placeholders
|
|
236
239
|
|
|
240
|
+
/** @param {{ type?: string, height?: string | number, children?: any }} [props] */
|
|
237
241
|
export function IslandSkeleton({
|
|
238
242
|
type = 'default', // 'default' | 'card' | 'text' | 'custom'
|
|
239
243
|
height,
|
|
240
244
|
children,
|
|
241
|
-
}) {
|
|
245
|
+
} = {}) {
|
|
242
246
|
injectStyles();
|
|
243
247
|
|
|
244
248
|
if (type === 'card') {
|
|
@@ -271,16 +275,22 @@ export function useSkeleton(asyncFn, _deps = []) {
|
|
|
271
275
|
const data = signal(null);
|
|
272
276
|
const error = signal(null);
|
|
273
277
|
|
|
278
|
+
// A run that a later one superseded (asyncFn read a signal that changed
|
|
279
|
+
// before it settled) settles nothing: the latest run's result is shown.
|
|
280
|
+
let latest = 0;
|
|
274
281
|
effect(() => {
|
|
282
|
+
const run = ++latest;
|
|
275
283
|
isLoading.set(true);
|
|
276
284
|
error.set(null);
|
|
277
285
|
|
|
278
286
|
Promise.resolve(asyncFn())
|
|
279
287
|
.then(result => {
|
|
280
|
-
|
|
288
|
+
if (run !== latest) return;
|
|
289
|
+
data.set(() => result);
|
|
281
290
|
isLoading.set(false);
|
|
282
291
|
})
|
|
283
292
|
.catch(err => {
|
|
293
|
+
if (run !== latest) return;
|
|
284
294
|
error.set(err);
|
|
285
295
|
isLoading.set(false);
|
|
286
296
|
});
|
|
@@ -303,7 +313,7 @@ export function Placeholder({
|
|
|
303
313
|
label = 'Loading...',
|
|
304
314
|
showLabel = false,
|
|
305
315
|
variant = 'shimmer',
|
|
306
|
-
}) {
|
|
316
|
+
} = {}) {
|
|
307
317
|
injectStyles();
|
|
308
318
|
|
|
309
319
|
return h('div', {
|
|
@@ -329,7 +339,7 @@ export function Placeholder({
|
|
|
329
339
|
|
|
330
340
|
// --- Loading Dots ---
|
|
331
341
|
|
|
332
|
-
export function LoadingDots({ size = 8, color = '#666' }) {
|
|
342
|
+
export function LoadingDots({ size = 8, color = '#666' } = {}) {
|
|
333
343
|
injectStyles();
|
|
334
344
|
|
|
335
345
|
const dotStyle = {
|
|
@@ -354,7 +364,7 @@ export function LoadingDots({ size = 8, color = '#666' }) {
|
|
|
354
364
|
|
|
355
365
|
// --- Spinner ---
|
|
356
366
|
|
|
357
|
-
export function Spinner({ size = 24, color = '#666', strokeWidth = 2 }) {
|
|
367
|
+
export function Spinner({ size = 24, color = '#666', strokeWidth = 2 } = {}) {
|
|
358
368
|
return h('svg', {
|
|
359
369
|
width: size,
|
|
360
370
|
height: size,
|
package/src/store.js
CHANGED
|
@@ -3,15 +3,16 @@
|
|
|
3
3
|
// Like Zustand meets signals — define a store, use it anywhere.
|
|
4
4
|
|
|
5
5
|
import { signal, computed, batch, __DEV__ } from './reactive.js';
|
|
6
|
+
import { report } from './diagnostics.js';
|
|
6
7
|
|
|
7
|
-
// ---
|
|
8
|
+
// --- derived ---
|
|
8
9
|
// Marker wrapper to explicitly tag a function as a computed in createStore.
|
|
9
10
|
// Without this, createStore can't distinguish computed(state => ...) from action(item => ...).
|
|
10
11
|
//
|
|
11
12
|
// Usage:
|
|
12
13
|
// const useCounter = createStore({
|
|
13
14
|
// count: 0,
|
|
14
|
-
// doubled:
|
|
15
|
+
// doubled: derived(state => state.count * 2),
|
|
15
16
|
// addItem(item) { /* this is an action */ },
|
|
16
17
|
// });
|
|
17
18
|
|
|
@@ -20,31 +21,28 @@ export function derived(fn) {
|
|
|
20
21
|
return fn;
|
|
21
22
|
}
|
|
22
23
|
|
|
23
|
-
// Deprecated alias — use derived() instead
|
|
24
|
-
let _storeComputedWarned = false;
|
|
25
|
-
export function storeComputed(fn) {
|
|
26
|
-
if (!_storeComputedWarned) {
|
|
27
|
-
_storeComputedWarned = true;
|
|
28
|
-
console.warn('[what] storeComputed() is deprecated. Use derived() instead.');
|
|
29
|
-
}
|
|
30
|
-
return derived(fn);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
24
|
// --- createStore ---
|
|
34
25
|
// Creates a reactive store with actions. Each key becomes a signal.
|
|
35
26
|
//
|
|
27
|
+
// In an action, `this` reads the current state and derived values, and
|
|
28
|
+
// assigning a state key writes its signal. The assignment is what notifies: an
|
|
29
|
+
// in-place mutation such as `this.items.push(item)` changes the array but no
|
|
30
|
+
// reader re-runs, so assign a new value instead. An action runs as one batch
|
|
31
|
+
// and returns what the function returns.
|
|
32
|
+
//
|
|
36
33
|
// Usage:
|
|
37
34
|
// const useCounter = createStore({
|
|
38
35
|
// count: 0,
|
|
39
|
-
//
|
|
36
|
+
// items: [],
|
|
37
|
+
// doubled: derived(state => state.count * 2), // computed
|
|
40
38
|
// increment() { this.count++; }, // action
|
|
41
|
-
//
|
|
42
|
-
// addItem(item) { this.items.push(item); }, // action (not confused with computed)
|
|
39
|
+
// addItem(item) { this.items = [...this.items, item]; },
|
|
43
40
|
// });
|
|
44
41
|
//
|
|
45
42
|
// function Counter() {
|
|
46
|
-
// const
|
|
47
|
-
// return h('div', null, count, ' / ',
|
|
43
|
+
// const counter = useCounter();
|
|
44
|
+
// return h('div', null, () => counter.count, ' / ', () => counter.doubled,
|
|
45
|
+
// h('button', { onClick: counter.increment }, '+'));
|
|
48
46
|
// }
|
|
49
47
|
|
|
50
48
|
export function createStore(definition) {
|
|
@@ -57,11 +55,9 @@ export function createStore(definition) {
|
|
|
57
55
|
for (const [key, value] of Object.entries(definition)) {
|
|
58
56
|
if (typeof value === 'function' && value._storeComputed) {
|
|
59
57
|
if (__DEV__ && value.length === 0) {
|
|
60
|
-
|
|
61
|
-
`[what] derived() for "${key}" should accept the state parameter, e.g. derived(state => ...).`
|
|
62
|
-
);
|
|
58
|
+
report('ERR_DERIVED_WITHOUT_STATE', { data: { key } });
|
|
63
59
|
}
|
|
64
|
-
// Computed: explicitly marked with
|
|
60
|
+
// Computed: explicitly marked with derived()
|
|
65
61
|
computeds[key] = value;
|
|
66
62
|
} else if (typeof value === 'function') {
|
|
67
63
|
// Action: any other function
|
|
@@ -124,13 +120,3 @@ export function createStore(definition) {
|
|
|
124
120
|
return result;
|
|
125
121
|
};
|
|
126
122
|
}
|
|
127
|
-
|
|
128
|
-
// --- Simple atom --- [DEPRECATED: use signal() directly]
|
|
129
|
-
let _atomWarned = false;
|
|
130
|
-
export function atom(initial) {
|
|
131
|
-
if (!_atomWarned) {
|
|
132
|
-
_atomWarned = true;
|
|
133
|
-
console.warn('[what] atom() is deprecated. Use signal() directly instead.');
|
|
134
|
-
}
|
|
135
|
-
return signal(initial);
|
|
136
|
-
}
|