@abide/abide 0.52.0 → 0.53.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/AGENTS.md +16 -7
- package/CHANGELOG.md +25 -0
- package/README.md +7 -5
- package/package.json +2 -1
- package/src/lib/ui/compile/UI_RUNTIME_IMPORTS.ts +1 -0
- package/src/lib/ui/compile/analyzeComponent.ts +2 -0
- package/src/lib/ui/compile/compileComponent.ts +3 -1
- package/src/lib/ui/compile/compileSSR.ts +12 -0
- package/src/lib/ui/compile/compileShadow.ts +37 -34
- package/src/lib/ui/compile/desugarSignals.ts +60 -32
- package/src/lib/ui/compile/generateBuild.ts +5 -1
- package/src/lib/ui/compile/hasTopLevelAwait.ts +28 -0
- package/src/lib/ui/compile/isFunctionScopeBoundary.ts +20 -0
- package/src/lib/ui/compile/liftAsyncSubExpressions.ts +2 -2
- package/src/lib/ui/compile/lowerContext.ts +4 -0
- package/src/lib/ui/compile/lowerScript.ts +11 -1
- package/src/lib/ui/compile/renameSignalRefs.ts +18 -0
- package/src/lib/ui/compile/types/AnalyzedComponent.ts +4 -0
- package/src/lib/ui/dom/appendText.ts +29 -8
- package/src/lib/ui/dom/appendTextAt.ts +21 -4
- package/src/lib/ui/dom/attr.ts +16 -1
- package/src/lib/ui/dom/awaitBlock.ts +34 -13
- package/src/lib/ui/dom/each.ts +10 -2
- package/src/lib/ui/dom/eachAsync.ts +9 -1
- package/src/lib/ui/dom/readCellBlocking.ts +29 -0
- package/src/lib/ui/dom/readTextOrSuspend.ts +21 -0
- package/src/lib/ui/dom/spreadAttrs.ts +47 -1
- package/src/lib/ui/dom/switchBlock.ts +19 -2
- package/src/lib/ui/dom/tryBlock.ts +10 -0
- package/src/lib/ui/dom/when.ts +19 -1
- package/src/lib/ui/dom/withSuspense.ts +22 -0
- package/src/lib/ui/runtime/SuspenseSignal.ts +24 -0
- package/src/lib/ui/runtime/flushEffects.ts +18 -10
- package/src/lib/ui/watch.ts +7 -2
|
@@ -55,6 +55,10 @@ export function signalRefsTransformer(
|
|
|
55
55
|
`computed` (see `desugarSignals`). A nearer lexical binding of the same name shadows
|
|
56
56
|
them like any other signal. */
|
|
57
57
|
cellReadNames: ReadonlySet<string> = new Set(),
|
|
58
|
+
/* The subset of `cellReadNames` that are BLOCKING `await` cells (ADR-0042), read through
|
|
59
|
+
`$$readCellBlocking(name)` (suspend-on-pending) instead of `$$readCell`. Non-empty only on
|
|
60
|
+
the CLIENT template lowering; the script pass and SSR keep it empty (`$$readCell`). */
|
|
61
|
+
blockingCellNames: ReadonlySet<string> = new Set(),
|
|
58
62
|
): ts.TransformerFactory<ts.SourceFile> {
|
|
59
63
|
/* The signal names that a nested binding can shadow — only these matter for
|
|
60
64
|
scope tracking, so we ignore every other local binding. */
|
|
@@ -162,6 +166,7 @@ export function signalRefsTransformer(
|
|
|
162
166
|
computedNames,
|
|
163
167
|
blockLocal,
|
|
164
168
|
cellReadNames,
|
|
169
|
+
blockingCellNames,
|
|
165
170
|
)
|
|
166
171
|
if (replacement !== undefined) {
|
|
167
172
|
return ts.factory.createPropertyAssignment(node.name.text, replacement)
|
|
@@ -175,6 +180,7 @@ export function signalRefsTransformer(
|
|
|
175
180
|
computedNames,
|
|
176
181
|
blockLocal,
|
|
177
182
|
cellReadNames,
|
|
183
|
+
blockingCellNames,
|
|
178
184
|
)
|
|
179
185
|
if (replacement !== undefined) {
|
|
180
186
|
return replacement
|
|
@@ -393,6 +399,7 @@ function referenceFor(
|
|
|
393
399
|
computedNames: ReadonlySet<string>,
|
|
394
400
|
blockLocal: ReadonlySet<string> = new Set(),
|
|
395
401
|
cellReadNames: ReadonlySet<string> = new Set(),
|
|
402
|
+
blockingCellNames: ReadonlySet<string> = new Set(),
|
|
396
403
|
): ts.Expression | undefined {
|
|
397
404
|
if (blockLocal.has(name)) {
|
|
398
405
|
return ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier(name), 'value')
|
|
@@ -402,6 +409,17 @@ function referenceFor(
|
|
|
402
409
|
if (name === 'scope') {
|
|
403
410
|
return ts.factory.createIdentifier('$$scope')
|
|
404
411
|
}
|
|
412
|
+
/* A BLOCKING `await` cell → `$$readCellBlocking(name)` (ADR-0042): the throwing peek that
|
|
413
|
+
SUSPENDS the render region while pending, so an `await` binding reads as a resolved value
|
|
414
|
+
and its region withholds until it settles. Client template lowering only — `blockingCellNames`
|
|
415
|
+
is empty in the script pass and SSR, which keep the plain `$$readCell` peek below. */
|
|
416
|
+
if (blockingCellNames.has(name)) {
|
|
417
|
+
return ts.factory.createCallExpression(
|
|
418
|
+
ts.factory.createIdentifier('$$readCellBlocking'),
|
|
419
|
+
undefined,
|
|
420
|
+
[ts.factory.createIdentifier(name)],
|
|
421
|
+
)
|
|
422
|
+
}
|
|
405
423
|
/* A `linked` / async `computed` cell → `$$readCell(name)`: one read shape that peeks an
|
|
406
424
|
async cell and reads `.value` off a sync one. */
|
|
407
425
|
if (cellReadNames.has(name)) {
|
|
@@ -28,6 +28,10 @@ export type AnalyzedComponent = {
|
|
|
28
28
|
`computed`. Threaded to both back-ends so a template reference (`{draft}`) lowers to
|
|
29
29
|
the unified cell read consistently on client and server. */
|
|
30
30
|
cellReadNames: Set<string>
|
|
31
|
+
/* The subset of `cellReadNames` that are BLOCKING `await` cells (ADR-0042): a template-injected
|
|
32
|
+
`{await X}` cell or a script `computed(await …)`. The CLIENT back-end reads these via
|
|
33
|
+
`$$readCellBlocking` (suspend-on-pending) rather than `$$readCell`. */
|
|
34
|
+
blockingCellNames: Set<string>
|
|
31
35
|
nodes: TemplateNode[]
|
|
32
36
|
/* One entry per non-empty `<style>` in the template (in source order): the scope
|
|
33
37
|
attribute its covered elements carry (annotated onto `nodes`) and the scoped
|
|
@@ -3,10 +3,12 @@ import { snippetPayload } from '../../shared/snippet.ts'
|
|
|
3
3
|
import { effect } from '../effect.ts'
|
|
4
4
|
import { claimChild } from '../runtime/claimChild.ts'
|
|
5
5
|
import { RENDER } from '../runtime/RENDER.ts'
|
|
6
|
+
import { SuspenseSignal } from '../runtime/SuspenseSignal.ts'
|
|
6
7
|
import { appendSnippet } from './appendSnippet.ts'
|
|
7
8
|
import { assertClaimedText } from './assertClaimedText.ts'
|
|
8
9
|
import { isComment } from './isComment.ts'
|
|
9
10
|
import { parseRawNodes } from './parseRawNodes.ts'
|
|
11
|
+
import { readTextOrSuspend } from './readTextOrSuspend.ts'
|
|
10
12
|
|
|
11
13
|
const CLOSE = '/abide:html'
|
|
12
14
|
|
|
@@ -22,16 +24,36 @@ its parsed nodes go between an anchor (create), or the server-rendered nodes
|
|
|
22
24
|
between `<!--abide:html-->`/`<!--/abide:html-->` markers are adopted (hydrate), and
|
|
23
25
|
a change re-parses and swaps. A binding is text or raw for its lifetime (decided by
|
|
24
26
|
its first value), so plain text — the common case — stays a cheap single node.
|
|
27
|
+
|
|
28
|
+
A blocking `await` read that is pending throws a `SuspenseSignal` (ADR-0042): the
|
|
29
|
+
interpolation SUSPENDS — it renders empty and withholds until the value resolves,
|
|
30
|
+
never evaluating the surrounding expression against a pending `undefined`. The read
|
|
31
|
+
tracked the cell, so the bind effect re-runs on settle. A suspend can only occur on a
|
|
32
|
+
cold client render (on hydrate the warm-seed makes the cell `refreshing()`, not
|
|
33
|
+
`pending()`, D4), and a suspended value is treated as text — its snippet/html shape is
|
|
34
|
+
decided from the first resolved value.
|
|
25
35
|
*/
|
|
26
36
|
// @documentation plumbing
|
|
27
37
|
export function appendText(parent: Node, read: () => unknown, splitAlways = false): void {
|
|
38
|
+
/* Probe the first value once, tolerating a suspend (a pending blocking read) — a suspended
|
|
39
|
+
interpolation skips snippet/html detection and takes the text path, starting empty. */
|
|
40
|
+
let probe: unknown
|
|
41
|
+
let suspended = false
|
|
42
|
+
try {
|
|
43
|
+
probe = read()
|
|
44
|
+
} catch (signal) {
|
|
45
|
+
if (!(signal instanceof SuspenseSignal)) {
|
|
46
|
+
throw signal
|
|
47
|
+
}
|
|
48
|
+
suspended = true
|
|
49
|
+
}
|
|
28
50
|
/* A snippet call (`{row(args)}`) mounts its builder; a `html\`\`` value inserts
|
|
29
51
|
raw markup; everything else is escaped text — decided by the first value. */
|
|
30
|
-
if (typeof snippetPayload(
|
|
52
|
+
if (!suspended && typeof snippetPayload(probe) === 'function') {
|
|
31
53
|
appendSnippet(parent, read)
|
|
32
54
|
return
|
|
33
55
|
}
|
|
34
|
-
if (rawHtmlString(
|
|
56
|
+
if (!suspended && rawHtmlString(probe) !== undefined) {
|
|
35
57
|
appendRawHtml(parent, read)
|
|
36
58
|
return
|
|
37
59
|
}
|
|
@@ -39,8 +61,9 @@ export function appendText(parent: Node, read: () => unknown, splitAlways = fals
|
|
|
39
61
|
if (hydration !== undefined) {
|
|
40
62
|
const claimed = claimChild(hydration, parent)
|
|
41
63
|
/* Nullish reads render as empty text, never the literal `"undefined"` — so a
|
|
42
|
-
pending async read (undefined-while-pending, ADR-0032 D3) shows nothing.
|
|
43
|
-
|
|
64
|
+
pending async read (undefined-while-pending, ADR-0032 D3) shows nothing. A blocking
|
|
65
|
+
read never suspends here (warm-seeded → `refreshing()`), so `probe` holds its value. */
|
|
66
|
+
const firstValue = probe
|
|
44
67
|
const value = firstValue == null ? '' : String(firstValue)
|
|
45
68
|
/* A value that first rendered empty produced NO server text node, so the cursor
|
|
46
69
|
points at the following node (an element/comment) or past the end (null) — not a
|
|
@@ -77,16 +100,14 @@ export function appendText(parent: Node, read: () => unknown, splitAlways = fals
|
|
|
77
100
|
at the end). */
|
|
78
101
|
hydration.next.set(parent, isText ? node.nextSibling : claimed)
|
|
79
102
|
effect(() => {
|
|
80
|
-
|
|
81
|
-
node.data = next == null ? '' : String(next)
|
|
103
|
+
node.data = readTextOrSuspend(read)
|
|
82
104
|
})
|
|
83
105
|
return
|
|
84
106
|
}
|
|
85
107
|
const node = document.createTextNode('')
|
|
86
108
|
parent.appendChild(node)
|
|
87
109
|
effect(() => {
|
|
88
|
-
|
|
89
|
-
node.data = next == null ? '' : String(next)
|
|
110
|
+
node.data = readTextOrSuspend(read)
|
|
90
111
|
})
|
|
91
112
|
}
|
|
92
113
|
|
|
@@ -2,9 +2,11 @@ import { rawHtmlString } from '../../shared/html.ts'
|
|
|
2
2
|
import { snippetPayload } from '../../shared/snippet.ts'
|
|
3
3
|
import { effect } from '../effect.ts'
|
|
4
4
|
import { RENDER } from '../runtime/RENDER.ts'
|
|
5
|
+
import { SuspenseSignal } from '../runtime/SuspenseSignal.ts'
|
|
5
6
|
import { appendSnippet } from './appendSnippet.ts'
|
|
6
7
|
import { appendText } from './appendText.ts'
|
|
7
8
|
import { parseRawNodes } from './parseRawNodes.ts'
|
|
9
|
+
import { readTextOrSuspend } from './readTextOrSuspend.ts'
|
|
8
10
|
|
|
9
11
|
/*
|
|
10
12
|
A reactive `{expr}` interpolation mounted at a skeleton anchor comment (`<!--a-->`), used
|
|
@@ -39,14 +41,25 @@ export function appendTextAt(anchor: Node, read: () => unknown): void {
|
|
|
39
41
|
return
|
|
40
42
|
}
|
|
41
43
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
+
/* Probe once, tolerating a suspend (a pending blocking read): a suspended interpolation skips
|
|
45
|
+
snippet/html detection and takes the text path, starting empty until the value resolves. */
|
|
46
|
+
let first: unknown
|
|
47
|
+
let suspended = false
|
|
48
|
+
try {
|
|
49
|
+
first = read()
|
|
50
|
+
} catch (signal) {
|
|
51
|
+
if (!(signal instanceof SuspenseSignal)) {
|
|
52
|
+
throw signal
|
|
53
|
+
}
|
|
54
|
+
suspended = true
|
|
55
|
+
}
|
|
56
|
+
if (!suspended && typeof snippetPayload(first) === 'function') {
|
|
44
57
|
const fragment = document.createDocumentFragment()
|
|
45
58
|
appendSnippet(fragment, read)
|
|
46
59
|
parent.insertBefore(fragment, anchor.nextSibling)
|
|
47
60
|
return
|
|
48
61
|
}
|
|
49
|
-
if (rawHtmlString(first) !== undefined) {
|
|
62
|
+
if (!suspended && rawHtmlString(first) !== undefined) {
|
|
50
63
|
let nodes: Node[] = []
|
|
51
64
|
effect(() => {
|
|
52
65
|
for (const node of nodes) {
|
|
@@ -63,7 +76,11 @@ export function appendTextAt(anchor: Node, read: () => unknown): void {
|
|
|
63
76
|
}
|
|
64
77
|
const node = document.createTextNode('')
|
|
65
78
|
parent.insertBefore(node, anchor.nextSibling)
|
|
79
|
+
/* Text/suspend handling shared with `appendText`'s bind via `readTextOrSuspend`: stringify the
|
|
80
|
+
value, show '' while a blocking read is pending (re-running on settle, since the read tracked
|
|
81
|
+
its cell), and coerce nullish to '' (never the literal "undefined") — keeping this create path
|
|
82
|
+
congruent with the hydrate path above, which delegates to `appendText`. */
|
|
66
83
|
effect(() => {
|
|
67
|
-
node.data =
|
|
84
|
+
node.data = readTextOrSuspend(read)
|
|
68
85
|
})
|
|
69
86
|
}
|
package/src/lib/ui/dom/attr.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { effect } from '../effect.ts'
|
|
2
2
|
import { RENDER } from '../runtime/RENDER.ts'
|
|
3
3
|
import { reportHydrationDivergence } from '../runtime/reportHydrationDivergence.ts'
|
|
4
|
+
import { SuspenseSignal } from '../runtime/SuspenseSignal.ts'
|
|
4
5
|
|
|
5
6
|
/* The attribute string an element carries for `value`, or `null` when absent — the present/absent +
|
|
6
7
|
stringify semantics below, factored so the hydration compare and the write agree exactly. */
|
|
@@ -24,7 +25,21 @@ export function attr(element: Element, name: string, read: () => unknown): void
|
|
|
24
25
|
invisible, since the effect always overwrites. */
|
|
25
26
|
let hydrating = RENDER.hydration !== undefined
|
|
26
27
|
effect(() => {
|
|
27
|
-
|
|
28
|
+
let value: unknown
|
|
29
|
+
try {
|
|
30
|
+
value = read()
|
|
31
|
+
} catch (signal) {
|
|
32
|
+
/* A pending blocking `await` read (ADR-0042) suspends this attribute: leave it
|
|
33
|
+
unset until the value resolves. The read tracked its cell, so this effect
|
|
34
|
+
re-runs on settle. A suspend can only occur on a cold client render (warm-seed
|
|
35
|
+
keeps a hydrating read `refreshing()`, never `pending()`), so the hydration
|
|
36
|
+
compare below is never reached while suspended. */
|
|
37
|
+
if (!(signal instanceof SuspenseSignal)) {
|
|
38
|
+
throw signal
|
|
39
|
+
}
|
|
40
|
+
element.removeAttribute(name)
|
|
41
|
+
return
|
|
42
|
+
}
|
|
28
43
|
const next = attributeText(value)
|
|
29
44
|
if (hydrating) {
|
|
30
45
|
hydrating = false
|
|
@@ -8,6 +8,7 @@ import { RENDER } from '../runtime/RENDER.ts'
|
|
|
8
8
|
import type { ResumeEntry } from '../runtime/RESUME.ts'
|
|
9
9
|
import { RESUME } from '../runtime/RESUME.ts'
|
|
10
10
|
import { scopeGroup } from '../runtime/scopeGroup.ts'
|
|
11
|
+
import { SuspenseSignal } from '../runtime/SuspenseSignal.ts'
|
|
11
12
|
import type { State } from '../runtime/types/State.ts'
|
|
12
13
|
import { withoutHydration } from '../runtime/withoutHydration.ts'
|
|
13
14
|
import { state } from '../state.ts'
|
|
@@ -109,17 +110,10 @@ export function awaitBlock(
|
|
|
109
110
|
activeKind = 'catch'
|
|
110
111
|
}
|
|
111
112
|
|
|
112
|
-
/*
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
settleThen(result) // warm-sync → resolved now, no flash
|
|
117
|
-
return
|
|
118
|
-
}
|
|
119
|
-
/* A then-branch is already mounted (a revalidation): keep it visible and update in
|
|
120
|
-
place when the new value settles, instead of blanking to pending and rebuilding —
|
|
121
|
-
this is the no-flash live-update path. A first load (or a prior pending/catch)
|
|
122
|
-
shows the pending branch (or detaches) while the promise is in flight. */
|
|
113
|
+
/* Show the pending branch (or detach when there is none), UNLESS a then-branch is already
|
|
114
|
+
mounted — a revalidation keeps it visible and updates in place when the new value settles
|
|
115
|
+
(the no-flash live-update path). Shared by an in-flight promise and a suspended subject read. */
|
|
116
|
+
const showPending = (): void => {
|
|
123
117
|
if (activeKind !== 'then') {
|
|
124
118
|
if (renderPending !== undefined) {
|
|
125
119
|
branch.place((host) => renderPending(host))
|
|
@@ -129,6 +123,18 @@ export function awaitBlock(
|
|
|
129
123
|
activeKind = undefined
|
|
130
124
|
}
|
|
131
125
|
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/* Render a settled-or-pending result into the current generation. */
|
|
129
|
+
const render = (result: unknown): void => {
|
|
130
|
+
const gen = guard.token()
|
|
131
|
+
if (!isThenable(result)) {
|
|
132
|
+
settleThen(result) // warm-sync → resolved now, no flash
|
|
133
|
+
return
|
|
134
|
+
}
|
|
135
|
+
/* A first load (or a prior pending/catch) shows the pending branch while the promise is in
|
|
136
|
+
flight; a live then-branch stays put (see `showPending`). */
|
|
137
|
+
showPending()
|
|
132
138
|
result.then(
|
|
133
139
|
(value) => {
|
|
134
140
|
if (guard.live(gen)) {
|
|
@@ -296,8 +302,23 @@ export function awaitBlock(
|
|
|
296
302
|
}
|
|
297
303
|
/* Read the promise every subsequent run so an invalidate re-runs the block. ONLY this
|
|
298
304
|
read is tracked (the branch builds untracked via `scope`), so the block re-runs only
|
|
299
|
-
when its promise source does, not on any branch-state change.
|
|
300
|
-
|
|
305
|
+
when its promise source does, not on any branch-state change. A blocking `await` cell
|
|
306
|
+
embedded in the subject (`{#await loadMore(user.id)}` where `user` is an `await` cell)
|
|
307
|
+
throws a `SuspenseSignal` while pending (ADR-0042) — on a COLD client render the read
|
|
308
|
+
runs synchronously here, so withhold to the pending branch until it settles (mirroring
|
|
309
|
+
the local suspense withhold every other reading region does), rather than letting the
|
|
310
|
+
throw escape the build. The read tracked its cell, so this effect re-runs on resolve. */
|
|
311
|
+
let result: unknown
|
|
312
|
+
try {
|
|
313
|
+
result = promiseThunk()
|
|
314
|
+
} catch (signal) {
|
|
315
|
+
if (!(signal instanceof SuspenseSignal)) {
|
|
316
|
+
throw signal
|
|
317
|
+
}
|
|
318
|
+
showPending()
|
|
319
|
+
return
|
|
320
|
+
}
|
|
321
|
+
render(result)
|
|
301
322
|
})
|
|
302
323
|
}
|
|
303
324
|
|
package/src/lib/ui/dom/each.ts
CHANGED
|
@@ -13,6 +13,7 @@ import { buildDetachedRange } from './buildDetachedRange.ts'
|
|
|
13
13
|
import { moveRange } from './moveRange.ts'
|
|
14
14
|
import { removeRange } from './removeRange.ts'
|
|
15
15
|
import type { EachRow } from './types/EachRow.ts'
|
|
16
|
+
import { withSuspense } from './withSuspense.ts'
|
|
16
17
|
|
|
17
18
|
/*
|
|
18
19
|
Keyed list binding — the runtime for `<template each key=>`. Each row is a content
|
|
@@ -119,6 +120,13 @@ export function each<T>(
|
|
|
119
120
|
}
|
|
120
121
|
}
|
|
121
122
|
|
|
123
|
+
/* Read the source, tolerating a pending blocking `await` read (a SuspenseSignal): treat a
|
|
124
|
+
suspend as an empty list — identical to the undefined-while-pending peek (ADR-0032 D3) — so
|
|
125
|
+
the each renders no rows until the source resolves. The read still tracked its cell, so the
|
|
126
|
+
driving effect re-runs on settle. */
|
|
127
|
+
const readSource = (): Iterable<T> | undefined =>
|
|
128
|
+
withSuspense<Iterable<T> | undefined>(items, undefined)
|
|
129
|
+
|
|
122
130
|
let anchor: Node
|
|
123
131
|
/* When hydrating, the first effect run must NOT reconcile — the rows it would
|
|
124
132
|
build are already adopted in place below. */
|
|
@@ -128,7 +136,7 @@ export function each<T>(
|
|
|
128
136
|
let position = 0
|
|
129
137
|
/* An undefined source renders an empty list, not a throw — a `{#for x in promise}`
|
|
130
138
|
whose lifted source peeks undefined while pending (ADR-0032 D3). */
|
|
131
|
-
for (const item of
|
|
139
|
+
for (const item of readSource() ?? []) {
|
|
132
140
|
const key = keyOf(item)
|
|
133
141
|
const row = buildRow(item, position) // claims the SSR row where it sits
|
|
134
142
|
if (rows.has(key)) {
|
|
@@ -156,7 +164,7 @@ export function each<T>(
|
|
|
156
164
|
effect(() => {
|
|
157
165
|
/* Read (subscribe) every run, including the adopting one. Materialize a
|
|
158
166
|
non-array iterable to an array so a generator yields fresh each run. */
|
|
159
|
-
const source =
|
|
167
|
+
const source = readSource()
|
|
160
168
|
if (adopting) {
|
|
161
169
|
adopting = false // rows already adopted in document order; nothing to move
|
|
162
170
|
return
|
|
@@ -11,6 +11,7 @@ import { state } from '../state.ts'
|
|
|
11
11
|
import { buildDetachedRange } from './buildDetachedRange.ts'
|
|
12
12
|
import { removeRange } from './removeRange.ts'
|
|
13
13
|
import type { EachRow } from './types/EachRow.ts'
|
|
14
|
+
import { withSuspense } from './withSuspense.ts'
|
|
14
15
|
|
|
15
16
|
/*
|
|
16
17
|
Async keyed list — the runtime for `<template each await key=>`. Like `each`, but
|
|
@@ -113,7 +114,14 @@ export function eachAsync<T>(
|
|
|
113
114
|
removeRange(row.start, row.end)
|
|
114
115
|
}
|
|
115
116
|
rows.clear()
|
|
116
|
-
|
|
117
|
+
/* Read (subscribe) the source synchronously, tolerating a pending blocking `await` read
|
|
118
|
+
embedded in it (a SuspenseSignal, ADR-0042): withhold — render no rows until it resolves,
|
|
119
|
+
exactly as sync `each` does. On a cold client render the read runs here at build; the
|
|
120
|
+
read tracked its cell, so this effect re-runs on settle. */
|
|
121
|
+
const iterable = withSuspense<AsyncIterable<T> | undefined>(items, undefined)
|
|
122
|
+
if (iterable === undefined) {
|
|
123
|
+
return
|
|
124
|
+
}
|
|
117
125
|
const present = new Set<string>()
|
|
118
126
|
let arrivals = 0 // stream arrival ordinal → each row's index
|
|
119
127
|
const drain = async (): Promise<void> => {
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { isAsyncCell } from '../../shared/isAsyncCell.ts'
|
|
2
|
+
import type { AsyncComputed } from '../../shared/types/AsyncComputed.ts'
|
|
3
|
+
import { SuspenseSignal } from '../runtime/SuspenseSignal.ts'
|
|
4
|
+
import { readCell } from './readCell.ts'
|
|
5
|
+
|
|
6
|
+
/*
|
|
7
|
+
The blocking-cell read the compiler lowers to `$$readCellBlocking(NAME)` for an `await`-marked
|
|
8
|
+
cell (ADR-0042 D5.3). Exactly `readCell`, with one added rule ahead of it: a cell with no value
|
|
9
|
+
*yet* throws a `SuspenseSignal` so the enclosing region SUSPENDS until the value resolves rather
|
|
10
|
+
than rendering against `undefined`. This is what makes an `await` binding read as a resolved `T`
|
|
11
|
+
(never `undefined`-while-pending) on both sides — the client mirrors the server flush barrier.
|
|
12
|
+
|
|
13
|
+
The suspend is keyed on `cell.pending()` (in-flight AND no value), NOT `peek() === undefined`:
|
|
14
|
+
- a blocking cell that legitimately resolves to `undefined` is not pending, so it does not
|
|
15
|
+
suspend forever;
|
|
16
|
+
- a warm-seeded cell on hydrate is `refreshing()` (has value, in flight), not `pending()`, so it
|
|
17
|
+
reads its held value and adopts the SSR markup with no suspend and no flash (ADR-0042 D4).
|
|
18
|
+
Everything past the pending gate — the throwing peek (error-with-no-retained-value → `AsyncCellError`
|
|
19
|
+
to `{#try}`, a retained value winning over pending/error via SWR), a function call, or a `.value`
|
|
20
|
+
read — is `readCell` verbatim, so the two reads can never drift.
|
|
21
|
+
*/
|
|
22
|
+
// @documentation plumbing
|
|
23
|
+
export function readCellBlocking(cell: unknown): unknown {
|
|
24
|
+
/* No value yet → suspend the reading region (distinct from an error, which `readCell` routes). */
|
|
25
|
+
if (isAsyncCell(cell) && (cell as AsyncComputed<unknown>).pending()) {
|
|
26
|
+
throw new SuspenseSignal(cell as AsyncComputed<unknown>)
|
|
27
|
+
}
|
|
28
|
+
return readCell(cell)
|
|
29
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { SuspenseSignal } from '../runtime/SuspenseSignal.ts'
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
The text a reactive `{expr}` binding writes: the stringified value, or `''` while a blocking
|
|
5
|
+
`await` read is pending — a `SuspenseSignal` (ADR-0042). Nullish stringifies to `''` (never the
|
|
6
|
+
literal `"undefined"`), so a pending streaming read and a suspended blocking read both show nothing.
|
|
7
|
+
A suspending read still tracked its cell before throwing, so the enclosing bind effect re-runs and
|
|
8
|
+
fills in once the value resolves. Rethrows any non-suspense error unchanged.
|
|
9
|
+
*/
|
|
10
|
+
export function readTextOrSuspend(read: () => unknown): string {
|
|
11
|
+
let next: unknown
|
|
12
|
+
try {
|
|
13
|
+
next = read()
|
|
14
|
+
} catch (signal) {
|
|
15
|
+
if (!(signal instanceof SuspenseSignal)) {
|
|
16
|
+
throw signal
|
|
17
|
+
}
|
|
18
|
+
return ''
|
|
19
|
+
}
|
|
20
|
+
return next == null ? '' : String(next)
|
|
21
|
+
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { effect } from '../effect.ts'
|
|
2
|
+
import { SuspenseSignal } from '../runtime/SuspenseSignal.ts'
|
|
1
3
|
import { attr } from './attr.ts'
|
|
2
4
|
import { on } from './on.ts'
|
|
3
5
|
|
|
@@ -19,7 +21,51 @@ export function spreadAttrs(
|
|
|
19
21
|
exclude: string[] = [],
|
|
20
22
|
): void {
|
|
21
23
|
const skip = new Set(exclude)
|
|
22
|
-
|
|
24
|
+
let object: Record<string, unknown>
|
|
25
|
+
try {
|
|
26
|
+
object = source()
|
|
27
|
+
} catch (signal) {
|
|
28
|
+
/* A spread over a PENDING blocking `await` read throws a `SuspenseSignal` here (ADR-0042):
|
|
29
|
+
its keys can't be enumerated yet. Binding the empty object now would create no `attr`/`on`
|
|
30
|
+
effects, so the attributes would never appear after the cell resolves — instead defer the
|
|
31
|
+
enumeration into a one-shot effect that re-runs on settle (the read tracked its cell). The
|
|
32
|
+
effect is pinned to this build's scope, so the `attr` binds it creates own their lifetime
|
|
33
|
+
correctly; a `spread` flag makes the enumeration fire exactly once (later value changes
|
|
34
|
+
re-read per key via `attr`, never re-enumerate). */
|
|
35
|
+
if (!(signal instanceof SuspenseSignal)) {
|
|
36
|
+
throw signal
|
|
37
|
+
}
|
|
38
|
+
let spread = false
|
|
39
|
+
effect(() => {
|
|
40
|
+
let resolved: Record<string, unknown>
|
|
41
|
+
try {
|
|
42
|
+
resolved = source()
|
|
43
|
+
} catch (retry) {
|
|
44
|
+
if (!(retry instanceof SuspenseSignal)) {
|
|
45
|
+
throw retry
|
|
46
|
+
}
|
|
47
|
+
return
|
|
48
|
+
}
|
|
49
|
+
if (spread) {
|
|
50
|
+
return
|
|
51
|
+
}
|
|
52
|
+
spread = true
|
|
53
|
+
bindKeys(element, source, resolved, skip)
|
|
54
|
+
})
|
|
55
|
+
return
|
|
56
|
+
}
|
|
57
|
+
bindKeys(element, source, object, skip)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/* Binds each own key of `object` onto `element`: an `on<event>` function attaches as a listener,
|
|
61
|
+
any other non-function value binds as a reactive attribute re-reading `source()[key]` per change.
|
|
62
|
+
A key in `skip` is left to its explicit binding. */
|
|
63
|
+
function bindKeys(
|
|
64
|
+
element: Element,
|
|
65
|
+
source: () => Record<string, unknown>,
|
|
66
|
+
object: Record<string, unknown>,
|
|
67
|
+
skip: Set<string>,
|
|
68
|
+
): void {
|
|
23
69
|
for (const key in object) {
|
|
24
70
|
if (skip.has(key)) {
|
|
25
71
|
continue
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { SuspenseSignal } from '../runtime/SuspenseSignal.ts'
|
|
1
2
|
import { mountSwappableRange } from './mountSwappableRange.ts'
|
|
2
3
|
import type { SwitchCase } from './types/SwitchCase.ts'
|
|
3
4
|
|
|
@@ -47,8 +48,24 @@ export function switchBlock(
|
|
|
47
48
|
parent,
|
|
48
49
|
/* A pending bare async subject (compiler-supplied `isPending`) selects no case — not
|
|
49
50
|
even the default — so the block renders nothing until the cell settles, rather than
|
|
50
|
-
conflating "still loading" with a subject that matched the default.
|
|
51
|
-
|
|
51
|
+
conflating "still loading" with a subject that matched the default. A blocking `await`
|
|
52
|
+
cell embedded in the subject or a `match` (a member access / compound the `isPending`
|
|
53
|
+
gate doesn't cover) throws a `SuspenseSignal` while pending (ADR-0042); select no case
|
|
54
|
+
until it settles, exactly as a bare async subject holds — the read tracked its cell, so
|
|
55
|
+
the swap effect re-runs on resolve. */
|
|
56
|
+
() => {
|
|
57
|
+
if (isPending?.() === true) {
|
|
58
|
+
return -1
|
|
59
|
+
}
|
|
60
|
+
try {
|
|
61
|
+
return select(subject())
|
|
62
|
+
} catch (signal) {
|
|
63
|
+
if (!(signal instanceof SuspenseSignal)) {
|
|
64
|
+
throw signal
|
|
65
|
+
}
|
|
66
|
+
return -1
|
|
67
|
+
}
|
|
68
|
+
},
|
|
52
69
|
(index) => {
|
|
53
70
|
const chosen = index === -1 ? undefined : cases[index]
|
|
54
71
|
return chosen && ((p) => chosen.render(p))
|
|
@@ -5,6 +5,7 @@ import { claimExpected } from '../runtime/claimExpected.ts'
|
|
|
5
5
|
import { OWNER } from '../runtime/OWNER.ts'
|
|
6
6
|
import { RENDER } from '../runtime/RENDER.ts'
|
|
7
7
|
import { scopeGroup } from '../runtime/scopeGroup.ts'
|
|
8
|
+
import { SuspenseSignal } from '../runtime/SuspenseSignal.ts'
|
|
8
9
|
import type { Boundary } from '../runtime/types/Boundary.ts'
|
|
9
10
|
import type { State } from '../runtime/types/State.ts'
|
|
10
11
|
import { withoutHydration } from '../runtime/withoutHydration.ts'
|
|
@@ -104,6 +105,15 @@ export function tryBlock(
|
|
|
104
105
|
activeKind = 'try'
|
|
105
106
|
errorCell = undefined
|
|
106
107
|
} catch (error) {
|
|
108
|
+
/* A `SuspenseSignal` is "value pending", not an error, and must NEVER render `{:catch}`
|
|
109
|
+
(ADR-0042 D3, mirroring `flushEffects`, which refuses to route it to a boundary) — that
|
|
110
|
+
would flash the author's catch during loading and, since `rewatch` only arms on an
|
|
111
|
+
`AsyncCellError`, stick there forever. Every reading region withholds its own suspend
|
|
112
|
+
locally (post-ADR-0042 the `{#await}`/`{#each await}` subjects included), so this is
|
|
113
|
+
unreachable in a compiled template; rethrow defensively rather than mislabel it. */
|
|
114
|
+
if (error instanceof SuspenseSignal) {
|
|
115
|
+
throw error
|
|
116
|
+
}
|
|
107
117
|
if (!hasCatch) {
|
|
108
118
|
throw error
|
|
109
119
|
}
|
package/src/lib/ui/dom/when.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { SuspenseSignal } from '../runtime/SuspenseSignal.ts'
|
|
1
2
|
import { mountSwappableRange } from './mountSwappableRange.ts'
|
|
2
3
|
|
|
3
4
|
/*
|
|
@@ -26,7 +27,24 @@ export function when(
|
|
|
26
27
|
): void {
|
|
27
28
|
mountSwappableRange(
|
|
28
29
|
parent,
|
|
29
|
-
() =>
|
|
30
|
+
() => {
|
|
31
|
+
if (isPending?.() === true) {
|
|
32
|
+
return 'pending'
|
|
33
|
+
}
|
|
34
|
+
/* A blocking `await` cell embedded in the condition — a member access or compound the
|
|
35
|
+
whole-subject `isPending` gate doesn't cover (`{#if !sources.length}`, `{#if user &&
|
|
36
|
+
await load()}`) — throws a `SuspenseSignal` while pending (ADR-0042). Withhold the
|
|
37
|
+
block (render neither branch) until it settles, exactly as a bare async subject holds;
|
|
38
|
+
the read tracked its cell, so the swap effect re-runs on resolve. */
|
|
39
|
+
try {
|
|
40
|
+
return condition() ? 'then' : 'else'
|
|
41
|
+
} catch (signal) {
|
|
42
|
+
if (!(signal instanceof SuspenseSignal)) {
|
|
43
|
+
throw signal
|
|
44
|
+
}
|
|
45
|
+
return 'pending'
|
|
46
|
+
}
|
|
47
|
+
},
|
|
30
48
|
(branch) => (branch === 'then' ? render : branch === 'else' ? renderElse : undefined),
|
|
31
49
|
before,
|
|
32
50
|
/* An async subject (compiler supplies `isPending`) can render a different branch on the
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { SuspenseSignal } from '../runtime/SuspenseSignal.ts'
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
Runs `read`, returning `fallback` when it SUSPENDS — a pending blocking `await` read throws a
|
|
5
|
+
`SuspenseSignal` (ADR-0042). The reading region shows `fallback` until the value resolves; the
|
|
6
|
+
throwing read subscribed the enclosing effect to the cell, so the effect re-runs and fills in on
|
|
7
|
+
settle. Any non-suspense error propagates unchanged (a real rejection still reaches `{#try}`). The
|
|
8
|
+
single place the `instanceof SuspenseSignal` swallow lives for a value-returning bind, so the read
|
|
9
|
+
sites (`each`, `watch`, …) share one definition rather than copying the try/catch. Text binds use
|
|
10
|
+
the sibling `readTextOrSuspend` (fallback `''` with String coercion); an attribute leaves itself
|
|
11
|
+
unset inline (a side effect, not a fallback value).
|
|
12
|
+
*/
|
|
13
|
+
export function withSuspense<T>(read: () => T, fallback: T): T {
|
|
14
|
+
try {
|
|
15
|
+
return read()
|
|
16
|
+
} catch (signal) {
|
|
17
|
+
if (!(signal instanceof SuspenseSignal)) {
|
|
18
|
+
throw signal
|
|
19
|
+
}
|
|
20
|
+
return fallback
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { AsyncComputed } from '../../shared/types/AsyncComputed.ts'
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
The sentinel a compiled blocking read (`$$readCellBlocking`, an `await`-marked cell) raises
|
|
5
|
+
when the cell has no value *yet* — "not resolved," not an error (ADR-0042 D3). It is a sibling
|
|
6
|
+
of `AsyncCellError` but a DISTINCT class so the two are told apart by `instanceof`: a suspend is
|
|
7
|
+
caught LOCALLY by the reading region — each DOM primitive (`appendText`, `attr`, `each`,
|
|
8
|
+
`spreadAttrs`, `watch`, and the `when`/`switchBlock` condition) swallows it and withholds to an
|
|
9
|
+
empty fallback, re-running on settle because the throwing read already subscribed the region's
|
|
10
|
+
effect to the cell. It must never be handled as an error: `flushEffects` refuses to route a
|
|
11
|
+
`SuspenseSignal` to a reactive `{#try}` (that would flash the author's `{:catch}` during loading),
|
|
12
|
+
while an `AsyncCellError` still passes through. It carries the originating cell for diagnostics.
|
|
13
|
+
Extends `Error` only to satisfy throw-an-Error lint and match `AsyncCellError`; it carries no
|
|
14
|
+
`cause` — there is nothing wrong, the value is simply pending.
|
|
15
|
+
*/
|
|
16
|
+
export class SuspenseSignal extends Error {
|
|
17
|
+
readonly cell: AsyncComputed<unknown>
|
|
18
|
+
|
|
19
|
+
constructor(cell: AsyncComputed<unknown>) {
|
|
20
|
+
super('abide: suspense (value pending)')
|
|
21
|
+
this.name = 'SuspenseSignal'
|
|
22
|
+
this.cell = cell
|
|
23
|
+
}
|
|
24
|
+
}
|