svelte 5.58.0 → 5.59.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/compiler/index.js +1 -1
- package/package.json +19 -1
- package/renderer.d.ts +1 -0
- package/src/compiler/errors.js +19 -0
- package/src/compiler/index.js +37 -2
- package/src/compiler/migrate/index.js +2 -1
- package/src/compiler/phases/1-parse/read/options.js +20 -0
- package/src/compiler/phases/2-analyze/index.js +21 -2
- package/src/compiler/phases/2-analyze/visitors/AnimateDirective.js +5 -0
- package/src/compiler/phases/2-analyze/visitors/Attribute.js +6 -2
- package/src/compiler/phases/2-analyze/visitors/BindDirective.js +4 -0
- package/src/compiler/phases/2-analyze/visitors/ExpressionTag.js +2 -1
- package/src/compiler/phases/2-analyze/visitors/HtmlTag.js +5 -0
- package/src/compiler/phases/2-analyze/visitors/OnDirective.js +6 -0
- package/src/compiler/phases/2-analyze/visitors/RegularElement.js +13 -6
- package/src/compiler/phases/2-analyze/visitors/SvelteBody.js +5 -0
- package/src/compiler/phases/2-analyze/visitors/SvelteDocument.js +5 -0
- package/src/compiler/phases/2-analyze/visitors/SvelteElement.js +5 -1
- package/src/compiler/phases/2-analyze/visitors/SvelteHead.js +5 -0
- package/src/compiler/phases/2-analyze/visitors/SvelteWindow.js +5 -0
- package/src/compiler/phases/2-analyze/visitors/Text.js +3 -1
- package/src/compiler/phases/2-analyze/visitors/TransitionDirective.js +6 -0
- package/src/compiler/phases/2-analyze/visitors/shared/element.js +3 -2
- package/src/compiler/phases/3-transform/client/transform-client.js +68 -1
- package/src/compiler/phases/3-transform/client/transform-template/index.js +4 -3
- package/src/compiler/phases/3-transform/client/transform-template/template.js +1 -1
- package/src/compiler/phases/3-transform/client/utils.js +3 -2
- package/src/compiler/phases/3-transform/client/visitors/RegularElement.js +23 -13
- package/src/compiler/phases/3-transform/client/visitors/RenderTag.js +13 -1
- package/src/compiler/phases/3-transform/client/visitors/SnippetBlock.js +11 -1
- package/src/compiler/phases/3-transform/client/visitors/SvelteBoundary.js +8 -5
- package/src/compiler/phases/3-transform/client/visitors/shared/component.js +17 -8
- package/src/compiler/phases/3-transform/client/visitors/shared/element.js +4 -3
- package/src/compiler/phases/3-transform/client/visitors/shared/fragment.js +7 -1
- package/src/compiler/phases/3-transform/server/transform-server.js +14 -1
- package/src/compiler/phases/3-transform/server/visitors/ExpressionStatement.js +8 -0
- package/src/compiler/phases/3-transform/server/visitors/RegularElement.js +8 -4
- package/src/compiler/print/index.js +45 -57
- package/src/compiler/state.js +12 -0
- package/src/compiler/utils/builders.js +2 -2
- package/src/compiler/validate-options.js +47 -1
- package/src/index-client.js +3 -3
- package/src/internal/client/constants.js +15 -1
- package/src/internal/client/custom-renderer/containers.js +213 -0
- package/src/internal/client/custom-renderer/cross.js +145 -0
- package/src/internal/client/custom-renderer/index.js +28 -0
- package/src/internal/client/custom-renderer/state.js +59 -0
- package/src/internal/client/dev/css.js +3 -1
- package/src/internal/client/dev/debug.js +6 -6
- package/src/internal/client/dev/elements.js +9 -7
- package/src/internal/client/dev/tracing.js +67 -11
- package/src/internal/client/dev/validation.js +2 -1
- package/src/internal/client/dom/blocks/async.js +2 -1
- package/src/internal/client/dom/blocks/await.js +110 -19
- package/src/internal/client/dom/blocks/boundary.js +142 -88
- package/src/internal/client/dom/blocks/branches.js +94 -75
- package/src/internal/client/dom/blocks/css-props.js +31 -3
- package/src/internal/client/dom/blocks/each.js +166 -45
- package/src/internal/client/dom/blocks/html.js +19 -13
- package/src/internal/client/dom/blocks/slot.js +3 -3
- package/src/internal/client/dom/blocks/snippet.js +52 -3
- package/src/internal/client/dom/blocks/svelte-element.js +17 -6
- package/src/internal/client/dom/blocks/svelte-head.js +13 -4
- package/src/internal/client/dom/css.js +4 -4
- package/src/internal/client/dom/elements/attributes.js +91 -51
- package/src/internal/client/dom/elements/bindings/input.js +19 -10
- package/src/internal/client/dom/elements/bindings/select.js +13 -9
- package/src/internal/client/dom/elements/class.js +16 -5
- package/src/internal/client/dom/elements/events.js +153 -21
- package/src/internal/client/dom/elements/misc.js +3 -2
- package/src/internal/client/dom/elements/style.js +17 -6
- package/src/internal/client/dom/hydration.js +6 -6
- package/src/internal/client/dom/operations.js +435 -14
- package/src/internal/client/dom/reconciler.js +2 -2
- package/src/internal/client/dom/template.js +94 -66
- package/src/internal/client/error-handling.js +4 -12
- package/src/internal/client/errors.js +85 -2
- package/src/internal/client/index.js +9 -2
- package/src/internal/client/reactivity/async.js +257 -122
- package/src/internal/client/reactivity/batch.js +1124 -531
- package/src/internal/client/reactivity/deriveds.js +281 -177
- package/src/internal/client/reactivity/effects.js +87 -54
- package/src/internal/client/reactivity/sources.js +30 -124
- package/src/internal/client/reactivity/status.js +1 -1
- package/src/internal/client/render.js +60 -87
- package/src/internal/client/runtime.js +931 -153
- package/src/internal/client/validate.js +2 -2
- package/src/internal/disclose-version.js +1 -1
- package/src/internal/flags/custom-renderer-server.js +10 -0
- package/src/internal/flags/custom-renderer.js +3 -0
- package/src/internal/flags/index.js +6 -0
- package/src/internal/init-operations.js +3 -0
- package/src/internal/shared/errors.js +20 -0
- package/src/legacy/legacy-client.js +6 -2
- package/src/renderer/index-server.js +18 -0
- package/src/renderer/index.js +2 -0
- package/src/version.js +1 -1
- package/types/index.d.ts +245 -25
- package/types/index.d.ts.map +10 -1
- package/src/internal/client/reactivity/utils.js +0 -18
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import * as esrap from 'esrap';
|
|
4
4
|
import ts from 'esrap/languages/ts';
|
|
5
5
|
import { is_void } from '../../utils.js';
|
|
6
|
+
import { regex_ends_with_whitespaces, regex_starts_with_whitespaces } from '../phases/patterns.js';
|
|
6
7
|
|
|
7
8
|
/** Threshold for when content should be formatted on separate lines */
|
|
8
9
|
const LINE_BREAK_THRESHOLD = 50;
|
|
@@ -81,7 +82,9 @@ function escape_identifier(name) {
|
|
|
81
82
|
*/
|
|
82
83
|
export function print(ast, options = undefined) {
|
|
83
84
|
const comments = (ast.type === 'Root' && ast.comments) || [];
|
|
84
|
-
const state = {
|
|
85
|
+
const state = {
|
|
86
|
+
preserve_whitespace: ast.type === 'Root' && ast.options?.preserveWhitespace ? 1 : 0
|
|
87
|
+
};
|
|
85
88
|
const css_comments =
|
|
86
89
|
(ast.type === 'Root' ? ast.css?.comments : ast.type === 'StyleSheet' ? ast.comments : null) ||
|
|
87
90
|
[];
|
|
@@ -122,7 +125,8 @@ function block(context, node, preserve_whitespace = false, allow_inline = false)
|
|
|
122
125
|
return;
|
|
123
126
|
}
|
|
124
127
|
|
|
125
|
-
|
|
128
|
+
// Wrapping at fragment edges is safe even when adjacent children must stay together.
|
|
129
|
+
if (allow_inline && !child_context.multiline && child_context.measure() <= LINE_BREAK_THRESHOLD) {
|
|
126
130
|
context.append(child_context);
|
|
127
131
|
} else {
|
|
128
132
|
context.indent();
|
|
@@ -260,7 +264,11 @@ function print_element(node, context, comments, state) {
|
|
|
260
264
|
const name = node.name.toLowerCase();
|
|
261
265
|
const preserve =
|
|
262
266
|
(node.type === 'RegularElement' || node.type === 'TitleElement') &&
|
|
263
|
-
(name === 'pre' ||
|
|
267
|
+
(name === 'pre' ||
|
|
268
|
+
name === 'textarea' ||
|
|
269
|
+
name === 'title' ||
|
|
270
|
+
name === 'script' ||
|
|
271
|
+
name === 'style');
|
|
264
272
|
|
|
265
273
|
if (preserve) state.preserve_whitespace += 1;
|
|
266
274
|
base_element(node, context, comments, state);
|
|
@@ -552,12 +560,14 @@ const svelte_visitors = (comments, state) => ({
|
|
|
552
560
|
for (const item of [node.module, node.instance, node.fragment, node.css]) {
|
|
553
561
|
if (!item) continue;
|
|
554
562
|
|
|
555
|
-
if (started) {
|
|
563
|
+
if (started && state.preserve_whitespace === 0) {
|
|
556
564
|
context.margin();
|
|
557
565
|
context.newline();
|
|
558
566
|
}
|
|
559
567
|
|
|
560
|
-
context.
|
|
568
|
+
const child_context = context.new();
|
|
569
|
+
child_context.visit(item);
|
|
570
|
+
context.append(child_context);
|
|
561
571
|
started = true;
|
|
562
572
|
}
|
|
563
573
|
},
|
|
@@ -566,7 +576,7 @@ const svelte_visitors = (comments, state) => ({
|
|
|
566
576
|
context.write('<script');
|
|
567
577
|
attributes(node, node.attributes, context, comments);
|
|
568
578
|
context.write('>');
|
|
569
|
-
block(context, node.content
|
|
579
|
+
block(context, node.content);
|
|
570
580
|
context.write('</script>');
|
|
571
581
|
},
|
|
572
582
|
|
|
@@ -574,7 +584,6 @@ const svelte_visitors = (comments, state) => ({
|
|
|
574
584
|
if (state.preserve_whitespace > 0) {
|
|
575
585
|
for (const child of node.nodes) {
|
|
576
586
|
context.visit(child);
|
|
577
|
-
context.multiline ||= child.type === 'Text' && /[\r\n]/.test(child.data);
|
|
578
587
|
}
|
|
579
588
|
return;
|
|
580
589
|
}
|
|
@@ -583,9 +592,9 @@ const svelte_visitors = (comments, state) => ({
|
|
|
583
592
|
const last = node.nodes.at(-1);
|
|
584
593
|
const has_surrounding_whitespace =
|
|
585
594
|
first?.type === 'Text' &&
|
|
586
|
-
|
|
595
|
+
regex_starts_with_whitespaces.test(first.raw) &&
|
|
587
596
|
last?.type === 'Text' &&
|
|
588
|
-
|
|
597
|
+
regex_ends_with_whitespaces.test(last.raw);
|
|
589
598
|
|
|
590
599
|
/** @type {{ nodes: AST.SvelteNode[]; leading_whitespace: boolean }[]} */
|
|
591
600
|
const items = [];
|
|
@@ -603,62 +612,40 @@ const svelte_visitors = (comments, state) => ({
|
|
|
603
612
|
};
|
|
604
613
|
|
|
605
614
|
for (let i = 0; i < node.nodes.length; i += 1) {
|
|
606
|
-
|
|
607
|
-
|
|
615
|
+
const child_node = node.nodes[i];
|
|
608
616
|
const prev = node.nodes[i - 1];
|
|
609
617
|
const next = node.nodes[i + 1];
|
|
610
618
|
|
|
611
|
-
if (child_node.type
|
|
612
|
-
child_node
|
|
619
|
+
if (child_node.type !== 'Text') {
|
|
620
|
+
sequence.push(child_node);
|
|
621
|
+
continue;
|
|
622
|
+
}
|
|
613
623
|
|
|
614
|
-
|
|
624
|
+
let raw = child_node.raw;
|
|
615
625
|
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
626
|
+
// Like the compiler, trim fragment edges, but preserve whitespace inside
|
|
627
|
+
// text and next to expressions. In particular, do not trim non-breaking spaces.
|
|
628
|
+
if (i === 0) raw = raw.replace(regex_starts_with_whitespaces, '');
|
|
629
|
+
if (i === node.nodes.length - 1) raw = raw.replace(regex_ends_with_whitespaces, '');
|
|
630
|
+
if (raw === '') continue;
|
|
620
631
|
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
632
|
+
// Only split sequences where there was collapsible whitespace already.
|
|
633
|
+
// Inserting a newline between adjacent elements would introduce a space.
|
|
634
|
+
if (prev?.type !== 'ExpressionTag' && regex_starts_with_whitespaces.test(raw)) {
|
|
635
|
+
flush();
|
|
636
|
+
leading_whitespace = true;
|
|
637
|
+
raw = raw.replace(regex_starts_with_whitespaces, '');
|
|
638
|
+
}
|
|
628
639
|
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
leading_whitespace = true;
|
|
632
|
-
child_node.data = child_node.data.trimStart();
|
|
633
|
-
}
|
|
640
|
+
const trailing_whitespace =
|
|
641
|
+
next?.type !== 'ExpressionTag' && regex_ends_with_whitespaces.test(raw);
|
|
634
642
|
|
|
635
|
-
|
|
636
|
-
|
|
643
|
+
if (trailing_whitespace) raw = raw.replace(regex_ends_with_whitespaces, '');
|
|
644
|
+
if (raw !== '') sequence.push({ ...child_node, raw });
|
|
637
645
|
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
}
|
|
642
|
-
}
|
|
643
|
-
} else {
|
|
644
|
-
const is_block_element =
|
|
645
|
-
child_node.type === 'RegularElement' ||
|
|
646
|
-
child_node.type === 'Component' ||
|
|
647
|
-
child_node.type === 'SvelteBody' ||
|
|
648
|
-
child_node.type === 'SvelteHead' ||
|
|
649
|
-
child_node.type === 'SvelteFragment' ||
|
|
650
|
-
child_node.type === 'SvelteBoundary' ||
|
|
651
|
-
child_node.type === 'SvelteDocument' ||
|
|
652
|
-
child_node.type === 'SvelteSelf' ||
|
|
653
|
-
child_node.type === 'SvelteWindow' ||
|
|
654
|
-
child_node.type === 'SvelteComponent' ||
|
|
655
|
-
child_node.type === 'SvelteElement' ||
|
|
656
|
-
child_node.type === 'SlotElement' ||
|
|
657
|
-
child_node.type === 'TitleElement';
|
|
658
|
-
|
|
659
|
-
if (is_block_element && sequence.length > 0) flush();
|
|
660
|
-
sequence.push(child_node);
|
|
661
|
-
if (is_block_element) flush();
|
|
646
|
+
if (trailing_whitespace) {
|
|
647
|
+
flush();
|
|
648
|
+
leading_whitespace = true;
|
|
662
649
|
}
|
|
663
650
|
}
|
|
664
651
|
|
|
@@ -1125,7 +1112,8 @@ const svelte_visitors = (comments, state) => ({
|
|
|
1125
1112
|
},
|
|
1126
1113
|
|
|
1127
1114
|
Text(node, context) {
|
|
1128
|
-
context.write(node.
|
|
1115
|
+
context.write(node.raw);
|
|
1116
|
+
context.multiline ||= /[\r\n]/.test(node.raw);
|
|
1129
1117
|
},
|
|
1130
1118
|
|
|
1131
1119
|
TitleElement(node, context) {
|
package/src/compiler/state.js
CHANGED
|
@@ -50,6 +50,15 @@ export let dev;
|
|
|
50
50
|
|
|
51
51
|
export let runes = false;
|
|
52
52
|
|
|
53
|
+
/**
|
|
54
|
+
* The custom renderer for the component currently being compiled:
|
|
55
|
+
* - `string`: the renderer module path (the component uses a custom renderer)
|
|
56
|
+
* - `null`: the custom renderer feature is enabled but this component renders to the DOM
|
|
57
|
+
* - `undefined`: the custom renderer feature is off
|
|
58
|
+
* @type {string | null | undefined}
|
|
59
|
+
*/
|
|
60
|
+
export let custom_renderer = undefined;
|
|
61
|
+
|
|
53
62
|
/** @type {(index: number) => Location} */
|
|
54
63
|
export let locator;
|
|
55
64
|
|
|
@@ -143,6 +152,7 @@ export function is_ignored(node, code) {
|
|
|
143
152
|
export function reset(state) {
|
|
144
153
|
dev = false;
|
|
145
154
|
runes = false;
|
|
155
|
+
custom_renderer = undefined;
|
|
146
156
|
component_name = UNKNOWN_FILENAME;
|
|
147
157
|
source = '';
|
|
148
158
|
source_lines = undefined;
|
|
@@ -159,6 +169,7 @@ export function reset(state) {
|
|
|
159
169
|
* component_name?: string;
|
|
160
170
|
* rootDir?: string;
|
|
161
171
|
* runes: boolean;
|
|
172
|
+
* custom_renderer?: string | null | undefined;
|
|
162
173
|
* }} state
|
|
163
174
|
*/
|
|
164
175
|
export function adjust(state) {
|
|
@@ -166,6 +177,7 @@ export function adjust(state) {
|
|
|
166
177
|
|
|
167
178
|
dev = state.dev;
|
|
168
179
|
runes = state.runes;
|
|
180
|
+
custom_renderer = state.custom_renderer;
|
|
169
181
|
component_name = state.component_name ?? UNKNOWN_FILENAME;
|
|
170
182
|
|
|
171
183
|
if (typeof root_dir === 'string' && filename.startsWith(root_dir)) {
|
|
@@ -636,7 +636,7 @@ export function import_all(as, source) {
|
|
|
636
636
|
}
|
|
637
637
|
|
|
638
638
|
/**
|
|
639
|
-
* @param {Array<[string, string]>} parts
|
|
639
|
+
* @param {Array<[string, string] | [string, string, boolean]>} parts
|
|
640
640
|
* @param {string} source
|
|
641
641
|
* @returns {ESTree.ImportDeclaration}
|
|
642
642
|
*/
|
|
@@ -646,7 +646,7 @@ export function imports(parts, source) {
|
|
|
646
646
|
attributes: [],
|
|
647
647
|
source: literal(source),
|
|
648
648
|
specifiers: parts.map((p) => ({
|
|
649
|
-
type: 'ImportSpecifier',
|
|
649
|
+
type: p[2] ? 'ImportDefaultSpecifier' : 'ImportSpecifier',
|
|
650
650
|
imported: id(p[0]),
|
|
651
651
|
local: id(p[1])
|
|
652
652
|
}))
|
|
@@ -44,7 +44,53 @@ const common_options = {
|
|
|
44
44
|
warningFilter: fun(() => true),
|
|
45
45
|
|
|
46
46
|
experimental: object({
|
|
47
|
-
async: boolean(false)
|
|
47
|
+
async: boolean(false),
|
|
48
|
+
// `customRenderer` can be:
|
|
49
|
+
// - `undefined`: the feature is off, components compile to plain DOM
|
|
50
|
+
// - `true`: the feature is on, every component defaults to DOM (opt in via `<svelte:options>`)
|
|
51
|
+
// - a string: the feature is on, every component defaults to that module (opt out via `<svelte:options>`)
|
|
52
|
+
// - a function: the feature is on, the module is resolved lazily per file (`null` means DOM)
|
|
53
|
+
// The normalized value is always a function returning `string | null | undefined` where
|
|
54
|
+
// `string` is the renderer module, `null` is "DOM but feature enabled" (push `null`) and
|
|
55
|
+
// `undefined` is "feature off" (no renderer pushed at all). A function can't return
|
|
56
|
+
// `undefined` for individual files: those components wouldn't push `null`, so they'd break
|
|
57
|
+
// when rendered inside a component that uses a custom renderer. We throw rather than
|
|
58
|
+
// silently treating `undefined` as `null`, so that a forgotten `return` doesn't go unnoticed.
|
|
59
|
+
customRenderer: validator(
|
|
60
|
+
/** @type {(options: { filename: string }) => string | null | undefined} */ (() => undefined),
|
|
61
|
+
(input, keypath) => {
|
|
62
|
+
if (input === true) {
|
|
63
|
+
return () => null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (typeof input === 'string') {
|
|
67
|
+
// the module is imported by every component, so a relative path would resolve
|
|
68
|
+
// relative to each component rather than to wherever the option is set (which
|
|
69
|
+
// the compiler can't know)
|
|
70
|
+
if (/^\.\.?([/\\]|$)/.test(input)) {
|
|
71
|
+
throw_error(
|
|
72
|
+
`${keypath} cannot be a relative path, because it would be resolved relative to each component. Use a package name, an alias or an absolute path instead (received "${input}")`
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return () => input;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (typeof input === 'function') {
|
|
80
|
+
return (/** @type {{ filename: string }} */ options) => {
|
|
81
|
+
const result = input(options);
|
|
82
|
+
|
|
83
|
+
if (result !== null && typeof result !== 'string') {
|
|
84
|
+
throw_error(`${keypath} function must return a string or null`);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return result;
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
throw_error(`${keypath} should be true, a string or a function, if specified`);
|
|
92
|
+
}
|
|
93
|
+
)
|
|
48
94
|
})
|
|
49
95
|
};
|
|
50
96
|
|
package/src/index-client.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/** @import { ComponentContext, ComponentContextLegacy } from '#client' */
|
|
2
2
|
/** @import { EventDispatcher } from './index.js' */
|
|
3
3
|
/** @import { NotFunction } from './internal/types.js' */
|
|
4
|
-
import { active_reaction, untrack } from './internal/client/runtime.js';
|
|
4
|
+
import { active_reaction, get_abort_signal, untrack } from './internal/client/runtime.js';
|
|
5
5
|
import { is_array } from './internal/shared/utils.js';
|
|
6
6
|
import { user_effect } from './internal/client/index.js';
|
|
7
7
|
import * as e from './internal/client/errors.js';
|
|
@@ -46,7 +46,7 @@ if (DEV) {
|
|
|
46
46
|
/**
|
|
47
47
|
* Returns an [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) that aborts when the current [derived](https://svelte.dev/docs/svelte/$derived) or [effect](https://svelte.dev/docs/svelte/$effect) re-runs or is destroyed.
|
|
48
48
|
*
|
|
49
|
-
* Must be called while a derived or effect is running.
|
|
49
|
+
* Must be called while a derived or effect is running, before anything is awaited.
|
|
50
50
|
*
|
|
51
51
|
* ```svelte
|
|
52
52
|
* <script>
|
|
@@ -71,7 +71,7 @@ export function getAbortSignal() {
|
|
|
71
71
|
e.get_abort_signal_outside_reaction();
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
return (active_reaction
|
|
74
|
+
return get_abort_signal(active_reaction);
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
/**
|
|
@@ -19,7 +19,7 @@ export const BOUNDARY_EFFECT = 1 << 7;
|
|
|
19
19
|
* Set on the effect that `pause_effect` was called on, i.e. the root of a paused subtree,
|
|
20
20
|
* as opposed to its descendants which are merely `INERT`. This allows `resume_effect` on
|
|
21
21
|
* an ancestor to skip subtrees that were paused for their own reasons (such as a block
|
|
22
|
-
* whose condition is still false) rather than resurrecting them
|
|
22
|
+
* whose condition is still false) rather than resurrecting them.
|
|
23
23
|
*/
|
|
24
24
|
export const PAUSED = 1 << 8;
|
|
25
25
|
/**
|
|
@@ -57,6 +57,13 @@ export const ASYNC = 1 << 22;
|
|
|
57
57
|
|
|
58
58
|
export const ERROR_VALUE = 1 << 23;
|
|
59
59
|
|
|
60
|
+
/** A run of the reaction asked for an abort signal (see `getAbortSignal`) */
|
|
61
|
+
export const HAS_ABORT_CONTROLLER = 1 << 27;
|
|
62
|
+
|
|
63
|
+
// Flags exclusive to deriveds
|
|
64
|
+
/** The global state of the derived is a value that was assigned to it, rather than a run of it */
|
|
65
|
+
export const ASSIGNED = 1 << 26;
|
|
66
|
+
|
|
60
67
|
export const STATE_SYMBOL = Symbol('$state');
|
|
61
68
|
/** Marks component export objects, so that `proxy(...)` leaves them untouched */
|
|
62
69
|
export const COMPONENT_SYMBOL = Symbol('component');
|
|
@@ -85,3 +92,10 @@ export const ELEMENT_NODE = 1;
|
|
|
85
92
|
export const TEXT_NODE = 3;
|
|
86
93
|
export const COMMENT_NODE = 8;
|
|
87
94
|
export const DOCUMENT_FRAGMENT_NODE = 11;
|
|
95
|
+
|
|
96
|
+
export const CUSTOM_RENDERER_NODE_TYPE_MAP = /** @type {const} */ ({
|
|
97
|
+
fragment: DOCUMENT_FRAGMENT_NODE,
|
|
98
|
+
element: ELEMENT_NODE,
|
|
99
|
+
text: TEXT_NODE,
|
|
100
|
+
comment: COMMENT_NODE
|
|
101
|
+
});
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/** @import { TemplateNode } from '#client' */
|
|
2
|
+
/** @import { Renderer } from './types.js' */
|
|
3
|
+
import { DOCUMENT_FRAGMENT_NODE } from '#client/constants';
|
|
4
|
+
import { get_next_sibling, get_parent_node, insert_before, node_type } from '../dom/operations.js';
|
|
5
|
+
import { queue_micro_task } from '../dom/task.js';
|
|
6
|
+
import { set_before_effects } from '../reactivity/batch.js';
|
|
7
|
+
import { pop_renderer, push_renderer } from './state.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @typedef {{
|
|
11
|
+
* host: Renderer | null;
|
|
12
|
+
* guest: Renderer | null;
|
|
13
|
+
* host_node: TemplateNode;
|
|
14
|
+
* start: TemplateNode;
|
|
15
|
+
* end: TemplateNode;
|
|
16
|
+
* }} Crossing
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The crossings rendered into each container, in the order of their content in the container
|
|
21
|
+
* @type {Map<unknown, Crossing[]>}
|
|
22
|
+
*/
|
|
23
|
+
var containers = new Map();
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The number of containers with more than one crossing. Only those need syncing
|
|
27
|
+
*/
|
|
28
|
+
var shared_containers = 0;
|
|
29
|
+
|
|
30
|
+
var queued = false;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Registers a crossing whose content (between `start` and `end`) was appended to `container`
|
|
34
|
+
* @param {unknown} container
|
|
35
|
+
* @param {Crossing} crossing
|
|
36
|
+
*/
|
|
37
|
+
export function add_crossing(container, crossing) {
|
|
38
|
+
var crossings = containers.get(container);
|
|
39
|
+
|
|
40
|
+
if (crossings === undefined) {
|
|
41
|
+
containers.set(container, [crossing]);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
crossings.push(crossing);
|
|
46
|
+
|
|
47
|
+
if (crossings.length === 2 && shared_containers++ === 0) {
|
|
48
|
+
// updates can move host nodes (e.g. in a keyed `{#each}` block), so sync each time a
|
|
49
|
+
// batch has updated the content, before its user effects run
|
|
50
|
+
set_before_effects(sync_shared_containers);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// the host node might not be in its final position yet (e.g. it's still in a template fragment
|
|
54
|
+
// that hasn't been inserted), so sync once the current work is done. Batches sync when they
|
|
55
|
+
// update the content anyway, but initial renders don't involve one
|
|
56
|
+
if (!queued) {
|
|
57
|
+
queued = true;
|
|
58
|
+
queue_micro_task(() => {
|
|
59
|
+
queued = false;
|
|
60
|
+
sync_shared_containers();
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* @param {unknown} container
|
|
67
|
+
* @param {Crossing} crossing
|
|
68
|
+
*/
|
|
69
|
+
export function remove_crossing(container, crossing) {
|
|
70
|
+
var crossings = /** @type {Crossing[]} */ (containers.get(container));
|
|
71
|
+
crossings.splice(crossings.indexOf(crossing), 1);
|
|
72
|
+
|
|
73
|
+
if (crossings.length === 1 && --shared_containers === 0) {
|
|
74
|
+
set_before_effects(null);
|
|
75
|
+
}
|
|
76
|
+
if (crossings.length === 0) containers.delete(container);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Reorders the content of crossings that share a container to match the order of their host nodes,
|
|
81
|
+
* which can change without the crossings being recreated (e.g. in a keyed `{#each}` block)
|
|
82
|
+
*/
|
|
83
|
+
export function sync_shared_containers() {
|
|
84
|
+
// Moving the content of a crossing also moves the host nodes of the crossings nested inside it,
|
|
85
|
+
// which can be in another shared container (e.g. DOM > renderer A > renderer B) that was already
|
|
86
|
+
// synced. So if anything moved, sync again. This settles, since content only ever contains the
|
|
87
|
+
// host nodes of crossings nested deeper, and is only needed with several shared containers
|
|
88
|
+
do {
|
|
89
|
+
var moved = false;
|
|
90
|
+
|
|
91
|
+
for (var crossings of containers.values()) {
|
|
92
|
+
if (crossings.length > 1 && sync(crossings)) moved = true;
|
|
93
|
+
}
|
|
94
|
+
} while (moved && shared_containers > 1);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* @param {Crossing[]} crossings
|
|
99
|
+
* @returns {boolean} whether any content was moved
|
|
100
|
+
*/
|
|
101
|
+
function sync(crossings) {
|
|
102
|
+
var host = crossings[0].host;
|
|
103
|
+
|
|
104
|
+
// with different hosts there's no common order to follow
|
|
105
|
+
for (var crossing of crossings) {
|
|
106
|
+
if (crossing.host !== host) return false;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
var previous_renderer = push_renderer(host);
|
|
110
|
+
|
|
111
|
+
try {
|
|
112
|
+
/** @type {Map<Crossing, TemplateNode[]>} */
|
|
113
|
+
var paths = new Map();
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* The crossings whose host nodes are in the same tree, by the root of that tree. Host nodes
|
|
117
|
+
* can be in different trees (e.g. one is offscreen while an async batch is pending, or the
|
|
118
|
+
* host renderer has several roots), in which case there's no order between them
|
|
119
|
+
* @type {Map<TemplateNode, Crossing[]>}
|
|
120
|
+
*/
|
|
121
|
+
var trees = new Map();
|
|
122
|
+
|
|
123
|
+
for (crossing of crossings) {
|
|
124
|
+
var nodes = path(crossing.host_node);
|
|
125
|
+
var tree = trees.get(nodes[0]);
|
|
126
|
+
|
|
127
|
+
if (tree === undefined) trees.set(nodes[0], (tree = []));
|
|
128
|
+
|
|
129
|
+
tree.push(crossing);
|
|
130
|
+
paths.set(crossing, nodes);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
for (tree of trees.values()) {
|
|
134
|
+
tree.sort((a, b) =>
|
|
135
|
+
compare(
|
|
136
|
+
/** @type {TemplateNode[]} */ (paths.get(a)),
|
|
137
|
+
/** @type {TemplateNode[]} */ (paths.get(b))
|
|
138
|
+
)
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// order the crossings of each tree, slotting them into the positions that the crossings
|
|
143
|
+
// of that tree occupied. This way, crossings keep their place relative to those of other trees
|
|
144
|
+
var order = crossings.map((crossing) => {
|
|
145
|
+
var root = /** @type {TemplateNode[]} */ (paths.get(crossing))[0];
|
|
146
|
+
return /** @type {Crossing} */ (/** @type {Crossing[]} */ (trees.get(root)).shift());
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
if (order.every((crossing, index) => crossing === crossings[index])) return false;
|
|
150
|
+
} finally {
|
|
151
|
+
pop_renderer(previous_renderer);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
previous_renderer = push_renderer(order[0].guest);
|
|
155
|
+
|
|
156
|
+
try {
|
|
157
|
+
// working backwards from the last crossing, which stays where it is, move each crossing's
|
|
158
|
+
// content in front of the next one's (unless it's already there)
|
|
159
|
+
for (var k = order.length - 2; k >= 0; k--) {
|
|
160
|
+
var next = order[k + 1].start;
|
|
161
|
+
if (get_next_sibling(order[k].end) === next) continue;
|
|
162
|
+
|
|
163
|
+
var node = /** @type {TemplateNode | null} */ (order[k].start);
|
|
164
|
+
|
|
165
|
+
while (node !== null) {
|
|
166
|
+
var sibling = node === order[k].end ? null : get_next_sibling(node);
|
|
167
|
+
insert_before(next, node);
|
|
168
|
+
node = sibling;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
} finally {
|
|
172
|
+
pop_renderer(previous_renderer);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
crossings.splice(0, crossings.length, ...order);
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* The node and its ancestors, starting with the root (uses the current renderer)
|
|
181
|
+
* @param {TemplateNode} node
|
|
182
|
+
*/
|
|
183
|
+
function path(node) {
|
|
184
|
+
/** @type {TemplateNode[]} */
|
|
185
|
+
var nodes = [];
|
|
186
|
+
|
|
187
|
+
for (var n = /** @type {TemplateNode | null} */ (node); n !== null; n = get_parent_node(n)) {
|
|
188
|
+
nodes.push(n);
|
|
189
|
+
if (node_type(n) === DOCUMENT_FRAGMENT_NODE) break;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
return nodes.reverse();
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Compares the positions of two nodes in the same tree, given their paths (uses the current renderer)
|
|
197
|
+
* @param {TemplateNode[]} a
|
|
198
|
+
* @param {TemplateNode[]} b
|
|
199
|
+
*/
|
|
200
|
+
function compare(a, b) {
|
|
201
|
+
var i = 0;
|
|
202
|
+
while (i < a.length && i < b.length && a[i] === b[i]) i++;
|
|
203
|
+
|
|
204
|
+
// one contains the other, which host nodes of different crossings can't do
|
|
205
|
+
if (i === a.length || i === b.length) return 0;
|
|
206
|
+
|
|
207
|
+
// a[i] and b[i] are siblings: find out which comes first
|
|
208
|
+
for (var node = get_next_sibling(a[i]); node !== null; node = get_next_sibling(node)) {
|
|
209
|
+
if (node === b[i]) return -1;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return 1;
|
|
213
|
+
}
|