@barqjs/universal 0.0.0-stage → 0.2.0-next.1
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 +154 -2
- package/dist/index.d.ts +108 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +685 -0
- package/dist/index.js.map +1 -0
- package/dist/jsx-runtime.d.ts +47 -0
- package/dist/jsx-runtime.d.ts.map +1 -0
- package/dist/jsx-runtime.js +1 -0
- package/package.json +73 -4
- package/src/index.ts +23 -0
- package/src/jsx-runtime.ts +76 -0
- package/src/renderer.ts +1061 -0
package/src/renderer.ts
ADDED
|
@@ -0,0 +1,1061 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A renderer over any tree of nodes a host can create and arrange.
|
|
3
|
+
*
|
|
4
|
+
* `@barqjs/core` owns control flow and decides when a range of nodes changes;
|
|
5
|
+
* this file is the node layer under it, written against `HostOps` instead of
|
|
6
|
+
* the document: the `RangeHost` control flow writes through, the list reorder
|
|
7
|
+
* `each` hands it, and the entry points compiled code calls.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
type BoundaryKind,
|
|
12
|
+
type COUNT,
|
|
13
|
+
type JSXElement,
|
|
14
|
+
type Maybe,
|
|
15
|
+
type RevealOrder,
|
|
16
|
+
type Scope,
|
|
17
|
+
createComponent,
|
|
18
|
+
createEffect,
|
|
19
|
+
getOwner,
|
|
20
|
+
isBlock,
|
|
21
|
+
NotReadyError,
|
|
22
|
+
onCleanup,
|
|
23
|
+
ownRange,
|
|
24
|
+
readSlot,
|
|
25
|
+
rootScope,
|
|
26
|
+
runWithOwner,
|
|
27
|
+
untrack,
|
|
28
|
+
} from "@barqjs/core";
|
|
29
|
+
import type { Block, Cell } from "@barqjs/core";
|
|
30
|
+
import {
|
|
31
|
+
CONTEXT_MISS,
|
|
32
|
+
LIVE_EDGES,
|
|
33
|
+
LIVE_ROWS,
|
|
34
|
+
NOTHING,
|
|
35
|
+
activate,
|
|
36
|
+
boundaryIn,
|
|
37
|
+
branchIn,
|
|
38
|
+
cellSlot,
|
|
39
|
+
disposeScope,
|
|
40
|
+
eachIn,
|
|
41
|
+
effectUnder,
|
|
42
|
+
mountTree,
|
|
43
|
+
internalMemo,
|
|
44
|
+
isNodes,
|
|
45
|
+
islandIn,
|
|
46
|
+
jsxAsValue,
|
|
47
|
+
longestIncreasingSubsequence,
|
|
48
|
+
lookupContext,
|
|
49
|
+
nextActivation,
|
|
50
|
+
offerDefaultHost,
|
|
51
|
+
provideHost,
|
|
52
|
+
provideOn,
|
|
53
|
+
revealIn,
|
|
54
|
+
standingRow,
|
|
55
|
+
standingRows,
|
|
56
|
+
teardown,
|
|
57
|
+
underScope,
|
|
58
|
+
withoutClaim,
|
|
59
|
+
type RangeHost,
|
|
60
|
+
type Row,
|
|
61
|
+
type Site,
|
|
62
|
+
type SyncRows,
|
|
63
|
+
} from "@barqjs/core/internal";
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* What a host gives the renderer. `N` is the host's node; the renderer never
|
|
67
|
+
* looks inside one.
|
|
68
|
+
*
|
|
69
|
+
* `insertNode` MOVES a node that already has a parent, the way the document's
|
|
70
|
+
* `insertBefore` does: a reorder is an insert of a node that is already in
|
|
71
|
+
* the tree, and the renderer never removes it first.
|
|
72
|
+
*/
|
|
73
|
+
export interface HostOps<N extends object> {
|
|
74
|
+
createElement(tag: string): N;
|
|
75
|
+
createTextNode(value: string): N;
|
|
76
|
+
replaceText(node: N, value: string): void;
|
|
77
|
+
isTextNode(node: N): boolean;
|
|
78
|
+
/** `prev` is the value this name was last set to on this node, `undefined` the first time. */
|
|
79
|
+
setProperty(node: N, name: string, value: unknown, prev: unknown): void;
|
|
80
|
+
/** Put `node` under `parent` before `anchor`, or last when `anchor` is `null`. */
|
|
81
|
+
insertNode(parent: N, node: N, anchor: N | null): void;
|
|
82
|
+
removeNode(parent: N, node: N): void;
|
|
83
|
+
getParentNode(node: N): N | null | undefined;
|
|
84
|
+
getFirstChild(node: N): N | null | undefined;
|
|
85
|
+
getNextSibling(node: N): N | null | undefined;
|
|
86
|
+
/** A node that renders nothing, to anchor a range against. Defaults to an empty text node. */
|
|
87
|
+
createSentinel?(): N;
|
|
88
|
+
/**
|
|
89
|
+
* A detached node that holds children, for content built while it is not
|
|
90
|
+
* shown: a `<Loading>` boundary's body while its fallback stands, or a range
|
|
91
|
+
* built before anything placed it. Defaults to `createElement("#container")`.
|
|
92
|
+
*/
|
|
93
|
+
createContainer?(): N;
|
|
94
|
+
/**
|
|
95
|
+
* Hand a `ref` what it should hold for `node`, in place of the node itself.
|
|
96
|
+
* A host whose node stands for an object it builds later (once a property
|
|
97
|
+
* the object's constructor needs has arrived) calls `deliver` with that
|
|
98
|
+
* object when it exists and again each time it is replaced, and returns the
|
|
99
|
+
* function that stops delivering. Without it a ref is handed the node, once.
|
|
100
|
+
*/
|
|
101
|
+
attachRef?(node: N, deliver: (value: unknown) => void): () => void;
|
|
102
|
+
/**
|
|
103
|
+
* Told once a batch of inserts under `parent` is done: the nodes a list
|
|
104
|
+
* placed, a hole filled, a range moved. A host that keeps its own order of
|
|
105
|
+
* children cheaply by appending during the batch puts them in order here,
|
|
106
|
+
* so nothing reads them in the wrong order.
|
|
107
|
+
*/
|
|
108
|
+
placed?(parent: N): void;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** What `render` accepts besides the tree and the node it is mounted into. */
|
|
112
|
+
export interface RenderOptions {
|
|
113
|
+
/** Parents the root, so the tree reads that owner's contexts and is disposed with it. */
|
|
114
|
+
readonly owner?: Scope | null;
|
|
115
|
+
readonly renderId?: string;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export interface PortalProps<N extends object> {
|
|
119
|
+
/** The node the children mount into. Defaults to the node the tree was rendered into. */
|
|
120
|
+
mount?: N | null | (() => N | null | undefined);
|
|
121
|
+
children: unknown;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** What a `dynamic` source may answer with: a tag name or a function of one props object. */
|
|
125
|
+
export type DynamicTag = string | ((props: never) => unknown);
|
|
126
|
+
|
|
127
|
+
export type Spreadable = Record<string, unknown> | (() => Record<string, unknown>);
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* What `createRenderer` returns. Every member is a function of its arguments
|
|
131
|
+
* alone, so a renderer module destructures them into its exports.
|
|
132
|
+
*/
|
|
133
|
+
export interface Renderer<N extends object> {
|
|
134
|
+
/**
|
|
135
|
+
* Build `code` under a new root and put what it renders into `root`. The
|
|
136
|
+
* returned function disposes the root and removes what it placed.
|
|
137
|
+
*/
|
|
138
|
+
render: (code: (() => JSXElement) | JSXElement, root: N, options?: RenderOptions) => () => void;
|
|
139
|
+
createElement: (tag: string) => N;
|
|
140
|
+
createTextNode: (value: string) => N;
|
|
141
|
+
insertNode: (parent: N, node: N, anchor?: N | null) => void;
|
|
142
|
+
/** Writes one property and returns `value`, so a binding can keep it as its previous value. */
|
|
143
|
+
setProp: <T>(node: N, name: string, value: T, prev?: unknown) => T;
|
|
144
|
+
insert: (parent: N, value: unknown, anchor?: N | null) => void;
|
|
145
|
+
spread: (node: N, props: Spreadable, later?: readonly string[]) => void;
|
|
146
|
+
branch: <K>(
|
|
147
|
+
parent: N | null,
|
|
148
|
+
anchor: N | null,
|
|
149
|
+
key: Cell<K>,
|
|
150
|
+
bodies: Block<unknown> | readonly (Block<unknown> | null | undefined)[],
|
|
151
|
+
flags?: number,
|
|
152
|
+
) => N | N[] | null;
|
|
153
|
+
each: <T>(
|
|
154
|
+
parent: N | null,
|
|
155
|
+
anchor: N | null,
|
|
156
|
+
src: Cell<Maybe<readonly T[]>> | Cell<number>,
|
|
157
|
+
carrier: ((item: T) => unknown) | false | null | typeof COUNT | Cell<unknown>,
|
|
158
|
+
row: Block<unknown, never[]>,
|
|
159
|
+
flags?: number,
|
|
160
|
+
fallback?: Block<unknown> | null,
|
|
161
|
+
) => N | N[] | null;
|
|
162
|
+
boundary: (
|
|
163
|
+
parent: N | null,
|
|
164
|
+
anchor: N | null,
|
|
165
|
+
kind: BoundaryKind,
|
|
166
|
+
fallback: Block<unknown> | null | undefined,
|
|
167
|
+
body: Block<unknown>,
|
|
168
|
+
flags?: number,
|
|
169
|
+
on?: Cell<unknown>,
|
|
170
|
+
) => N | N[] | null;
|
|
171
|
+
island: (parent: N | null, anchor: N | null, block: Block<unknown>, flags?: number) => N | null;
|
|
172
|
+
reveal: (
|
|
173
|
+
order: Cell<RevealOrder | undefined>,
|
|
174
|
+
collapsed: Cell<boolean | undefined>,
|
|
175
|
+
block: Block<unknown>,
|
|
176
|
+
) => N[];
|
|
177
|
+
portal: (target: Cell<N | null | undefined>, block: Block<unknown>, flags?: number) => N;
|
|
178
|
+
/** Solid 2.0's factory for a component chosen at run time: a tag name or a component. */
|
|
179
|
+
dynamic: (
|
|
180
|
+
source: () => DynamicTag | null | undefined | false,
|
|
181
|
+
) => (props: Record<string, unknown>) => JSXElement;
|
|
182
|
+
Portal: (props: PortalProps<N>) => JSXElement;
|
|
183
|
+
/** The host control flow writes through, for a construct written by hand. */
|
|
184
|
+
host: RangeHost<N>;
|
|
185
|
+
/** The list reorder `each` hands control flow, over the same operations. */
|
|
186
|
+
syncRows: SyncRows<N>;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const NO_ARGS: readonly unknown[] = [];
|
|
190
|
+
|
|
191
|
+
/** The node a tree was rendered into, for a portal that names no target. */
|
|
192
|
+
const RENDER_ROOT: unique symbol = Symbol("render-root");
|
|
193
|
+
|
|
194
|
+
/** What a live hole resolved to: text it writes through, or the nodes it owns. */
|
|
195
|
+
type Resolved<N> = string | number | N[];
|
|
196
|
+
|
|
197
|
+
const UNRESOLVED: unique symbol = Symbol("barq-unresolved");
|
|
198
|
+
|
|
199
|
+
/** A child that is neither a node, a function, an array nor nothing: text. */
|
|
200
|
+
function asText(value: unknown): string {
|
|
201
|
+
if (typeof value === "string") return value;
|
|
202
|
+
if (typeof value === "number" || typeof value === "bigint") return String(value);
|
|
203
|
+
return typeof value === "symbol" ? value.toString() : "";
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export function createRenderer<N extends object>(ops: HostOps<N>): Renderer<N> {
|
|
207
|
+
const isTextNode = (node: N): boolean => ops.isTextNode(node);
|
|
208
|
+
const removeNode = (parent: N, node: N): void => ops.removeNode(parent, node);
|
|
209
|
+
const setProperty = (node: N, name: string, value: unknown, prev: unknown): void =>
|
|
210
|
+
ops.setProperty(node, name, value, prev);
|
|
211
|
+
const createSentinel =
|
|
212
|
+
ops.createSentinel === undefined ? () => createTextNode("") : () => ops.createSentinel!();
|
|
213
|
+
const createContainer =
|
|
214
|
+
ops.createContainer === undefined
|
|
215
|
+
? () => ops.createElement("#container")
|
|
216
|
+
: () => ops.createContainer!();
|
|
217
|
+
const attachRef =
|
|
218
|
+
ops.attachRef === undefined
|
|
219
|
+
? undefined
|
|
220
|
+
: (node: N, deliver: (value: unknown) => void) => ops.attachRef!(node, deliver);
|
|
221
|
+
const placed =
|
|
222
|
+
ops.placed === undefined ? (_parent: N): void => {} : (parent: N) => ops.placed!(parent);
|
|
223
|
+
|
|
224
|
+
// The text each text node was created with or last set to. The host has no
|
|
225
|
+
// operation to read it back, and a hole that rewrites the same string keeps
|
|
226
|
+
// its node only if the comparison can be made here.
|
|
227
|
+
const texts = new WeakMap<N, string>();
|
|
228
|
+
const parks = new WeakSet<N>();
|
|
229
|
+
// A container's children leave it when it is placed. Read twice, it answers
|
|
230
|
+
// with what it held, as a document fragment's drained content is kept.
|
|
231
|
+
const drained = new WeakMap<N, N[]>();
|
|
232
|
+
|
|
233
|
+
function createTextNode(value: string): N {
|
|
234
|
+
const node = ops.createTextNode(value);
|
|
235
|
+
texts.set(node, value);
|
|
236
|
+
return node;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function writeText(node: N, value: string): void {
|
|
240
|
+
if (texts.get(node) === value) return;
|
|
241
|
+
ops.replaceText(node, value);
|
|
242
|
+
texts.set(node, value);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function parentOf(node: N): N | null {
|
|
246
|
+
return ops.getParentNode(node) ?? null;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function nextOf(node: N): N | null {
|
|
250
|
+
return ops.getNextSibling(node) ?? null;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function firstOf(parent: N): N | null {
|
|
254
|
+
return ops.getFirstChild(parent) ?? null;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function insertNode(parent: N, node: N, anchor: N | null = null): void {
|
|
258
|
+
ops.insertNode(parent, node, anchor);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function detach(node: N): void {
|
|
262
|
+
const parent = parentOf(node);
|
|
263
|
+
if (parent !== null) removeNode(parent, node);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** `insertNode` that does nothing when the node is already there. */
|
|
267
|
+
function place(parent: N, node: N, before: N | null): void {
|
|
268
|
+
if (parentOf(node) === parent && nextOf(node) === before) return;
|
|
269
|
+
insertNode(parent, node, before);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function park(): N {
|
|
273
|
+
const node = createContainer();
|
|
274
|
+
parks.add(node);
|
|
275
|
+
return node;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function drain(container: N): N[] {
|
|
279
|
+
const first = firstOf(container);
|
|
280
|
+
if (first === null) return drained.get(container)?.slice() ?? [];
|
|
281
|
+
const nodes: N[] = [];
|
|
282
|
+
for (let node: N | null = first; node !== null; node = nextOf(node)) nodes.push(node);
|
|
283
|
+
for (const node of nodes) removeNode(container, node);
|
|
284
|
+
drained.set(container, nodes);
|
|
285
|
+
return nodes.slice();
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// ==========================================================================
|
|
289
|
+
// Children into nodes
|
|
290
|
+
// ==========================================================================
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* A Block is a construction and runs untracked. One that suspends is run
|
|
294
|
+
* again tracked: nothing was built and an untracked read registered nothing,
|
|
295
|
+
* so the position could never wake.
|
|
296
|
+
*/
|
|
297
|
+
function buildChild(value: (owner: unknown) => unknown, owner: Scope | null): unknown {
|
|
298
|
+
if (!isBlock(value)) return value(owner);
|
|
299
|
+
try {
|
|
300
|
+
return untrack(() => value(owner));
|
|
301
|
+
} catch (error) {
|
|
302
|
+
if (!(error instanceof NotReadyError)) throw error;
|
|
303
|
+
return value(owner);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/** What a Cell comes to once every Cell it yields has been read as well. */
|
|
308
|
+
function yielded(value: unknown, owner: Scope | null): unknown {
|
|
309
|
+
while (typeof value === "function" && !isBlock(value)) {
|
|
310
|
+
value = (value as (owner: unknown) => unknown)(owner);
|
|
311
|
+
}
|
|
312
|
+
return value;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function toNodes(child: unknown, scope: Scope | null): N[] {
|
|
316
|
+
if (child === null || child === undefined || typeof child === "boolean") return [];
|
|
317
|
+
if (typeof child === "function") {
|
|
318
|
+
if (!isBlock(child)) return liveHole(child as (owner: unknown) => unknown, scope);
|
|
319
|
+
return toNodes(buildChild(child as (owner: unknown) => unknown, scope), scope);
|
|
320
|
+
}
|
|
321
|
+
if (Array.isArray(child)) {
|
|
322
|
+
const nodes: N[] = [];
|
|
323
|
+
for (let i = 0; i < child.length; i++) {
|
|
324
|
+
const inner = toNodes(child[i], scope);
|
|
325
|
+
for (let j = 0; j < inner.length; j++) nodes.push(inner[j]);
|
|
326
|
+
}
|
|
327
|
+
return nodes;
|
|
328
|
+
}
|
|
329
|
+
if (typeof child === "object") {
|
|
330
|
+
return parks.has(child as N) ? drain(child as N) : [child as N];
|
|
331
|
+
}
|
|
332
|
+
return [createTextNode(asText(child))];
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Flatten a child to nodes for a hole that already holds `prev`, reusing a
|
|
337
|
+
* text node at the same position whose text is unchanged.
|
|
338
|
+
*/
|
|
339
|
+
function normalize(value: unknown, prev: readonly N[], scope: Scope | null): N[] {
|
|
340
|
+
const out: N[] = [];
|
|
341
|
+
const visit = (child: unknown): void => {
|
|
342
|
+
if (child === null || child === undefined || typeof child === "boolean") return;
|
|
343
|
+
if (typeof child === "function") {
|
|
344
|
+
if (isBlock(child)) {
|
|
345
|
+
visit(buildChild(child as (owner: unknown) => unknown, scope));
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
for (const node of liveHole(child as (owner: unknown) => unknown, scope)) out.push(node);
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
if (Array.isArray(child)) {
|
|
352
|
+
for (let i = 0; i < child.length; i++) visit(child[i]);
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
if (typeof child === "object") {
|
|
356
|
+
if (parks.has(child as N)) {
|
|
357
|
+
for (const node of drain(child as N)) out.push(node);
|
|
358
|
+
} else {
|
|
359
|
+
out.push(child as N);
|
|
360
|
+
}
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
const text = asText(child);
|
|
364
|
+
const candidate = prev[out.length];
|
|
365
|
+
if (candidate !== undefined && isTextNode(candidate) && texts.get(candidate) === text) {
|
|
366
|
+
out.push(candidate);
|
|
367
|
+
} else {
|
|
368
|
+
out.push(createTextNode(text));
|
|
369
|
+
}
|
|
370
|
+
};
|
|
371
|
+
visit(value);
|
|
372
|
+
return out;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* A Cell that is not the whole value of a hole: one entry of a children
|
|
377
|
+
* array, or a body with no element around it. It gets an effect and a text
|
|
378
|
+
* node of its own, so it updates where it stands without anything around it
|
|
379
|
+
* being asked again. The text node is the value when the value is text, and
|
|
380
|
+
* the edge its nodes are placed before when it is not.
|
|
381
|
+
*/
|
|
382
|
+
function liveHole(cell: (owner: unknown) => unknown, given: Scope | null): N[] {
|
|
383
|
+
let tail: N | null = null;
|
|
384
|
+
let current: N[] = [];
|
|
385
|
+
let initial = UNRESOLVED as Resolved<N> | typeof UNRESOLVED;
|
|
386
|
+
let written = false;
|
|
387
|
+
let standing = false;
|
|
388
|
+
|
|
389
|
+
const write = (resolved: Resolved<N>): void => {
|
|
390
|
+
if (typeof resolved !== "object") {
|
|
391
|
+
const text = String(resolved);
|
|
392
|
+
if (tail === null) {
|
|
393
|
+
tail = createTextNode(text);
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
if (current.length > 0) {
|
|
397
|
+
removeAll(current);
|
|
398
|
+
current = [];
|
|
399
|
+
}
|
|
400
|
+
writeText(tail, text);
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
if (tail === null) {
|
|
404
|
+
tail = createTextNode("");
|
|
405
|
+
current = resolved;
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
writeText(tail, "");
|
|
409
|
+
const parent = parentOf(tail);
|
|
410
|
+
current = parent === null ? resolved : placeInsert(parent, resolved, current, tail);
|
|
411
|
+
};
|
|
412
|
+
|
|
413
|
+
effectUnder<Resolved<N>>(
|
|
414
|
+
given,
|
|
415
|
+
"insert",
|
|
416
|
+
() => {
|
|
417
|
+
const owner = getOwner();
|
|
418
|
+
const resolved = resolve(yielded(cell(owner), owner), current, given);
|
|
419
|
+
if (!standing) initial = resolved;
|
|
420
|
+
return resolved;
|
|
421
|
+
},
|
|
422
|
+
(resolved) => {
|
|
423
|
+
if (!standing) {
|
|
424
|
+
written = true;
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
const first = resolved === initial;
|
|
428
|
+
initial = UNRESOLVED;
|
|
429
|
+
if (!first) write(resolved);
|
|
430
|
+
},
|
|
431
|
+
);
|
|
432
|
+
standing = true;
|
|
433
|
+
const took = initial;
|
|
434
|
+
if (written) initial = UNRESOLVED;
|
|
435
|
+
if (took !== UNRESOLVED) write(took);
|
|
436
|
+
const edge: N = tail ?? createTextNode("");
|
|
437
|
+
LIVE_EDGES.add(edge);
|
|
438
|
+
return current.length === 0 ? [edge] : [...current, edge];
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
function resolve(value: unknown, current: readonly N[], scope: Scope | null): Resolved<N> {
|
|
442
|
+
if (typeof value === "string" || typeof value === "number") return value;
|
|
443
|
+
return normalize(value, current, scope);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
// ==========================================================================
|
|
447
|
+
// Placing a hole's nodes
|
|
448
|
+
// ==========================================================================
|
|
449
|
+
|
|
450
|
+
// Last node first: a host keeping children in an array finds each one at
|
|
451
|
+
// its end and removes it without moving the rest.
|
|
452
|
+
function removeAll(nodes: readonly N[]): void {
|
|
453
|
+
for (let i = nodes.length - 1; i >= 0; i--) detach(nodes[i]);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* Put `value` into `parent` in place of `current`, before `marker`, and
|
|
458
|
+
* return what the hole holds now.
|
|
459
|
+
*/
|
|
460
|
+
function placeInsert(parent: N, value: Resolved<N>, current: N[], marker: N | null): N[] {
|
|
461
|
+
let next: N[];
|
|
462
|
+
if (typeof value === "string" || typeof value === "number") {
|
|
463
|
+
const text = String(value);
|
|
464
|
+
if (current.length === 1 && isTextNode(current[0])) {
|
|
465
|
+
writeText(current[0], text);
|
|
466
|
+
return current;
|
|
467
|
+
}
|
|
468
|
+
next = [createTextNode(text)];
|
|
469
|
+
} else {
|
|
470
|
+
next = value;
|
|
471
|
+
}
|
|
472
|
+
if (current.length === 0) {
|
|
473
|
+
for (let i = 0; i < next.length; i++) insertNode(parent, next[i], marker);
|
|
474
|
+
placed(parent);
|
|
475
|
+
return next;
|
|
476
|
+
}
|
|
477
|
+
if (next.length === 0) {
|
|
478
|
+
removeAll(current);
|
|
479
|
+
return [];
|
|
480
|
+
}
|
|
481
|
+
const into = parentOf(current[0]) ?? parent;
|
|
482
|
+
reconcile(into, current, next, marker);
|
|
483
|
+
placed(into);
|
|
484
|
+
return next;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* Reconcile two node arrays by identity: common prefix and suffix, the swap
|
|
489
|
+
* of two ends, then a map of what is left. Adapted from udomdiff, as the
|
|
490
|
+
* document renderer's is.
|
|
491
|
+
*/
|
|
492
|
+
function reconcile(parent: N, a: N[], b: N[], after: N | null): void {
|
|
493
|
+
const bLength = b.length;
|
|
494
|
+
let aEnd = a.length;
|
|
495
|
+
let bEnd = bLength;
|
|
496
|
+
let aStart = 0;
|
|
497
|
+
let bStart = 0;
|
|
498
|
+
let map: Map<N, number> | null = null;
|
|
499
|
+
|
|
500
|
+
while (aStart < aEnd || bStart < bEnd) {
|
|
501
|
+
if (a[aStart] === b[bStart]) {
|
|
502
|
+
aStart++;
|
|
503
|
+
bStart++;
|
|
504
|
+
continue;
|
|
505
|
+
}
|
|
506
|
+
while (a[aEnd - 1] === b[bEnd - 1]) {
|
|
507
|
+
aEnd--;
|
|
508
|
+
bEnd--;
|
|
509
|
+
}
|
|
510
|
+
if (aEnd === aStart) {
|
|
511
|
+
const anchor = bEnd < bLength ? (bStart ? nextOf(b[bStart - 1]) : b[bEnd - bStart]) : after;
|
|
512
|
+
while (bStart < bEnd) insertNode(parent, b[bStart++], anchor);
|
|
513
|
+
} else if (bEnd === bStart) {
|
|
514
|
+
while (aStart < aEnd) {
|
|
515
|
+
if (!map || !map.has(a[aStart])) detach(a[aStart]);
|
|
516
|
+
aStart++;
|
|
517
|
+
}
|
|
518
|
+
} else if (a[aStart] === b[bEnd - 1] && b[bStart] === a[aEnd - 1]) {
|
|
519
|
+
const node = nextOf(a[--aEnd]);
|
|
520
|
+
insertNode(parent, b[bStart++], nextOf(a[aStart++]));
|
|
521
|
+
insertNode(parent, b[--bEnd], node);
|
|
522
|
+
a[aEnd] = b[bEnd];
|
|
523
|
+
} else {
|
|
524
|
+
if (!map) {
|
|
525
|
+
map = new Map();
|
|
526
|
+
for (let i = bStart; i < bEnd; i++) map.set(b[i], i);
|
|
527
|
+
}
|
|
528
|
+
const index = map.get(a[aStart]);
|
|
529
|
+
if (index === undefined) {
|
|
530
|
+
detach(a[aStart++]);
|
|
531
|
+
} else if (index < bStart || index >= bEnd) {
|
|
532
|
+
aStart++;
|
|
533
|
+
} else {
|
|
534
|
+
let sequence = 1;
|
|
535
|
+
let t: number | undefined;
|
|
536
|
+
while (
|
|
537
|
+
aStart + sequence < aEnd &&
|
|
538
|
+
(t = map.get(a[aStart + sequence])) !== undefined &&
|
|
539
|
+
t === index + sequence
|
|
540
|
+
) {
|
|
541
|
+
sequence++;
|
|
542
|
+
}
|
|
543
|
+
if (sequence > index - bStart) {
|
|
544
|
+
const node = a[aStart];
|
|
545
|
+
while (bStart < index) insertNode(parent, b[bStart++], node);
|
|
546
|
+
} else {
|
|
547
|
+
const replaced = a[aStart++];
|
|
548
|
+
insertNode(parent, b[bStart++], replaced);
|
|
549
|
+
removeNode(parent, replaced);
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
// ==========================================================================
|
|
557
|
+
// The host control flow writes through
|
|
558
|
+
// ==========================================================================
|
|
559
|
+
|
|
560
|
+
/**
|
|
561
|
+
* What a list row of these nodes stands on. A row holding a live edge is
|
|
562
|
+
* given a node to start at, so it is read off the tree as it stands when it
|
|
563
|
+
* moves rather than as it was built.
|
|
564
|
+
*/
|
|
565
|
+
function asRow(nodes: N[]): N[] {
|
|
566
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
567
|
+
if (!LIVE_EDGES.has(nodes[i])) continue;
|
|
568
|
+
const row = [createSentinel(), ...nodes];
|
|
569
|
+
LIVE_ROWS.add(row);
|
|
570
|
+
return row;
|
|
571
|
+
}
|
|
572
|
+
return nodes;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
const host: RangeHost<N> = {
|
|
576
|
+
sentinel: createSentinel,
|
|
577
|
+
park,
|
|
578
|
+
isPark: (node) => parks.has(node),
|
|
579
|
+
parentOf,
|
|
580
|
+
nextOf,
|
|
581
|
+
prevOf(node) {
|
|
582
|
+
const parent = parentOf(node);
|
|
583
|
+
if (parent === null) return null;
|
|
584
|
+
let previous: N | null = null;
|
|
585
|
+
for (let at = firstOf(parent); at !== null && at !== node; at = nextOf(at)) previous = at;
|
|
586
|
+
return previous;
|
|
587
|
+
},
|
|
588
|
+
firstOf,
|
|
589
|
+
lastOf(parent) {
|
|
590
|
+
let last: N | null = null;
|
|
591
|
+
for (let at = firstOf(parent); at !== null; at = nextOf(at)) last = at;
|
|
592
|
+
return last;
|
|
593
|
+
},
|
|
594
|
+
row: asRow,
|
|
595
|
+
toNodes,
|
|
596
|
+
insertRange(parent, nodes, before) {
|
|
597
|
+
let anchor = before;
|
|
598
|
+
for (let i = nodes.length; i--;) {
|
|
599
|
+
place(parent, nodes[i], anchor);
|
|
600
|
+
anchor = nodes[i];
|
|
601
|
+
}
|
|
602
|
+
placed(parent);
|
|
603
|
+
},
|
|
604
|
+
removeRange: removeAll,
|
|
605
|
+
};
|
|
606
|
+
|
|
607
|
+
// ==========================================================================
|
|
608
|
+
// The list reorder
|
|
609
|
+
// ==========================================================================
|
|
610
|
+
|
|
611
|
+
function rowFirst(row: Row<N>): N | undefined {
|
|
612
|
+
return isNodes(row) ? row[0] : row;
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
function nodesOf(row: Row<N>): readonly N[] {
|
|
616
|
+
return isNodes(row) ? standingRow(host, row) : [row];
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
/**
|
|
620
|
+
* One list update. The rows both lists start and end with stay where they
|
|
621
|
+
* are, two ends that traded places are two moves, and what is left is
|
|
622
|
+
* placed against the longest run that already stands in order.
|
|
623
|
+
*/
|
|
624
|
+
const syncRows: SyncRows<N> = (parent, anchor, current, next) => {
|
|
625
|
+
const shared = Math.min(current.length, next.length);
|
|
626
|
+
let head = 0;
|
|
627
|
+
while (head < shared && current[head] === next[head]) head++;
|
|
628
|
+
let was = current.length;
|
|
629
|
+
let now = next.length;
|
|
630
|
+
while (was > head && now > head && current[was - 1] === next[now - 1]) {
|
|
631
|
+
was--;
|
|
632
|
+
now--;
|
|
633
|
+
}
|
|
634
|
+
while (
|
|
635
|
+
was - head > 1 &&
|
|
636
|
+
now - head > 1 &&
|
|
637
|
+
current[head] === next[now - 1] &&
|
|
638
|
+
next[head] === current[was - 1] &&
|
|
639
|
+
swapEnds(parent, current[head], current[was - 1])
|
|
640
|
+
) {
|
|
641
|
+
head++;
|
|
642
|
+
was--;
|
|
643
|
+
now--;
|
|
644
|
+
while (head < was && head < now && current[head] === next[head]) head++;
|
|
645
|
+
while (was > head && now > head && current[was - 1] === next[now - 1]) {
|
|
646
|
+
was--;
|
|
647
|
+
now--;
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
const leaving = head === 0 && was === current.length ? current : current.slice(head, was);
|
|
651
|
+
const arriving = head === 0 && now === next.length ? next : next.slice(head, now);
|
|
652
|
+
|
|
653
|
+
if (leaving.length > 0) {
|
|
654
|
+
if (arriving.length === 0) {
|
|
655
|
+
removeAll(standingRows(host, leaving));
|
|
656
|
+
} else {
|
|
657
|
+
const kept = new Set(arriving);
|
|
658
|
+
for (const row of leaving) {
|
|
659
|
+
if (!kept.has(row)) removeAll(nodesOf(row));
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
if (arriving.length > 0) {
|
|
665
|
+
let after = anchor;
|
|
666
|
+
for (let i = now; i < next.length; i++) {
|
|
667
|
+
const lead = rowFirst(next[i]);
|
|
668
|
+
if (lead === undefined) continue;
|
|
669
|
+
after = lead;
|
|
670
|
+
break;
|
|
671
|
+
}
|
|
672
|
+
orderRows(parent, after, leaving, arriving);
|
|
673
|
+
}
|
|
674
|
+
placed(parent);
|
|
675
|
+
};
|
|
676
|
+
|
|
677
|
+
function swapEnds(parent: N, low: Row<N>, high: Row<N>): boolean {
|
|
678
|
+
const lowNodes = nodesOf(low);
|
|
679
|
+
const highNodes = nodesOf(high);
|
|
680
|
+
if (lowNodes.length === 0 || highNodes.length === 0) return false;
|
|
681
|
+
const afterHigh = nextOf(highNodes[highNodes.length - 1]);
|
|
682
|
+
const lowFirst = lowNodes[0];
|
|
683
|
+
for (let i = 0; i < highNodes.length; i++) insertNode(parent, highNodes[i], lowFirst);
|
|
684
|
+
for (let i = 0; i < lowNodes.length; i++) insertNode(parent, lowNodes[i], afterHigh);
|
|
685
|
+
return true;
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
function orderRows(
|
|
689
|
+
parent: N,
|
|
690
|
+
anchor: N | null,
|
|
691
|
+
previous: readonly Row<N>[],
|
|
692
|
+
next: readonly Row<N>[],
|
|
693
|
+
): void {
|
|
694
|
+
if (previous.length === 0) {
|
|
695
|
+
let before = anchor;
|
|
696
|
+
for (let i = next.length - 1; i >= 0; i--) {
|
|
697
|
+
const row = nodesOf(next[i]);
|
|
698
|
+
for (let j = row.length - 1; j >= 0; j--) {
|
|
699
|
+
place(parent, row[j], before);
|
|
700
|
+
before = row[j];
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
return;
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
const previousIndex = new Map<Row<N>, number>();
|
|
707
|
+
for (let i = 0; i < previous.length; i++) previousIndex.set(previous[i], i);
|
|
708
|
+
const sources: number[] = new Array(next.length);
|
|
709
|
+
for (let i = 0; i < next.length; i++) sources[i] = previousIndex.get(next[i]) ?? -1;
|
|
710
|
+
const lis = longestIncreasingSubsequence(sources.filter((source) => source !== -1));
|
|
711
|
+
|
|
712
|
+
let lisIndex = lis.length - 1;
|
|
713
|
+
let before: N | null = anchor;
|
|
714
|
+
for (let i = next.length - 1; i >= 0; i--) {
|
|
715
|
+
const row = next[i];
|
|
716
|
+
const first = rowFirst(row);
|
|
717
|
+
if (first === undefined) continue;
|
|
718
|
+
if (sources[i] !== -1 && lisIndex >= 0 && lis[lisIndex] === sources[i]) {
|
|
719
|
+
lisIndex--;
|
|
720
|
+
} else {
|
|
721
|
+
// First to last, each before the same node, keeps a row of several
|
|
722
|
+
// nodes in its own order.
|
|
723
|
+
const nodes = nodesOf(row);
|
|
724
|
+
for (let j = 0; j < nodes.length; j++) insertNode(parent, nodes[j], before);
|
|
725
|
+
}
|
|
726
|
+
before = first;
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
// ==========================================================================
|
|
731
|
+
// What compiled code calls
|
|
732
|
+
// ==========================================================================
|
|
733
|
+
|
|
734
|
+
function insert(parent: N, value: unknown, marker?: N | null): void {
|
|
735
|
+
const given = getOwner();
|
|
736
|
+
const anchor = marker ?? null;
|
|
737
|
+
if (typeof value === "function" && isBlock(value)) {
|
|
738
|
+
value = buildChild(value as (owner: unknown) => unknown, given);
|
|
739
|
+
if (typeof value === "function") {
|
|
740
|
+
insert(parent, value, marker);
|
|
741
|
+
return;
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
if (typeof value === "function") {
|
|
745
|
+
insertLive(parent, value as (owner: unknown) => unknown, anchor, given);
|
|
746
|
+
return;
|
|
747
|
+
}
|
|
748
|
+
if (value === null || value === undefined || typeof value === "boolean") return;
|
|
749
|
+
const nodes = toNodes(value, given);
|
|
750
|
+
for (let i = 0; i < nodes.length; i++) insertNode(parent, nodes[i], anchor);
|
|
751
|
+
placed(parent);
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
function insertLive(
|
|
755
|
+
parent: N,
|
|
756
|
+
value: (owner: unknown) => unknown,
|
|
757
|
+
anchor: N | null,
|
|
758
|
+
given: Scope | null,
|
|
759
|
+
): void {
|
|
760
|
+
let current: N[] = [];
|
|
761
|
+
effectUnder<Resolved<N>>(
|
|
762
|
+
given,
|
|
763
|
+
"insert",
|
|
764
|
+
() => {
|
|
765
|
+
const owner = getOwner();
|
|
766
|
+
return resolve(yielded(buildChild(value, owner), owner), current, given);
|
|
767
|
+
},
|
|
768
|
+
(resolved) => {
|
|
769
|
+
current = placeInsert(parent, resolved, current, anchor);
|
|
770
|
+
},
|
|
771
|
+
);
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
function setProp<T>(node: N, name: string, value: T, prev?: unknown): T {
|
|
775
|
+
if (name === "ref") {
|
|
776
|
+
ref(node, value);
|
|
777
|
+
return value;
|
|
778
|
+
}
|
|
779
|
+
setProperty(node, name, value, prev);
|
|
780
|
+
return value;
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
function refuseBlock(target: unknown, origin: string, returned = false): void {
|
|
784
|
+
if (isBlock(target)) throw jsxAsValue(origin, returned);
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
/**
|
|
788
|
+
* A ref registration owned by the scope the node belongs to: a callback that
|
|
789
|
+
* returns a function has it run when that scope goes.
|
|
790
|
+
*/
|
|
791
|
+
function ref(node: N, value: unknown): void {
|
|
792
|
+
const owner = getOwner();
|
|
793
|
+
const undo: (() => void)[] = [];
|
|
794
|
+
const one = (target: unknown): void => {
|
|
795
|
+
refuseBlock(target, "A `ref`");
|
|
796
|
+
if (typeof target === "function") {
|
|
797
|
+
if (attachRef === undefined) {
|
|
798
|
+
const back: unknown = target(node);
|
|
799
|
+
refuseBlock(back, "A `ref`", true);
|
|
800
|
+
if (typeof back === "function") undo.push(back as () => void);
|
|
801
|
+
return;
|
|
802
|
+
}
|
|
803
|
+
let release: (() => void) | undefined;
|
|
804
|
+
const stop = attachRef(node, (held) => {
|
|
805
|
+
release?.();
|
|
806
|
+
const back: unknown = target(held);
|
|
807
|
+
refuseBlock(back, "A `ref`", true);
|
|
808
|
+
release = typeof back === "function" ? (back as () => void) : undefined;
|
|
809
|
+
});
|
|
810
|
+
undo.push(() => {
|
|
811
|
+
stop();
|
|
812
|
+
release?.();
|
|
813
|
+
});
|
|
814
|
+
} else if (typeof target === "object" && target !== null && "current" in target) {
|
|
815
|
+
if (attachRef === undefined) {
|
|
816
|
+
target.current = node;
|
|
817
|
+
return;
|
|
818
|
+
}
|
|
819
|
+
undo.push(
|
|
820
|
+
attachRef(node, (held) => {
|
|
821
|
+
target.current = held;
|
|
822
|
+
}),
|
|
823
|
+
);
|
|
824
|
+
}
|
|
825
|
+
};
|
|
826
|
+
if (Array.isArray(value)) {
|
|
827
|
+
for (const target of value) one(target);
|
|
828
|
+
} else {
|
|
829
|
+
one(value);
|
|
830
|
+
}
|
|
831
|
+
if (undo.length === 0 || owner === null) return;
|
|
832
|
+
underScope(owner, "ref", () => {
|
|
833
|
+
onCleanup(() => {
|
|
834
|
+
for (const fn of undo) fn();
|
|
835
|
+
});
|
|
836
|
+
});
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
const isHandlerKey = (key: string): boolean =>
|
|
840
|
+
key.charCodeAt(0) === 111 && key.charCodeAt(1) === 110;
|
|
841
|
+
|
|
842
|
+
/**
|
|
843
|
+
* A props object onto a node, diffed against what was last applied, with a
|
|
844
|
+
* property that vanished set to `undefined`. A getter is read in the
|
|
845
|
+
* tracked half, so the write half sees values only; a handler and a `ref`
|
|
846
|
+
* are functions that are not getters and pass through as they are.
|
|
847
|
+
* `children` is not a property: the element's own children are placed by
|
|
848
|
+
* whoever built it.
|
|
849
|
+
*/
|
|
850
|
+
function spread(node: N, props: Spreadable, later?: readonly string[]): void {
|
|
851
|
+
const given = getOwner();
|
|
852
|
+
const applied: Record<string, unknown> = {};
|
|
853
|
+
let first = true;
|
|
854
|
+
|
|
855
|
+
const read = (): Record<string, unknown> => {
|
|
856
|
+
const next = typeof props === "function" ? props() : props;
|
|
857
|
+
const resolved: Record<string, unknown> = {};
|
|
858
|
+
for (const key in next) {
|
|
859
|
+
if (key === "children") continue;
|
|
860
|
+
if (later !== undefined && later.includes(key)) continue;
|
|
861
|
+
const value = next[key];
|
|
862
|
+
if (key === "ref" || isHandlerKey(key)) {
|
|
863
|
+
resolved[key] = value;
|
|
864
|
+
continue;
|
|
865
|
+
}
|
|
866
|
+
refuseBlock(value, `The \`${key}\` prop in a spread`);
|
|
867
|
+
resolved[key] = typeof value === "function" ? (value as () => unknown)() : value;
|
|
868
|
+
}
|
|
869
|
+
return resolved;
|
|
870
|
+
};
|
|
871
|
+
|
|
872
|
+
effectUnder<Record<string, unknown>>(given, "spread", read, (next) => {
|
|
873
|
+
for (const key in applied) {
|
|
874
|
+
if (key in next) continue;
|
|
875
|
+
setProperty(node, key, undefined, applied[key]);
|
|
876
|
+
delete applied[key];
|
|
877
|
+
}
|
|
878
|
+
for (const key in next) {
|
|
879
|
+
const value = next[key];
|
|
880
|
+
if (key === "ref") {
|
|
881
|
+
if (first) runWithOwner(given, () => ref(node, value));
|
|
882
|
+
continue;
|
|
883
|
+
}
|
|
884
|
+
if (key in applied && applied[key] === value) continue;
|
|
885
|
+
setProperty(node, key, value, applied[key]);
|
|
886
|
+
applied[key] = value;
|
|
887
|
+
}
|
|
888
|
+
first = false;
|
|
889
|
+
});
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
/** One element by tag name, its props spread and its children inserted. */
|
|
893
|
+
function element(tag: string, props: Record<string, unknown>): N {
|
|
894
|
+
const node = ops.createElement(tag);
|
|
895
|
+
spread(node, props);
|
|
896
|
+
const children = props.children;
|
|
897
|
+
if (children !== undefined && children !== null) insert(node, children);
|
|
898
|
+
return node;
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
// ==========================================================================
|
|
902
|
+
// Components that need a host
|
|
903
|
+
// ==========================================================================
|
|
904
|
+
|
|
905
|
+
function portal(target: Cell<N | null | undefined>, block: Block<unknown>): N {
|
|
906
|
+
const given = getOwner();
|
|
907
|
+
cellSlot(target, "Portal.mount");
|
|
908
|
+
cellSlot(untrack(target), "Portal.mount", true);
|
|
909
|
+
const marker = createSentinel();
|
|
910
|
+
underScope(given, "portal", () => {
|
|
911
|
+
let instance = NOTHING;
|
|
912
|
+
let request = 0;
|
|
913
|
+
const site: Site = { host, parent: null, anchor: null };
|
|
914
|
+
const clear = (): void => {
|
|
915
|
+
if (instance !== NOTHING) {
|
|
916
|
+
teardown(host, instance);
|
|
917
|
+
instance = NOTHING;
|
|
918
|
+
}
|
|
919
|
+
site.parent = null;
|
|
920
|
+
};
|
|
921
|
+
// An effect, so the mount happens once the pass that built the marker
|
|
922
|
+
// has committed, and is held with the rest of a boundary's content while
|
|
923
|
+
// the boundary shows its fallback.
|
|
924
|
+
createEffect(
|
|
925
|
+
() => {
|
|
926
|
+
const requested = target();
|
|
927
|
+
cellSlot(requested, "Portal.mount", true);
|
|
928
|
+
return requested;
|
|
929
|
+
},
|
|
930
|
+
(requested) => {
|
|
931
|
+
clear();
|
|
932
|
+
const mine = ++request;
|
|
933
|
+
const mount = (): void => {
|
|
934
|
+
if (mine !== request) return;
|
|
935
|
+
if (requested !== null && requested !== undefined && typeof requested !== "object") {
|
|
936
|
+
throw new TypeError(
|
|
937
|
+
`\`<Portal mount>\` was given ${JSON.stringify(requested)}, and on a custom host it ` +
|
|
938
|
+
"takes a node of that host. A selector is something only the document can look " +
|
|
939
|
+
"up: pass the node itself, or leave `mount` out to mount into the render root.",
|
|
940
|
+
);
|
|
941
|
+
}
|
|
942
|
+
const into = requested ?? renderRootOf(given);
|
|
943
|
+
if (into === null) return;
|
|
944
|
+
site.parent = into;
|
|
945
|
+
nextActivation();
|
|
946
|
+
instance = activate(given, site, block, NO_ARGS, 0, "portal");
|
|
947
|
+
};
|
|
948
|
+
// A portal nothing owns has no scope to build under, and the engine
|
|
949
|
+
// refuses one made from inside an effect's write.
|
|
950
|
+
if (given === null) queueMicrotask(mount);
|
|
951
|
+
else mount();
|
|
952
|
+
},
|
|
953
|
+
);
|
|
954
|
+
onCleanup(clear);
|
|
955
|
+
});
|
|
956
|
+
return marker;
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
function renderRootOf(owner: Scope | null): N | null {
|
|
960
|
+
const found = lookupContext(owner, RENDER_ROOT);
|
|
961
|
+
return found === CONTEXT_MISS ? null : (found as N);
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
function Portal(props: PortalProps<N>): JSXElement {
|
|
965
|
+
return portal(
|
|
966
|
+
() => readSlot(props.mount, "Portal.mount") as N | null | undefined,
|
|
967
|
+
props.children as Block<unknown>,
|
|
968
|
+
);
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
function dynamic(
|
|
972
|
+
source: () => DynamicTag | null | undefined | false,
|
|
973
|
+
): (props: Record<string, unknown>) => JSXElement {
|
|
974
|
+
return (props) => {
|
|
975
|
+
const component = internalMemo(source);
|
|
976
|
+
return branch(null, null, component, () => {
|
|
977
|
+
const resolved = untrack(component);
|
|
978
|
+
if (resolved === null || resolved === undefined || resolved === false) return null;
|
|
979
|
+
if (typeof resolved === "string") return element(resolved, props);
|
|
980
|
+
// A component chosen at run time is called as a compiled tag calls one.
|
|
981
|
+
return createComponent(resolved as (props: Record<string, unknown>) => unknown, props);
|
|
982
|
+
});
|
|
983
|
+
};
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
// ==========================================================================
|
|
987
|
+
// Control flow over this host
|
|
988
|
+
// ==========================================================================
|
|
989
|
+
|
|
990
|
+
function branch<K>(
|
|
991
|
+
parent: N | null,
|
|
992
|
+
anchor: N | null,
|
|
993
|
+
key: Cell<K>,
|
|
994
|
+
bodies: Block<unknown> | readonly (Block<unknown> | null | undefined)[],
|
|
995
|
+
flags?: number,
|
|
996
|
+
): N | N[] | null {
|
|
997
|
+
return branchIn(host, parent, anchor, key, bodies, flags);
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
// ==========================================================================
|
|
1001
|
+
// Mounting
|
|
1002
|
+
// ==========================================================================
|
|
1003
|
+
|
|
1004
|
+
function render(
|
|
1005
|
+
code: (() => JSXElement) | JSXElement,
|
|
1006
|
+
root: N,
|
|
1007
|
+
options?: RenderOptions,
|
|
1008
|
+
): () => void {
|
|
1009
|
+
const scope = rootScope(options?.owner ?? null, options?.renderId);
|
|
1010
|
+
// Provided on the root, so a component called inside this tree renders
|
|
1011
|
+
// through this host whatever the default is: an app that renders into the
|
|
1012
|
+
// document as well has two hosts, and only the root knows which is whose.
|
|
1013
|
+
// A document renderer's control flow reads it too, for a component that
|
|
1014
|
+
// compiled for the document and is rendered here.
|
|
1015
|
+
provideHost(scope, { host, sync: syncRows });
|
|
1016
|
+
provideOn(scope, RENDER_ROOT, root);
|
|
1017
|
+
ownRange(scope, () => {
|
|
1018
|
+
for (let child = firstOf(root); child !== null; child = firstOf(root)) {
|
|
1019
|
+
removeNode(root, child);
|
|
1020
|
+
}
|
|
1021
|
+
});
|
|
1022
|
+
// A host has no server markup, so a tree mounted while a page hydrates,
|
|
1023
|
+
// as a canvas component mounting its scene does, claims nothing.
|
|
1024
|
+
withoutClaim(() => {
|
|
1025
|
+
mountTree(() => {
|
|
1026
|
+
runWithOwner(scope, () => {
|
|
1027
|
+
insert(root, typeof code === "function" ? code() : code);
|
|
1028
|
+
});
|
|
1029
|
+
});
|
|
1030
|
+
});
|
|
1031
|
+
return () => {
|
|
1032
|
+
disposeScope(scope);
|
|
1033
|
+
};
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
// A component called outside any tree this renderer mounted finds a host
|
|
1037
|
+
// through the default, which this fills only when nothing else has.
|
|
1038
|
+
offerDefaultHost(host, syncRows);
|
|
1039
|
+
|
|
1040
|
+
return {
|
|
1041
|
+
render,
|
|
1042
|
+
createElement: (tag) => ops.createElement(tag),
|
|
1043
|
+
createTextNode,
|
|
1044
|
+
insertNode,
|
|
1045
|
+
setProp,
|
|
1046
|
+
insert,
|
|
1047
|
+
spread,
|
|
1048
|
+
branch,
|
|
1049
|
+
each: (parent, anchor, src, carrier, row, flags, fallback) =>
|
|
1050
|
+
eachIn(host, syncRows, parent, anchor, src, carrier, row, flags, fallback),
|
|
1051
|
+
boundary: (parent, anchor, kind, fallback, body, flags, on) =>
|
|
1052
|
+
boundaryIn(host, parent, anchor, kind, fallback, body, flags, on),
|
|
1053
|
+
island: (parent, anchor, block, flags) => islandIn(host, parent, anchor, block, flags),
|
|
1054
|
+
reveal: (order, collapsed, block) => revealIn(host, order, collapsed, block),
|
|
1055
|
+
portal,
|
|
1056
|
+
dynamic,
|
|
1057
|
+
Portal,
|
|
1058
|
+
host,
|
|
1059
|
+
syncRows,
|
|
1060
|
+
};
|
|
1061
|
+
}
|