@barqjs/universal 0.0.0-stage → 0.2.0-next.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/dist/index.js ADDED
@@ -0,0 +1,686 @@
1
+ import { NotReadyError, createEffect, getOwner, isBlock, onCleanup, ownRange, readSlot, rootScope, runWithOwner, untrack } from "@barqjs/core";
2
+ import { CONTEXT_MISS, LIVE_EDGES, LIVE_ROWS, NOTHING, activate, boundaryIn, branchIn, cellSlot, disposeScope, eachIn, effectUnder, internalMemo, isNodes, islandIn, jsxAsValue, longestIncreasingSubsequence, lookupContext, mountTree, nextActivation, offerDefaultHost, provideHost, provideOn, revealIn, standingRow, standingRows, teardown, underScope, withoutClaim } from "@barqjs/core/internal";
3
+ //#region src/renderer.ts
4
+ /**
5
+ * A renderer over any tree of nodes a host can create and arrange.
6
+ *
7
+ * `@barqjs/core` owns control flow and decides when a range of nodes changes;
8
+ * this file is the node layer under it, written against `HostOps` instead of
9
+ * the document: the `RangeHost` control flow writes through, the list reorder
10
+ * `each` hands it, and the entry points compiled code calls.
11
+ */
12
+ const NO_ARGS = [];
13
+ /** The node a tree was rendered into, for a portal that names no target. */
14
+ const RENDER_ROOT = Symbol("render-root");
15
+ const UNRESOLVED = Symbol("barq-unresolved");
16
+ /** A child that is neither a node, a function, an array nor nothing: text. */
17
+ function asText(value) {
18
+ if (typeof value === "string") return value;
19
+ if (typeof value === "number" || typeof value === "bigint") return String(value);
20
+ return typeof value === "symbol" ? value.toString() : "";
21
+ }
22
+ function createRenderer(ops) {
23
+ const isTextNode = (node) => ops.isTextNode(node);
24
+ const removeNode = (parent, node) => ops.removeNode(parent, node);
25
+ const setProperty = (node, name, value, prev) => ops.setProperty(node, name, value, prev);
26
+ const createSentinel = ops.createSentinel === void 0 ? () => createTextNode("") : () => ops.createSentinel();
27
+ const createContainer = ops.createContainer === void 0 ? () => ops.createElement("#container") : () => ops.createContainer();
28
+ const attachRef = ops.attachRef === void 0 ? void 0 : (node, deliver) => ops.attachRef(node, deliver);
29
+ const placed = ops.placed === void 0 ? (_parent) => {} : (parent) => ops.placed(parent);
30
+ const texts = /* @__PURE__ */ new WeakMap();
31
+ const parks = /* @__PURE__ */ new WeakSet();
32
+ const drained = /* @__PURE__ */ new WeakMap();
33
+ function createTextNode(value) {
34
+ const node = ops.createTextNode(value);
35
+ texts.set(node, value);
36
+ return node;
37
+ }
38
+ function writeText(node, value) {
39
+ if (texts.get(node) === value) return;
40
+ ops.replaceText(node, value);
41
+ texts.set(node, value);
42
+ }
43
+ function parentOf(node) {
44
+ return ops.getParentNode(node) ?? null;
45
+ }
46
+ function nextOf(node) {
47
+ return ops.getNextSibling(node) ?? null;
48
+ }
49
+ function firstOf(parent) {
50
+ return ops.getFirstChild(parent) ?? null;
51
+ }
52
+ function insertNode(parent, node, anchor = null) {
53
+ ops.insertNode(parent, node, anchor);
54
+ }
55
+ function detach(node) {
56
+ const parent = parentOf(node);
57
+ if (parent !== null) removeNode(parent, node);
58
+ }
59
+ /** `insertNode` that does nothing when the node is already there. */
60
+ function place(parent, node, before) {
61
+ if (parentOf(node) === parent && nextOf(node) === before) return;
62
+ insertNode(parent, node, before);
63
+ }
64
+ function park() {
65
+ const node = createContainer();
66
+ parks.add(node);
67
+ return node;
68
+ }
69
+ function drain(container) {
70
+ const first = firstOf(container);
71
+ if (first === null) return drained.get(container)?.slice() ?? [];
72
+ const nodes = [];
73
+ for (let node = first; node !== null; node = nextOf(node)) nodes.push(node);
74
+ for (const node of nodes) removeNode(container, node);
75
+ drained.set(container, nodes);
76
+ return nodes.slice();
77
+ }
78
+ /**
79
+ * A Block is a construction and runs untracked. One that suspends is run
80
+ * again tracked: nothing was built and an untracked read registered nothing,
81
+ * so the position could never wake.
82
+ */
83
+ function buildChild(value, owner) {
84
+ if (!isBlock(value)) return value(owner);
85
+ try {
86
+ return untrack(() => value(owner));
87
+ } catch (error) {
88
+ if (!(error instanceof NotReadyError)) throw error;
89
+ return value(owner);
90
+ }
91
+ }
92
+ /** What a Cell comes to once every Cell it yields has been read as well. */
93
+ function yielded(value, owner) {
94
+ while (typeof value === "function" && !isBlock(value)) value = value(owner);
95
+ return value;
96
+ }
97
+ function toNodes(child, scope) {
98
+ if (child === null || child === void 0 || typeof child === "boolean") return [];
99
+ if (typeof child === "function") {
100
+ if (!isBlock(child)) return liveHole(child, scope);
101
+ return toNodes(buildChild(child, scope), scope);
102
+ }
103
+ if (Array.isArray(child)) {
104
+ const nodes = [];
105
+ for (let i = 0; i < child.length; i++) {
106
+ const inner = toNodes(child[i], scope);
107
+ for (let j = 0; j < inner.length; j++) nodes.push(inner[j]);
108
+ }
109
+ return nodes;
110
+ }
111
+ if (typeof child === "object") return parks.has(child) ? drain(child) : [child];
112
+ return [createTextNode(asText(child))];
113
+ }
114
+ /**
115
+ * Flatten a child to nodes for a hole that already holds `prev`, reusing a
116
+ * text node at the same position whose text is unchanged.
117
+ */
118
+ function normalize(value, prev, scope) {
119
+ const out = [];
120
+ const visit = (child) => {
121
+ if (child === null || child === void 0 || typeof child === "boolean") return;
122
+ if (typeof child === "function") {
123
+ if (isBlock(child)) {
124
+ visit(buildChild(child, scope));
125
+ return;
126
+ }
127
+ for (const node of liveHole(child, scope)) out.push(node);
128
+ return;
129
+ }
130
+ if (Array.isArray(child)) {
131
+ for (let i = 0; i < child.length; i++) visit(child[i]);
132
+ return;
133
+ }
134
+ if (typeof child === "object") {
135
+ if (parks.has(child)) for (const node of drain(child)) out.push(node);
136
+ else out.push(child);
137
+ return;
138
+ }
139
+ const text = asText(child);
140
+ const candidate = prev[out.length];
141
+ if (candidate !== void 0 && isTextNode(candidate) && texts.get(candidate) === text) out.push(candidate);
142
+ else out.push(createTextNode(text));
143
+ };
144
+ visit(value);
145
+ return out;
146
+ }
147
+ /**
148
+ * A Cell that is not the whole value of a hole: one entry of a children
149
+ * array, or a body with no element around it. It gets an effect and a text
150
+ * node of its own, so it updates where it stands without anything around it
151
+ * being asked again. The text node is the value when the value is text, and
152
+ * the edge its nodes are placed before when it is not.
153
+ */
154
+ function liveHole(cell, given) {
155
+ let tail = null;
156
+ let current = [];
157
+ let initial = UNRESOLVED;
158
+ let written = false;
159
+ let standing = false;
160
+ const write = (resolved) => {
161
+ if (typeof resolved !== "object") {
162
+ const text = String(resolved);
163
+ if (tail === null) {
164
+ tail = createTextNode(text);
165
+ return;
166
+ }
167
+ if (current.length > 0) {
168
+ removeAll(current);
169
+ current = [];
170
+ }
171
+ writeText(tail, text);
172
+ return;
173
+ }
174
+ if (tail === null) {
175
+ tail = createTextNode("");
176
+ current = resolved;
177
+ return;
178
+ }
179
+ writeText(tail, "");
180
+ const parent = parentOf(tail);
181
+ current = parent === null ? resolved : placeInsert(parent, resolved, current, tail);
182
+ };
183
+ effectUnder(given, "insert", () => {
184
+ const owner = getOwner();
185
+ const resolved = resolve(yielded(cell(owner), owner), current, given);
186
+ if (!standing) initial = resolved;
187
+ return resolved;
188
+ }, (resolved) => {
189
+ if (!standing) {
190
+ written = true;
191
+ return;
192
+ }
193
+ const first = resolved === initial;
194
+ initial = UNRESOLVED;
195
+ if (!first) write(resolved);
196
+ });
197
+ standing = true;
198
+ const took = initial;
199
+ if (written) initial = UNRESOLVED;
200
+ if (took !== UNRESOLVED) write(took);
201
+ const edge = tail ?? createTextNode("");
202
+ LIVE_EDGES.add(edge);
203
+ return current.length === 0 ? [edge] : [...current, edge];
204
+ }
205
+ function resolve(value, current, scope) {
206
+ if (typeof value === "string" || typeof value === "number") return value;
207
+ return normalize(value, current, scope);
208
+ }
209
+ function removeAll(nodes) {
210
+ for (let i = nodes.length - 1; i >= 0; i--) detach(nodes[i]);
211
+ }
212
+ /**
213
+ * Put `value` into `parent` in place of `current`, before `marker`, and
214
+ * return what the hole holds now.
215
+ */
216
+ function placeInsert(parent, value, current, marker) {
217
+ let next;
218
+ if (typeof value === "string" || typeof value === "number") {
219
+ const text = String(value);
220
+ if (current.length === 1 && isTextNode(current[0])) {
221
+ writeText(current[0], text);
222
+ return current;
223
+ }
224
+ next = [createTextNode(text)];
225
+ } else next = value;
226
+ if (current.length === 0) {
227
+ for (let i = 0; i < next.length; i++) insertNode(parent, next[i], marker);
228
+ placed(parent);
229
+ return next;
230
+ }
231
+ if (next.length === 0) {
232
+ removeAll(current);
233
+ return [];
234
+ }
235
+ const into = parentOf(current[0]) ?? parent;
236
+ reconcile(into, current, next, marker);
237
+ placed(into);
238
+ return next;
239
+ }
240
+ /**
241
+ * Reconcile two node arrays by identity: common prefix and suffix, the swap
242
+ * of two ends, then a map of what is left. Adapted from udomdiff, as the
243
+ * document renderer's is.
244
+ */
245
+ function reconcile(parent, a, b, after) {
246
+ const bLength = b.length;
247
+ let aEnd = a.length;
248
+ let bEnd = bLength;
249
+ let aStart = 0;
250
+ let bStart = 0;
251
+ let map = null;
252
+ while (aStart < aEnd || bStart < bEnd) {
253
+ if (a[aStart] === b[bStart]) {
254
+ aStart++;
255
+ bStart++;
256
+ continue;
257
+ }
258
+ while (a[aEnd - 1] === b[bEnd - 1]) {
259
+ aEnd--;
260
+ bEnd--;
261
+ }
262
+ if (aEnd === aStart) {
263
+ const anchor = bEnd < bLength ? bStart ? nextOf(b[bStart - 1]) : b[bEnd - bStart] : after;
264
+ while (bStart < bEnd) insertNode(parent, b[bStart++], anchor);
265
+ } else if (bEnd === bStart) while (aStart < aEnd) {
266
+ if (!map || !map.has(a[aStart])) detach(a[aStart]);
267
+ aStart++;
268
+ }
269
+ else if (a[aStart] === b[bEnd - 1] && b[bStart] === a[aEnd - 1]) {
270
+ const node = nextOf(a[--aEnd]);
271
+ insertNode(parent, b[bStart++], nextOf(a[aStart++]));
272
+ insertNode(parent, b[--bEnd], node);
273
+ a[aEnd] = b[bEnd];
274
+ } else {
275
+ if (!map) {
276
+ map = /* @__PURE__ */ new Map();
277
+ for (let i = bStart; i < bEnd; i++) map.set(b[i], i);
278
+ }
279
+ const index = map.get(a[aStart]);
280
+ if (index === void 0) detach(a[aStart++]);
281
+ else if (index < bStart || index >= bEnd) aStart++;
282
+ else {
283
+ let sequence = 1;
284
+ let t;
285
+ while (aStart + sequence < aEnd && (t = map.get(a[aStart + sequence])) !== void 0 && t === index + sequence) sequence++;
286
+ if (sequence > index - bStart) {
287
+ const node = a[aStart];
288
+ while (bStart < index) insertNode(parent, b[bStart++], node);
289
+ } else {
290
+ const replaced = a[aStart++];
291
+ insertNode(parent, b[bStart++], replaced);
292
+ removeNode(parent, replaced);
293
+ }
294
+ }
295
+ }
296
+ }
297
+ }
298
+ /**
299
+ * What a list row of these nodes stands on. A row holding a live edge is
300
+ * given a node to start at, so it is read off the tree as it stands when it
301
+ * moves rather than as it was built.
302
+ */
303
+ function asRow(nodes) {
304
+ for (let i = 0; i < nodes.length; i++) {
305
+ if (!LIVE_EDGES.has(nodes[i])) continue;
306
+ const row = [createSentinel(), ...nodes];
307
+ LIVE_ROWS.add(row);
308
+ return row;
309
+ }
310
+ return nodes;
311
+ }
312
+ const host = {
313
+ sentinel: createSentinel,
314
+ park,
315
+ isPark: (node) => parks.has(node),
316
+ parentOf,
317
+ nextOf,
318
+ prevOf(node) {
319
+ const parent = parentOf(node);
320
+ if (parent === null) return null;
321
+ let previous = null;
322
+ for (let at = firstOf(parent); at !== null && at !== node; at = nextOf(at)) previous = at;
323
+ return previous;
324
+ },
325
+ firstOf,
326
+ lastOf(parent) {
327
+ let last = null;
328
+ for (let at = firstOf(parent); at !== null; at = nextOf(at)) last = at;
329
+ return last;
330
+ },
331
+ row: asRow,
332
+ toNodes,
333
+ insertRange(parent, nodes, before) {
334
+ let anchor = before;
335
+ for (let i = nodes.length; i--;) {
336
+ place(parent, nodes[i], anchor);
337
+ anchor = nodes[i];
338
+ }
339
+ placed(parent);
340
+ },
341
+ removeRange: removeAll
342
+ };
343
+ function rowFirst(row) {
344
+ return isNodes(row) ? row[0] : row;
345
+ }
346
+ function nodesOf(row) {
347
+ return isNodes(row) ? standingRow(host, row) : [row];
348
+ }
349
+ /**
350
+ * One list update. The rows both lists start and end with stay where they
351
+ * are, two ends that traded places are two moves, and what is left is
352
+ * placed against the longest run that already stands in order.
353
+ */
354
+ const syncRows = (parent, anchor, current, next) => {
355
+ const shared = Math.min(current.length, next.length);
356
+ let head = 0;
357
+ while (head < shared && current[head] === next[head]) head++;
358
+ let was = current.length;
359
+ let now = next.length;
360
+ while (was > head && now > head && current[was - 1] === next[now - 1]) {
361
+ was--;
362
+ now--;
363
+ }
364
+ while (was - head > 1 && now - head > 1 && current[head] === next[now - 1] && next[head] === current[was - 1] && swapEnds(parent, current[head], current[was - 1])) {
365
+ head++;
366
+ was--;
367
+ now--;
368
+ while (head < was && head < now && current[head] === next[head]) head++;
369
+ while (was > head && now > head && current[was - 1] === next[now - 1]) {
370
+ was--;
371
+ now--;
372
+ }
373
+ }
374
+ const leaving = head === 0 && was === current.length ? current : current.slice(head, was);
375
+ const arriving = head === 0 && now === next.length ? next : next.slice(head, now);
376
+ if (leaving.length > 0) {
377
+ if (arriving.length === 0) removeAll(standingRows(host, leaving));
378
+ else {
379
+ const kept = new Set(arriving);
380
+ for (const row of leaving) if (!kept.has(row)) removeAll(nodesOf(row));
381
+ }
382
+ }
383
+ if (arriving.length > 0) {
384
+ let after = anchor;
385
+ for (let i = now; i < next.length; i++) {
386
+ const lead = rowFirst(next[i]);
387
+ if (lead === void 0) continue;
388
+ after = lead;
389
+ break;
390
+ }
391
+ orderRows(parent, after, leaving, arriving);
392
+ }
393
+ placed(parent);
394
+ };
395
+ function swapEnds(parent, low, high) {
396
+ const lowNodes = nodesOf(low);
397
+ const highNodes = nodesOf(high);
398
+ if (lowNodes.length === 0 || highNodes.length === 0) return false;
399
+ const afterHigh = nextOf(highNodes[highNodes.length - 1]);
400
+ const lowFirst = lowNodes[0];
401
+ for (let i = 0; i < highNodes.length; i++) insertNode(parent, highNodes[i], lowFirst);
402
+ for (let i = 0; i < lowNodes.length; i++) insertNode(parent, lowNodes[i], afterHigh);
403
+ return true;
404
+ }
405
+ function orderRows(parent, anchor, previous, next) {
406
+ if (previous.length === 0) {
407
+ let before = anchor;
408
+ for (let i = next.length - 1; i >= 0; i--) {
409
+ const row = nodesOf(next[i]);
410
+ for (let j = row.length - 1; j >= 0; j--) {
411
+ place(parent, row[j], before);
412
+ before = row[j];
413
+ }
414
+ }
415
+ return;
416
+ }
417
+ const previousIndex = /* @__PURE__ */ new Map();
418
+ for (let i = 0; i < previous.length; i++) previousIndex.set(previous[i], i);
419
+ const sources = new Array(next.length);
420
+ for (let i = 0; i < next.length; i++) sources[i] = previousIndex.get(next[i]) ?? -1;
421
+ const lis = longestIncreasingSubsequence(sources.filter((source) => source !== -1));
422
+ let lisIndex = lis.length - 1;
423
+ let before = anchor;
424
+ for (let i = next.length - 1; i >= 0; i--) {
425
+ const row = next[i];
426
+ const first = rowFirst(row);
427
+ if (first === void 0) continue;
428
+ if (sources[i] !== -1 && lisIndex >= 0 && lis[lisIndex] === sources[i]) lisIndex--;
429
+ else {
430
+ const nodes = nodesOf(row);
431
+ for (let j = 0; j < nodes.length; j++) insertNode(parent, nodes[j], before);
432
+ }
433
+ before = first;
434
+ }
435
+ }
436
+ function insert(parent, value, marker) {
437
+ const given = getOwner();
438
+ const anchor = marker ?? null;
439
+ if (typeof value === "function" && isBlock(value)) {
440
+ value = buildChild(value, given);
441
+ if (typeof value === "function") {
442
+ insert(parent, value, marker);
443
+ return;
444
+ }
445
+ }
446
+ if (typeof value === "function") {
447
+ insertLive(parent, value, anchor, given);
448
+ return;
449
+ }
450
+ if (value === null || value === void 0 || typeof value === "boolean") return;
451
+ const nodes = toNodes(value, given);
452
+ for (let i = 0; i < nodes.length; i++) insertNode(parent, nodes[i], anchor);
453
+ placed(parent);
454
+ }
455
+ function insertLive(parent, value, anchor, given) {
456
+ let current = [];
457
+ effectUnder(given, "insert", () => {
458
+ const owner = getOwner();
459
+ return resolve(yielded(buildChild(value, owner), owner), current, given);
460
+ }, (resolved) => {
461
+ current = placeInsert(parent, resolved, current, anchor);
462
+ });
463
+ }
464
+ function setProp(node, name, value, prev) {
465
+ if (name === "ref") {
466
+ ref(node, value);
467
+ return value;
468
+ }
469
+ setProperty(node, name, value, prev);
470
+ return value;
471
+ }
472
+ function refuseBlock(target, origin, returned = false) {
473
+ if (isBlock(target)) throw jsxAsValue(origin, returned);
474
+ }
475
+ /**
476
+ * A ref registration owned by the scope the node belongs to: a callback that
477
+ * returns a function has it run when that scope goes.
478
+ */
479
+ function ref(node, value) {
480
+ const owner = getOwner();
481
+ const undo = [];
482
+ const one = (target) => {
483
+ refuseBlock(target, "A `ref`");
484
+ if (typeof target === "function") {
485
+ if (attachRef === void 0) {
486
+ const back = target(node);
487
+ refuseBlock(back, "A `ref`", true);
488
+ if (typeof back === "function") undo.push(back);
489
+ return;
490
+ }
491
+ let release;
492
+ const stop = attachRef(node, (held) => {
493
+ release?.();
494
+ const back = target(held);
495
+ refuseBlock(back, "A `ref`", true);
496
+ release = typeof back === "function" ? back : void 0;
497
+ });
498
+ undo.push(() => {
499
+ stop();
500
+ release?.();
501
+ });
502
+ } else if (typeof target === "object" && target !== null && "current" in target) {
503
+ if (attachRef === void 0) {
504
+ target.current = node;
505
+ return;
506
+ }
507
+ undo.push(attachRef(node, (held) => {
508
+ target.current = held;
509
+ }));
510
+ }
511
+ };
512
+ if (Array.isArray(value)) for (const target of value) one(target);
513
+ else one(value);
514
+ if (undo.length === 0 || owner === null) return;
515
+ underScope(owner, "ref", () => {
516
+ onCleanup(() => {
517
+ for (const fn of undo) fn();
518
+ });
519
+ });
520
+ }
521
+ const isHandlerKey = (key) => key.charCodeAt(0) === 111 && key.charCodeAt(1) === 110;
522
+ /**
523
+ * A props object onto a node, diffed against what was last applied, with a
524
+ * property that vanished set to `undefined`. A getter is read in the
525
+ * tracked half, so the write half sees values only; a handler and a `ref`
526
+ * are functions that are not getters and pass through as they are.
527
+ * `children` is not a property: the element's own children are placed by
528
+ * whoever built it.
529
+ */
530
+ function spread(node, props, later) {
531
+ const given = getOwner();
532
+ const applied = {};
533
+ let first = true;
534
+ const read = () => {
535
+ const next = typeof props === "function" ? props() : props;
536
+ const resolved = {};
537
+ for (const key in next) {
538
+ if (key === "children") continue;
539
+ if (later !== void 0 && later.includes(key)) continue;
540
+ const value = next[key];
541
+ if (key === "ref" || isHandlerKey(key)) {
542
+ resolved[key] = value;
543
+ continue;
544
+ }
545
+ refuseBlock(value, `The \`${key}\` prop in a spread`);
546
+ resolved[key] = typeof value === "function" ? value() : value;
547
+ }
548
+ return resolved;
549
+ };
550
+ effectUnder(given, "spread", read, (next) => {
551
+ for (const key in applied) {
552
+ if (key in next) continue;
553
+ setProperty(node, key, void 0, applied[key]);
554
+ delete applied[key];
555
+ }
556
+ for (const key in next) {
557
+ const value = next[key];
558
+ if (key === "ref") {
559
+ if (first) runWithOwner(given, () => ref(node, value));
560
+ continue;
561
+ }
562
+ if (key in applied && applied[key] === value) continue;
563
+ setProperty(node, key, value, applied[key]);
564
+ applied[key] = value;
565
+ }
566
+ first = false;
567
+ });
568
+ }
569
+ /** One element by tag name, its props spread and its children inserted. */
570
+ function element(tag, props) {
571
+ const node = ops.createElement(tag);
572
+ spread(node, props);
573
+ const children = props.children;
574
+ if (children !== void 0 && children !== null) insert(node, children);
575
+ return node;
576
+ }
577
+ function portal(target, block) {
578
+ const given = getOwner();
579
+ cellSlot(target, "Portal.mount");
580
+ cellSlot(untrack(target), "Portal.mount", true);
581
+ const marker = createSentinel();
582
+ underScope(given, "portal", () => {
583
+ let instance = NOTHING;
584
+ let request = 0;
585
+ const site = {
586
+ host,
587
+ parent: null,
588
+ anchor: null
589
+ };
590
+ const clear = () => {
591
+ if (instance !== NOTHING) {
592
+ teardown(host, instance);
593
+ instance = NOTHING;
594
+ }
595
+ site.parent = null;
596
+ };
597
+ createEffect(() => {
598
+ const requested = target();
599
+ cellSlot(requested, "Portal.mount", true);
600
+ return requested;
601
+ }, (requested) => {
602
+ clear();
603
+ const mine = ++request;
604
+ const mount = () => {
605
+ if (mine !== request) return;
606
+ if (requested !== null && requested !== void 0 && typeof requested !== "object") throw new TypeError(`\`<Portal mount>\` was given ${JSON.stringify(requested)}, and on a custom host it takes a node of that host. A selector is something only the document can look up: pass the node itself, or leave \`mount\` out to mount into the render root.`);
607
+ const into = requested ?? renderRootOf(given);
608
+ if (into === null) return;
609
+ site.parent = into;
610
+ nextActivation();
611
+ instance = activate(given, site, block, NO_ARGS, 0, "portal");
612
+ };
613
+ if (given === null) queueMicrotask(mount);
614
+ else mount();
615
+ });
616
+ onCleanup(clear);
617
+ });
618
+ return marker;
619
+ }
620
+ function renderRootOf(owner) {
621
+ const found = lookupContext(owner, RENDER_ROOT);
622
+ return found === CONTEXT_MISS ? null : found;
623
+ }
624
+ function Portal(props) {
625
+ return portal(() => readSlot(props.mount, "Portal.mount"), props.children);
626
+ }
627
+ function dynamic(source) {
628
+ return (props) => {
629
+ const component = internalMemo(source);
630
+ return branch(null, null, component, () => {
631
+ const resolved = untrack(component);
632
+ if (resolved === null || resolved === void 0 || resolved === false) return null;
633
+ if (typeof resolved === "string") return element(resolved, props);
634
+ return resolved(props);
635
+ });
636
+ };
637
+ }
638
+ function branch(parent, anchor, key, bodies, flags) {
639
+ return branchIn(host, parent, anchor, key, bodies, flags);
640
+ }
641
+ function render(code, root, options) {
642
+ const scope = rootScope(options?.owner ?? null, options?.renderId);
643
+ provideHost(scope, {
644
+ host,
645
+ sync: syncRows
646
+ });
647
+ provideOn(scope, RENDER_ROOT, root);
648
+ ownRange(scope, () => {
649
+ for (let child = firstOf(root); child !== null; child = firstOf(root)) removeNode(root, child);
650
+ });
651
+ withoutClaim(() => {
652
+ mountTree(() => {
653
+ runWithOwner(scope, () => {
654
+ insert(root, typeof code === "function" ? code() : code);
655
+ });
656
+ });
657
+ });
658
+ return () => {
659
+ disposeScope(scope);
660
+ };
661
+ }
662
+ offerDefaultHost(host, syncRows);
663
+ return {
664
+ render,
665
+ createElement: (tag) => ops.createElement(tag),
666
+ createTextNode,
667
+ insertNode,
668
+ setProp,
669
+ insert,
670
+ spread,
671
+ branch,
672
+ each: (parent, anchor, src, carrier, row, flags, fallback) => eachIn(host, syncRows, parent, anchor, src, carrier, row, flags, fallback),
673
+ boundary: (parent, anchor, kind, fallback, body, flags, on) => boundaryIn(host, parent, anchor, kind, fallback, body, flags, on),
674
+ island: (parent, anchor, block, flags) => islandIn(host, parent, anchor, block, flags),
675
+ reveal: (order, collapsed, block) => revealIn(host, order, collapsed, block),
676
+ portal,
677
+ dynamic,
678
+ Portal,
679
+ host,
680
+ syncRows
681
+ };
682
+ }
683
+ //#endregion
684
+ export { createRenderer };
685
+
686
+ //# sourceMappingURL=index.js.map