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
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/** @import { TemplateNode } from '#client' */
|
|
2
|
+
/** @import { Renderer, RendererConnection } from './types.js' */
|
|
3
|
+
import { branch, teardown } from '../reactivity/effects.js';
|
|
4
|
+
import { assign_nodes } from '../dom/template.js';
|
|
5
|
+
import {
|
|
6
|
+
append_child,
|
|
7
|
+
create_text,
|
|
8
|
+
get_next_sibling,
|
|
9
|
+
get_parent_node,
|
|
10
|
+
insert_before,
|
|
11
|
+
remove_node
|
|
12
|
+
} from '../dom/operations.js';
|
|
13
|
+
import { listen_to_delegated_events } from '../dom/elements/events.js';
|
|
14
|
+
import * as e from '../errors.js';
|
|
15
|
+
import { current_renderer, push_renderer, pop_renderer } from './state.js';
|
|
16
|
+
import { add_crossing, remove_crossing } from './containers.js';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Returns `true` if rendering content that belongs to `renderer` at the current
|
|
20
|
+
* position means crossing from one renderer into another (DOM is `null`)
|
|
21
|
+
* @param {Renderer | null} renderer
|
|
22
|
+
*/
|
|
23
|
+
export function is_crossing(renderer) {
|
|
24
|
+
return current_renderer !== renderer;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Renders content belonging to `renderer` inside content belonging to a different renderer.
|
|
29
|
+
*
|
|
30
|
+
* The crossing is represented in the host tree by a single host-owned node, so
|
|
31
|
+
* that the host can move and remove it like any other node without ever walking
|
|
32
|
+
* siblings that belong to another renderer. The guest content is rendered into a
|
|
33
|
+
* container owned by `renderer`, between two guest-owned markers.
|
|
34
|
+
*
|
|
35
|
+
* @template T
|
|
36
|
+
* @param {Renderer | null} renderer The renderer the content belongs to (`null` for DOM)
|
|
37
|
+
* @param {TemplateNode} anchor An anchor owned by the current (host) renderer
|
|
38
|
+
* @param {(anchor: TemplateNode) => T} render
|
|
39
|
+
* @returns {T}
|
|
40
|
+
*/
|
|
41
|
+
export function cross(renderer, anchor, render) {
|
|
42
|
+
var host = current_renderer;
|
|
43
|
+
var parent = get_parent_node(anchor);
|
|
44
|
+
|
|
45
|
+
/** @type {RendererConnection<unknown, unknown>} */
|
|
46
|
+
var connection;
|
|
47
|
+
|
|
48
|
+
if (renderer?.embed) {
|
|
49
|
+
connection = renderer.embed({ host, parent, anchor });
|
|
50
|
+
} else if (host?.host) {
|
|
51
|
+
connection = host.host({ guest: renderer, parent, anchor });
|
|
52
|
+
} else {
|
|
53
|
+
e.renderer_cannot_cross(
|
|
54
|
+
renderer === null ? 'the DOM' : 'a custom renderer',
|
|
55
|
+
host === null
|
|
56
|
+
? 'the DOM'
|
|
57
|
+
: render !== null
|
|
58
|
+
? 'a different custom renderer'
|
|
59
|
+
: 'a custom renderer'
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// the host side of the crossing: a single node, inserted using the host renderer
|
|
64
|
+
var host_node = /** @type {TemplateNode} */ (connection.node ?? create_text());
|
|
65
|
+
insert_before(anchor, host_node);
|
|
66
|
+
assign_nodes(host_node, host_node);
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The guest-owned markers, set once they are both in the container
|
|
70
|
+
* @type {TemplateNode | null}
|
|
71
|
+
*/
|
|
72
|
+
var start = null;
|
|
73
|
+
/** @type {TemplateNode | null} */
|
|
74
|
+
var end = null;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Stops listening for delegated events on a DOM container
|
|
78
|
+
* @type {(() => void) | null}
|
|
79
|
+
*/
|
|
80
|
+
var stop_listening = null;
|
|
81
|
+
|
|
82
|
+
/** @type {T} */
|
|
83
|
+
var result;
|
|
84
|
+
|
|
85
|
+
var previous_renderer = push_renderer(renderer);
|
|
86
|
+
|
|
87
|
+
try {
|
|
88
|
+
// the guest side: markers inside the container, inserted using the guest renderer
|
|
89
|
+
var first = create_text();
|
|
90
|
+
var last = create_text();
|
|
91
|
+
var container = /** @type {Node} */ (connection.container);
|
|
92
|
+
append_child(container, first);
|
|
93
|
+
append_child(container, last);
|
|
94
|
+
start = first;
|
|
95
|
+
end = last;
|
|
96
|
+
|
|
97
|
+
// several crossings can share a container, in which case their content is kept in the
|
|
98
|
+
// order of their host nodes (see `sync_shared_containers`)
|
|
99
|
+
var crossing = { host, guest: renderer, host_node, start, end };
|
|
100
|
+
add_crossing(container, crossing);
|
|
101
|
+
|
|
102
|
+
if (renderer === null) {
|
|
103
|
+
// DOM content compiles event handlers like `onclick` to delegated handlers, which
|
|
104
|
+
// only run if the event reaches a delegation root. The container is usually outside
|
|
105
|
+
// the DOM mount target (or there is no DOM mount at all), so make it a root
|
|
106
|
+
stop_listening = listen_to_delegated_events(container);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
branch(() => {
|
|
110
|
+
result = render(last);
|
|
111
|
+
});
|
|
112
|
+
} finally {
|
|
113
|
+
pop_renderer(previous_renderer);
|
|
114
|
+
|
|
115
|
+
// Registered in `finally` so that the connection is cleaned up even if rendering the
|
|
116
|
+
// guest throws (e.g. inside a `<svelte:boundary>`), and created after the guest branch
|
|
117
|
+
// so that it runs after the guest's own teardown logic. Removing everything between
|
|
118
|
+
// the markers is idempotent, and is necessary even if the host node is being removed,
|
|
119
|
+
// since the container could be shared between several crossings (e.g. multiple scenes
|
|
120
|
+
// rendered into the same canvas)
|
|
121
|
+
teardown(() => {
|
|
122
|
+
if (crossing !== undefined) remove_crossing(container, crossing);
|
|
123
|
+
|
|
124
|
+
var previous_renderer = push_renderer(renderer);
|
|
125
|
+
|
|
126
|
+
try {
|
|
127
|
+
var node = start;
|
|
128
|
+
|
|
129
|
+
while (node !== null) {
|
|
130
|
+
var next = node === end ? null : get_next_sibling(node);
|
|
131
|
+
remove_node(/** @type {ChildNode} */ (node));
|
|
132
|
+
node = next;
|
|
133
|
+
}
|
|
134
|
+
} finally {
|
|
135
|
+
pop_renderer(previous_renderer);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
stop_listening?.();
|
|
139
|
+
connection.destroy?.();
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// @ts-expect-error assigned synchronously by the branch
|
|
144
|
+
return result;
|
|
145
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/** @import { Renderer, RendererNodes } from "./types.js" */
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Creates a custom renderer.
|
|
5
|
+
*
|
|
6
|
+
* The types of the renderer's nodes are inferred from the return types of `createFragment`,
|
|
7
|
+
* `createElement`, `createTextNode` and `createComment`, as long as those methods come before
|
|
8
|
+
* the others. Alternatively, pass them as the type argument:
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* const renderer = createRenderer<{ fragment: MyFragment; element: MyElement; text: MyText; comment: MyComment }>({
|
|
12
|
+
* // ...
|
|
13
|
+
* });
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* Only the first type parameter is meant to be passed; the others exist so that the node types can be
|
|
17
|
+
* inferred from the methods (TypeScript can't infer the record itself from them).
|
|
18
|
+
* @template {RendererNodes} [N=RendererNodes]
|
|
19
|
+
* @template {object} [TFragment=N['fragment']]
|
|
20
|
+
* @template {object} [TElement=N['element']]
|
|
21
|
+
* @template {object} [TText=N['text']]
|
|
22
|
+
* @template {object} [TComment=N['comment']]
|
|
23
|
+
* @param {Renderer<{ fragment: TFragment; element: TElement; text: TText; comment: TComment }>} renderer
|
|
24
|
+
* @returns {Renderer<{ fragment: TFragment; element: TElement; text: TText; comment: TComment }>}
|
|
25
|
+
*/
|
|
26
|
+
export function createRenderer(renderer) {
|
|
27
|
+
return renderer;
|
|
28
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/** @import { Renderer } from './types.js'; */
|
|
2
|
+
|
|
3
|
+
import { hydrating, set_hydrating } from '../dom/hydration.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @type {Renderer<any> | null}
|
|
7
|
+
*/
|
|
8
|
+
export let current_renderer = null;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @param {Renderer<any> | null} value
|
|
12
|
+
*/
|
|
13
|
+
export function set_renderer(value) {
|
|
14
|
+
current_renderer = value;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** How many `push_renderer` calls haven't been matched by a `pop_renderer` yet */
|
|
18
|
+
var depth = 0;
|
|
19
|
+
|
|
20
|
+
/** @type {number[]} Depths of pushes that suspended hydration, including nested hydrate calls */
|
|
21
|
+
var suspended_hydration_at = [];
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Makes `value` the current renderer (`null` for the DOM), returning the previous one.
|
|
25
|
+
* Every call must be matched by a call to `pop_renderer` with the returned value
|
|
26
|
+
* (in a `finally` block, if anything in between can throw).
|
|
27
|
+
*
|
|
28
|
+
* Custom renderers never hydrate, so hydration is suspended while one is active.
|
|
29
|
+
* This doesn't allocate, since it runs for every effect when custom renderers are used.
|
|
30
|
+
* @param {Renderer<any> | null} value
|
|
31
|
+
* @returns {Renderer<any> | null}
|
|
32
|
+
*/
|
|
33
|
+
export function push_renderer(value) {
|
|
34
|
+
var previous = current_renderer;
|
|
35
|
+
current_renderer = value;
|
|
36
|
+
depth += 1;
|
|
37
|
+
|
|
38
|
+
// this is to allow hydration code to treeshake
|
|
39
|
+
if (hydrating && value !== null) {
|
|
40
|
+
set_hydrating(false);
|
|
41
|
+
suspended_hydration_at.push(depth);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return previous;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Restores the renderer that was current before the matching `push_renderer` call
|
|
49
|
+
* @param {Renderer<any> | null} previous The value returned by `push_renderer`
|
|
50
|
+
*/
|
|
51
|
+
export function pop_renderer(previous) {
|
|
52
|
+
if (suspended_hydration_at[suspended_hydration_at.length - 1] === depth) {
|
|
53
|
+
suspended_hydration_at.pop();
|
|
54
|
+
set_hydrating(true);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
depth -= 1;
|
|
58
|
+
current_renderer = previous;
|
|
59
|
+
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { remove_node } from '../dom/operations.js';
|
|
2
|
+
|
|
1
3
|
/** @type {Map<String, Set<HTMLStyleElement>>} */
|
|
2
4
|
var all_styles = new Map();
|
|
3
5
|
|
|
@@ -24,7 +26,7 @@ export function cleanup_styles(hash) {
|
|
|
24
26
|
if (!styles) return;
|
|
25
27
|
|
|
26
28
|
for (const style of styles) {
|
|
27
|
-
style
|
|
29
|
+
remove_node(style);
|
|
28
30
|
}
|
|
29
31
|
|
|
30
32
|
all_styles.delete(hash);
|
|
@@ -113,11 +113,11 @@ export function log_effect_tree(effect, highlighted = [], depth = 0, is_reachabl
|
|
|
113
113
|
console.groupEnd();
|
|
114
114
|
}
|
|
115
115
|
|
|
116
|
-
if (effect.
|
|
116
|
+
if (effect.edges !== null) {
|
|
117
117
|
// eslint-disable-next-line no-console
|
|
118
118
|
console.groupCollapsed('%cdeps', 'font-weight: normal');
|
|
119
119
|
|
|
120
|
-
for (const dep of effect.
|
|
120
|
+
for (const dep of effect.edges) {
|
|
121
121
|
log_dep(dep);
|
|
122
122
|
}
|
|
123
123
|
|
|
@@ -163,8 +163,8 @@ function log_dep(dep) {
|
|
|
163
163
|
untrack(() => snapshot(derived.v))
|
|
164
164
|
);
|
|
165
165
|
|
|
166
|
-
if (derived.
|
|
167
|
-
for (const d of derived.
|
|
166
|
+
if (derived.edges) {
|
|
167
|
+
for (const d of derived.edges) {
|
|
168
168
|
log_dep(d);
|
|
169
169
|
}
|
|
170
170
|
}
|
|
@@ -459,10 +459,10 @@ export function log_inconsistent_branches(effect) {
|
|
|
459
459
|
console.log('%cfn:', 'font-weight: bold', branch.effect.fn);
|
|
460
460
|
}
|
|
461
461
|
|
|
462
|
-
if (branch.effect.
|
|
462
|
+
if (branch.effect.edges !== null) {
|
|
463
463
|
// eslint-disable-next-line no-console
|
|
464
464
|
console.groupCollapsed('%cdeps', 'font-weight: normal');
|
|
465
|
-
for (const dep of branch.effect.
|
|
465
|
+
for (const dep of branch.effect.edges) {
|
|
466
466
|
log_dep(dep);
|
|
467
467
|
}
|
|
468
468
|
// eslint-disable-next-line no-console
|
|
@@ -3,6 +3,7 @@ import { COMMENT_NODE, DOCUMENT_FRAGMENT_NODE, ELEMENT_NODE } from '#client/cons
|
|
|
3
3
|
import { HYDRATION_END, HYDRATION_START } from '../../../constants.js';
|
|
4
4
|
import { hydrating } from '../dom/hydration.js';
|
|
5
5
|
import { component_context, dev_stack } from '../context.js';
|
|
6
|
+
import { get_first_child, get_next_sibling, get_node_value, node_type } from '../dom/operations.js';
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* Assign locations immediately after cloning, before blocks or child components insert elements.
|
|
@@ -18,7 +19,7 @@ export function add_locations(fn, filename, locations) {
|
|
|
18
19
|
|
|
19
20
|
if (!hydrating) {
|
|
20
21
|
assign_locations(
|
|
21
|
-
dom
|
|
22
|
+
node_type(dom) === DOCUMENT_FRAGMENT_NODE ? get_first_child(dom) : dom,
|
|
22
23
|
filename,
|
|
23
24
|
locations
|
|
24
25
|
);
|
|
@@ -49,7 +50,7 @@ function assign_location(element, filename, location) {
|
|
|
49
50
|
};
|
|
50
51
|
|
|
51
52
|
if (location[2]) {
|
|
52
|
-
assign_locations(element
|
|
53
|
+
assign_locations(get_first_child(element), filename, location[2]);
|
|
53
54
|
}
|
|
54
55
|
}
|
|
55
56
|
|
|
@@ -63,17 +64,18 @@ function assign_locations(node, filename, locations) {
|
|
|
63
64
|
var depth = 0;
|
|
64
65
|
|
|
65
66
|
while (node && i < locations.length) {
|
|
66
|
-
if (hydrating && node
|
|
67
|
+
if (hydrating && node_type(node) === COMMENT_NODE) {
|
|
67
68
|
var comment = /** @type {Comment} */ (node);
|
|
68
|
-
|
|
69
|
-
|
|
69
|
+
const data = get_node_value(comment) ?? '';
|
|
70
|
+
if (data[0] === HYDRATION_START) depth += 1;
|
|
71
|
+
else if (data[0] === HYDRATION_END) depth -= 1;
|
|
70
72
|
}
|
|
71
73
|
|
|
72
74
|
// @ts-expect-error
|
|
73
|
-
if (depth === 0 && node
|
|
75
|
+
if (depth === 0 && node_type(node) === ELEMENT_NODE && !node.__svelte_meta) {
|
|
74
76
|
assign_location(/** @type {Element} */ (node), filename, locations[i++]);
|
|
75
77
|
}
|
|
76
78
|
|
|
77
|
-
node = node
|
|
79
|
+
node = get_next_sibling(node);
|
|
78
80
|
}
|
|
79
81
|
}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
/** @import { Derived, Reaction, Value } from '#client' */
|
|
1
|
+
/** @import { BatchRecord, BatchValue, Derived, DerivedRecord, Reaction, Value } from '#client' */
|
|
2
2
|
import { UNINITIALIZED } from '../../../constants.js';
|
|
3
3
|
import { snapshot } from '../../shared/clone.js';
|
|
4
4
|
import { DERIVED, ASYNC, PROXY_PATH_SYMBOL, STATE_SYMBOL } from '#client/constants';
|
|
5
5
|
import { effect_tracking } from '../reactivity/effects.js';
|
|
6
|
-
import {
|
|
6
|
+
import { batch_values, view_batch } from '../reactivity/batch.js';
|
|
7
|
+
import { get_view } from '../reactivity/deriveds.js';
|
|
8
|
+
import { active_reaction, run_of, untrack, version_of } from '../runtime.js';
|
|
7
9
|
|
|
8
10
|
/**
|
|
9
11
|
* @typedef {{
|
|
@@ -15,19 +17,67 @@ import { active_reaction, untrack } from '../runtime.js';
|
|
|
15
17
|
export let tracing_expressions = null;
|
|
16
18
|
|
|
17
19
|
/**
|
|
20
|
+
* The value of `signal`, with its version, as the active view of the world sees it (see `get`): for a
|
|
21
|
+
* derived, the run of it that applies — a record in the batch, or the derived itself — or an assigned value
|
|
18
22
|
* @param {Value} signal
|
|
19
|
-
* @
|
|
23
|
+
* @returns {Value | DerivedRecord | BatchValue}
|
|
20
24
|
*/
|
|
21
|
-
function
|
|
22
|
-
|
|
25
|
+
function view_of(signal) {
|
|
26
|
+
if (batch_values === null) return signal;
|
|
27
|
+
if ((signal.f & DERIVED) !== 0) return get_view(/** @type {Derived} */ (signal));
|
|
28
|
+
return batch_values.get(signal) ?? signal;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The run of `reaction` that applies in the active view of the world (see `run_of`), which for a derived
|
|
33
|
+
* in the view of a batch is its record in the batch if it has one (see `get_view`). While the reaction
|
|
34
|
+
* runs, that's its previous run
|
|
35
|
+
* @param {Reaction} reaction
|
|
36
|
+
* @returns {Reaction | BatchRecord}
|
|
37
|
+
*/
|
|
38
|
+
function run_in_view(reaction) {
|
|
39
|
+
if ((reaction.f & DERIVED) !== 0 && batch_values !== null) {
|
|
40
|
+
return view_batch?.get_record(reaction) ?? reaction;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return run_of(reaction);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Whether `signal` changed in the active view of the world since the traced reaction last ran. If its previous
|
|
48
|
+
* run read it, that's whether the version it saw is no longer current. Otherwise (it's a dependency of a derived
|
|
49
|
+
* the reaction read, or the reaction didn't read it before), whether it was written since `since`: the one place
|
|
50
|
+
* where versions are compared by order, which is only a heuristic — a view can undo writes, which gives values
|
|
51
|
+
* older versions again
|
|
52
|
+
* @param {Value} signal
|
|
53
|
+
* @param {Reaction | BatchRecord} run the previous run of the traced reaction (see `run_in_view`)
|
|
54
|
+
* @param {number} since the version of the reaction's previous run (`0` if there's none)
|
|
55
|
+
*/
|
|
56
|
+
function is_changed(signal, run, since) {
|
|
57
|
+
var i = run.deps?.indexOf(signal) ?? -1;
|
|
58
|
+
var seen = i === -1 ? undefined : run.dv?.[i];
|
|
59
|
+
|
|
60
|
+
if (seen !== undefined) return version_of(signal) !== seen;
|
|
61
|
+
|
|
62
|
+
return since === 0 || version_of(signal) > since;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* @param {Value} signal
|
|
67
|
+
* @param {TraceEntry | undefined} entry
|
|
68
|
+
* @param {Reaction | BatchRecord} run the previous run of the traced reaction (see `run_in_view`)
|
|
69
|
+
* @param {number} since the version of the reaction's previous run (`0` if there's none)
|
|
70
|
+
*/
|
|
71
|
+
function log_entry(signal, entry, run, since) {
|
|
72
|
+
const view = view_of(signal);
|
|
73
|
+
const value = view.v;
|
|
23
74
|
|
|
24
75
|
if (value === UNINITIALIZED) {
|
|
25
76
|
return;
|
|
26
77
|
}
|
|
27
78
|
|
|
28
79
|
const type = get_type(signal);
|
|
29
|
-
const
|
|
30
|
-
const dirty = signal.wv > current_reaction.wv || current_reaction.wv === 0;
|
|
80
|
+
const dirty = is_changed(signal, run, since);
|
|
31
81
|
const style = dirty
|
|
32
82
|
? 'color: CornflowerBlue; font-weight: bold'
|
|
33
83
|
: 'color: grey; font-weight: normal';
|
|
@@ -42,10 +92,13 @@ function log_entry(signal, entry) {
|
|
|
42
92
|
: value
|
|
43
93
|
);
|
|
44
94
|
|
|
45
|
-
if (
|
|
46
|
-
const deps = new Set(
|
|
95
|
+
if ((signal.f & DERIVED) !== 0) {
|
|
96
|
+
const deps = new Set(
|
|
97
|
+
('deps' in view ? view : run_in_view(/** @type {Derived} */ (signal))).deps
|
|
98
|
+
);
|
|
99
|
+
|
|
47
100
|
for (const dep of deps) {
|
|
48
|
-
log_entry(dep);
|
|
101
|
+
log_entry(dep, undefined, run, since);
|
|
49
102
|
}
|
|
50
103
|
}
|
|
51
104
|
|
|
@@ -111,10 +164,13 @@ export function trace(label, fn) {
|
|
|
111
164
|
console.group(`${prefix} %c(${time}ms)`, 'color: grey');
|
|
112
165
|
|
|
113
166
|
var entries = tracing_expressions.entries;
|
|
167
|
+
var reaction = /** @type {Reaction} */ (active_reaction);
|
|
168
|
+
var run = run_in_view(reaction);
|
|
169
|
+
var since = run.wv ?? reaction.wv;
|
|
114
170
|
|
|
115
171
|
untrack(() => {
|
|
116
172
|
for (const [signal, traces] of entries) {
|
|
117
|
-
log_entry(signal, traces);
|
|
173
|
+
log_entry(signal, traces, run, since);
|
|
118
174
|
}
|
|
119
175
|
});
|
|
120
176
|
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
import { current_renderer } from '../custom-renderer/state.js';
|
|
1
2
|
import * as e from '../errors.js';
|
|
2
3
|
/**
|
|
3
4
|
* @param {Node} anchor
|
|
4
5
|
* @param {...(()=>any)[]} args
|
|
5
6
|
*/
|
|
6
7
|
export function validate_snippet_args(anchor, ...args) {
|
|
7
|
-
if (typeof anchor !== 'object' || !(anchor instanceof Node)) {
|
|
8
|
+
if (!current_renderer && (typeof anchor !== 'object' || !(anchor instanceof Node))) {
|
|
8
9
|
e.invalid_snippet_arguments();
|
|
9
10
|
}
|
|
10
11
|
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
/** @import {
|
|
1
|
+
/** @import { TemplateNode, Value } from '#client' */
|
|
2
|
+
/** @import { Blocker } from '../../reactivity/async.js' */
|
|
2
3
|
import { flatten } from '../../reactivity/async.js';
|
|
3
4
|
import { get } from '../../runtime.js';
|
|
4
5
|
import {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/** @import { Source, TemplateNode } from '#client' */
|
|
1
|
+
/** @import { Effect, Source, TemplateNode } from '#client' */
|
|
2
2
|
import { is_promise } from '../../../shared/utils.js';
|
|
3
3
|
import { block } from '../../reactivity/effects.js';
|
|
4
4
|
import { internal_set, mutable_source, source } from '../../reactivity/sources.js';
|
|
@@ -10,12 +10,15 @@ import {
|
|
|
10
10
|
set_hydrating
|
|
11
11
|
} from '../hydration.js';
|
|
12
12
|
import { queue_micro_task } from '../task.js';
|
|
13
|
+
import { get_node_value, should_defer_append } from '../operations.js';
|
|
13
14
|
import { HYDRATION_START_ELSE, UNINITIALIZED } from '../../../../constants.js';
|
|
15
|
+
import { ASYNC, DESTROYED, EFFECT_PRESERVED } from '#client/constants';
|
|
14
16
|
import { is_runes } from '../../context.js';
|
|
15
17
|
import { Batch, current_batch, flushSync, is_flushing_sync } from '../../reactivity/batch.js';
|
|
16
18
|
import { BranchManager } from './branches.js';
|
|
17
|
-
import { capture, unset_context } from '../../reactivity/async.js';
|
|
19
|
+
import { capture, merge_target, unset_context } from '../../reactivity/async.js';
|
|
18
20
|
import { DEV } from 'esm-env';
|
|
21
|
+
import { active_effect } from '../../runtime.js';
|
|
19
22
|
|
|
20
23
|
const PENDING = 0;
|
|
21
24
|
const THEN = 1;
|
|
@@ -43,6 +46,11 @@ export function await_block(node, get_input, pending_fn, then_fn, catch_fn) {
|
|
|
43
46
|
var value = runes ? source(v) : mutable_source(v, false, false);
|
|
44
47
|
var error = runes ? source(v) : mutable_source(v, false, false);
|
|
45
48
|
|
|
49
|
+
// like the result of an async expression, the states of a run belong to the batch
|
|
50
|
+
// that holds them: another batch's commit doesn't overwrite them (see `BatchValue#async`)
|
|
51
|
+
value.f |= ASYNC;
|
|
52
|
+
error.f |= ASYNC;
|
|
53
|
+
|
|
46
54
|
if (DEV) {
|
|
47
55
|
value.label = '{#await ...} value';
|
|
48
56
|
error.label = '{#await ...} error';
|
|
@@ -50,15 +58,82 @@ export function await_block(node, get_input, pending_fn, then_fn, catch_fn) {
|
|
|
50
58
|
|
|
51
59
|
var branches = new BranchManager(node);
|
|
52
60
|
|
|
61
|
+
/** The latest run of the block in the real world */
|
|
62
|
+
var current = {};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The latest run of the block in each fork that ran it. Once the fork is committed, that's a run of the real
|
|
66
|
+
* world (and so are its later runs), until a later run in the real world supersedes it (`null`)
|
|
67
|
+
* @type {Map<Batch, {} | null>}
|
|
68
|
+
*/
|
|
69
|
+
var forks = new Map();
|
|
70
|
+
|
|
71
|
+
/** @param {Batch} batch */
|
|
72
|
+
var commit = (batch) => {
|
|
73
|
+
var run = forks.get(batch);
|
|
74
|
+
if (run) current = run;
|
|
75
|
+
forks.delete(batch);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/** @param {Batch} batch */
|
|
79
|
+
var discard = (batch) => {
|
|
80
|
+
forks.delete(batch);
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* @param {{}} run
|
|
85
|
+
* @param {Batch} batch
|
|
86
|
+
*/
|
|
87
|
+
var is_latest = (run, batch) => {
|
|
88
|
+
if (forks.has(batch)) return forks.get(batch) === run;
|
|
89
|
+
if (run !== current) return false;
|
|
90
|
+
|
|
91
|
+
// a fork that was committed since supersedes it
|
|
92
|
+
for (const [b, r] of forks) {
|
|
93
|
+
if (!b.is_fork && r !== null) return false;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return true;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* The batch that the states of a run in `batch` go into, if they aren't their own batch: the fork, so that they
|
|
101
|
+
* don't show before it's committed — and once it is, the batch it became, so that they land along with the rest
|
|
102
|
+
* of its changes while those are pending (its later runs, too: they replace the states it holds)
|
|
103
|
+
* @param {Batch} batch
|
|
104
|
+
*/
|
|
105
|
+
var owner = (batch) => {
|
|
106
|
+
var target = merge_target(batch);
|
|
107
|
+
return forks.has(batch) && target.linked ? target : null;
|
|
108
|
+
};
|
|
109
|
+
|
|
53
110
|
block(() => {
|
|
111
|
+
var effect = /** @type {Effect} */ (active_effect);
|
|
54
112
|
var batch = /** @type {Batch} */ (current_batch);
|
|
55
113
|
var input = get_input();
|
|
114
|
+
var run = {};
|
|
115
|
+
|
|
116
|
+
// (not the first run, which creates the block for every batch's view, see `should_defer_append`)
|
|
117
|
+
var fork = batch.is_fork && should_defer_append();
|
|
118
|
+
|
|
119
|
+
if (fork || forks.has(batch)) {
|
|
120
|
+
forks.set(batch, run);
|
|
121
|
+
batch.oncommit(commit);
|
|
122
|
+
batch.ondiscard(discard);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (!fork) {
|
|
126
|
+
for (const b of forks.keys()) {
|
|
127
|
+
if (!b.is_fork && b !== batch) forks.set(b, null);
|
|
128
|
+
}
|
|
56
129
|
|
|
57
|
-
|
|
130
|
+
current = run;
|
|
131
|
+
}
|
|
58
132
|
|
|
59
133
|
/** Whether or not there was a hydration mismatch. Needs to be a `let` or else it isn't treeshaken out */
|
|
60
134
|
// @ts-ignore coercing `node` to a `Comment` causes TypeScript and Prettier to fight
|
|
61
|
-
let mismatch =
|
|
135
|
+
let mismatch =
|
|
136
|
+
hydrating && is_promise(input) === (get_node_value(node) === HYDRATION_START_ELSE);
|
|
62
137
|
|
|
63
138
|
if (mismatch) {
|
|
64
139
|
// Hydration mismatch: remove everything inside the anchor and start fresh
|
|
@@ -71,15 +146,33 @@ export function await_block(node, get_input, pending_fn, then_fn, catch_fn) {
|
|
|
71
146
|
var resolved = false;
|
|
72
147
|
|
|
73
148
|
/**
|
|
74
|
-
* @param {() => void} fn
|
|
149
|
+
* @param {(set: (source: Source, value: any) => void) => void} fn
|
|
75
150
|
*/
|
|
76
151
|
const resolve = (fn) => {
|
|
77
|
-
if (
|
|
152
|
+
if ((effect.f & DESTROYED) !== 0 || !is_latest(run, batch)) return;
|
|
78
153
|
|
|
79
154
|
resolved = true;
|
|
155
|
+
|
|
156
|
+
var target = owner(batch);
|
|
157
|
+
|
|
158
|
+
if (target !== null) {
|
|
159
|
+
var previous = current_batch;
|
|
160
|
+
restore(target);
|
|
161
|
+
|
|
162
|
+
try {
|
|
163
|
+
fn((source, v) => /** @type {Batch} */ (target).capture(source, v, source.equals(v)));
|
|
164
|
+
} finally {
|
|
165
|
+
unset_context(false);
|
|
166
|
+
target.flush();
|
|
167
|
+
if (previous?.linked) previous.activate();
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
|
|
80
173
|
// We don't want to restore the previous batch here; {#await} blocks don't follow the async logic
|
|
81
174
|
// we have elsewhere, instead pending/resolve/fail states are each their own batch so to speak.
|
|
82
|
-
restore(
|
|
175
|
+
restore(null);
|
|
83
176
|
// ...but it might still be set here. That means a `save(...)` has restored it — but that batch will
|
|
84
177
|
// likely already have been committed by the time it resolves, and this resolve should be processed
|
|
85
178
|
// in a separate batch. We're not using batch.deactivate()/activate() above because get_input()
|
|
@@ -92,7 +185,7 @@ export function await_block(node, get_input, pending_fn, then_fn, catch_fn) {
|
|
|
92
185
|
Batch.ensure();
|
|
93
186
|
|
|
94
187
|
try {
|
|
95
|
-
fn();
|
|
188
|
+
fn(internal_set);
|
|
96
189
|
} finally {
|
|
97
190
|
unset_context(false);
|
|
98
191
|
|
|
@@ -104,19 +197,19 @@ export function await_block(node, get_input, pending_fn, then_fn, catch_fn) {
|
|
|
104
197
|
|
|
105
198
|
input.then(
|
|
106
199
|
(v) => {
|
|
107
|
-
resolve(() => {
|
|
108
|
-
|
|
200
|
+
resolve((set) => {
|
|
201
|
+
set(value, v);
|
|
109
202
|
branches.ensure(THEN, then_fn && ((target) => then_fn(target, value)));
|
|
110
203
|
});
|
|
111
204
|
},
|
|
112
205
|
(e) => {
|
|
113
|
-
resolve(() => {
|
|
114
|
-
|
|
206
|
+
resolve((set) => {
|
|
207
|
+
set(error, e);
|
|
115
208
|
branches.ensure(CATCH, catch_fn && ((target) => catch_fn(target, error)));
|
|
116
209
|
|
|
117
210
|
if (!catch_fn) {
|
|
118
211
|
// Rethrow the error if no catch block exists
|
|
119
|
-
throw
|
|
212
|
+
throw e;
|
|
120
213
|
}
|
|
121
214
|
});
|
|
122
215
|
}
|
|
@@ -136,7 +229,9 @@ export function await_block(node, get_input, pending_fn, then_fn, catch_fn) {
|
|
|
136
229
|
});
|
|
137
230
|
}
|
|
138
231
|
} else {
|
|
139
|
-
|
|
232
|
+
if (owner(batch) === batch) batch.capture(value, input, value.equals(input));
|
|
233
|
+
else internal_set(value, input);
|
|
234
|
+
|
|
140
235
|
branches.ensure(THEN, then_fn && ((target) => then_fn(target, value)));
|
|
141
236
|
}
|
|
142
237
|
|
|
@@ -144,9 +239,5 @@ export function await_block(node, get_input, pending_fn, then_fn, catch_fn) {
|
|
|
144
239
|
// continue in hydration mode
|
|
145
240
|
set_hydrating(true);
|
|
146
241
|
}
|
|
147
|
-
|
|
148
|
-
return () => {
|
|
149
|
-
destroyed = true;
|
|
150
|
-
};
|
|
151
|
-
});
|
|
242
|
+
}, EFFECT_PRESERVED); // states are selected asynchronously: keep the effect even without dependencies
|
|
152
243
|
}
|