Skip to content

Commit 6f43265

Browse files
committed
lazily create deep state with parent reaction
1 parent 14ca327 commit 6f43265

File tree

1 file changed

+17
-7
lines changed
  • packages/svelte/src/internal/client

1 file changed

+17
-7
lines changed

packages/svelte/src/internal/client/proxy.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/** @import { ProxyMetadata, Source } from '#client' */
22
import { DEV } from 'esm-env';
3-
import { get, active_effect } from './runtime.js';
3+
import { get, active_effect, active_reaction, set_active_reaction } from './runtime.js';
44
import { component_context } from './context.js';
55
import {
66
array_prototype,
@@ -42,6 +42,16 @@ export function proxy(value, parent = null, prev) {
4242
var version = source(0);
4343

4444
var stack = DEV && tracing_mode_flag ? get_stack('CreatedAt') : null;
45+
var reaction = active_reaction;
46+
47+
/** @param {any} value */
48+
var child = (value) => {
49+
var previous_reaction = active_reaction;
50+
set_active_reaction(reaction);
51+
var s = source(value, stack);
52+
set_active_reaction(previous_reaction);
53+
return s;
54+
};
4555

4656
if (is_proxied_array) {
4757
// We need to create the length source eagerly to ensure that
@@ -92,7 +102,7 @@ export function proxy(value, parent = null, prev) {
92102
var s = sources.get(prop);
93103

94104
if (s === undefined) {
95-
s = source(descriptor.value, stack);
105+
s = child(descriptor.value);
96106
sources.set(prop, s);
97107
} else {
98108
set(s, proxy(descriptor.value, metadata));
@@ -106,7 +116,7 @@ export function proxy(value, parent = null, prev) {
106116

107117
if (s === undefined) {
108118
if (prop in target) {
109-
sources.set(prop, source(UNINITIALIZED, stack));
119+
sources.set(prop, child(UNINITIALIZED));
110120
}
111121
} else {
112122
// When working with arrays, we need to also ensure we update the length when removing
@@ -140,7 +150,7 @@ export function proxy(value, parent = null, prev) {
140150

141151
// create a source, but only if it's an own property and not a prototype property
142152
if (s === undefined && (!exists || get_descriptor(target, prop)?.writable)) {
143-
s = source(proxy(exists ? target[prop] : UNINITIALIZED, metadata), stack);
153+
s = child(proxy(exists ? target[prop] : UNINITIALIZED, metadata));
144154
sources.set(prop, s);
145155
}
146156

@@ -208,7 +218,7 @@ export function proxy(value, parent = null, prev) {
208218
(active_effect !== null && (!has || get_descriptor(target, prop)?.writable))
209219
) {
210220
if (s === undefined) {
211-
s = source(has ? proxy(target[prop], metadata) : UNINITIALIZED, stack);
221+
s = child(has ? proxy(target[prop], metadata) : UNINITIALIZED);
212222
sources.set(prop, s);
213223
}
214224

@@ -235,7 +245,7 @@ export function proxy(value, parent = null, prev) {
235245
// If the item exists in the original, we need to create a uninitialized source,
236246
// else a later read of the property would result in a source being created with
237247
// the value of the original item at that index.
238-
other_s = source(UNINITIALIZED, stack);
248+
other_s = child(UNINITIALIZED);
239249
sources.set(i + '', other_s);
240250
}
241251
}
@@ -247,7 +257,7 @@ export function proxy(value, parent = null, prev) {
247257
// object property before writing to that property.
248258
if (s === undefined) {
249259
if (!has || get_descriptor(target, prop)?.writable) {
250-
s = source(undefined, stack);
260+
s = child(undefined);
251261
set(s, proxy(value, metadata));
252262
sources.set(prop, s);
253263
}

0 commit comments

Comments
 (0)