Skip to content

Commit cee6204

Browse files
committed
fix: cleanup each items in legacy mode
1 parent f0497b1 commit cee6204

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

.changeset/honest-rabbits-glow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: cleanup `each` items in legacy mode

packages/svelte/src/internal/client/dom/blocks/each.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ import {
3232
pause_effect,
3333
resume_effect
3434
} from '../../reactivity/effects.js';
35-
import { source, mutable_source, internal_set } from '../../reactivity/sources.js';
35+
import {
36+
source,
37+
mutable_source,
38+
internal_set,
39+
remove_from_legacy_sources
40+
} from '../../reactivity/sources.js';
3641
import { array_from, is_array } from '../../../shared/utils.js';
3742
import { INERT } from '#client/constants';
3843
import { queue_micro_task } from '../task.js';
@@ -549,7 +554,16 @@ function create_item(
549554
current_each_item = item;
550555

551556
try {
552-
item.e = branch(() => render_fn(anchor, v, i, get_collection), hydrating);
557+
item.e = branch(() => {
558+
render_fn(anchor, v, i, get_collection);
559+
// we only need to clean this up if the item is reactive and mutable
560+
// because that's when we add the source to the context preventing garbage collection of it
561+
if (reactive && mutable) {
562+
return () => {
563+
remove_from_legacy_sources(item.v);
564+
};
565+
}
566+
}, hydrating);
553567

554568
item.e.prev = prev && prev.e;
555569
item.e.next = next && next.e;

packages/svelte/src/internal/client/reactivity/sources.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,15 @@ export function mutable_source(initial_value, immutable = false) {
112112
return s;
113113
}
114114

115+
/**
116+
* @param {Source} source
117+
*/
118+
export function remove_from_legacy_sources(source) {
119+
if (component_context?.l?.s) {
120+
component_context.l.s = component_context.l.s.filter((s) => s !== source);
121+
}
122+
}
123+
115124
/**
116125
* @template V
117126
* @param {Value<V>} source

0 commit comments

Comments
 (0)