Skip to content

Commit 32183d1

Browse files
committed
handle cycles on context
1 parent 60b24de commit 32183d1

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

packages/svelte/src/internal/client/dev/ownership.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export function add_owner(object, owner, global = false) {
123123
}
124124
}
125125

126-
add_owner_to_object(object, owner);
126+
add_owner_to_object(object, owner, new Set());
127127
}
128128

129129
/**
@@ -140,18 +140,21 @@ export function widen_ownership(from, to) {
140140
to.owners = null;
141141
break;
142142
}
143+
143144
for (const owner of from.owners) {
144145
to.owners.add(owner);
145146
}
147+
146148
from = from.parent;
147149
}
148150
}
149151

150152
/**
151153
* @param {any} object
152154
* @param {Function} owner
155+
* @param {Set<any>} seen
153156
*/
154-
function add_owner_to_object(object, owner) {
157+
function add_owner_to_object(object, owner, seen) {
155158
const metadata = /** @type {import('#client').ProxyMetadata} */ (object?.[STATE_SYMBOL]);
156159

157160
if (metadata) {
@@ -160,6 +163,9 @@ function add_owner_to_object(object, owner) {
160163
metadata.owners.add(owner);
161164
}
162165
} else if (object && typeof object === 'object') {
166+
if (seen.has(object)) return;
167+
seen.add(object);
168+
163169
if (object[ADD_OWNER]) {
164170
// this is a class with state fields. we put this in a render effect
165171
// so that if state is replaced (e.g. `instance.name = { first, last }`)
@@ -173,12 +179,12 @@ function add_owner_to_object(object, owner) {
173179
if (proto === Object.prototype) {
174180
// recurse until we find a state proxy
175181
for (const key in object) {
176-
add_owner_to_object(object[key], owner);
182+
add_owner_to_object(object[key], owner, seen);
177183
}
178184
} else if (proto === Array.prototype) {
179185
// recurse until we find a state proxy
180186
for (let i = 0; i < object.length; i += 1) {
181-
add_owner_to_object(object[i], owner);
187+
add_owner_to_object(object[i], owner, seen);
182188
}
183189
}
184190
}

0 commit comments

Comments
 (0)