Skip to content

Commit fb627c0

Browse files
committed
fix: cleanup #initial_reaction on teardown to free memory
1 parent 2e69ed4 commit fb627c0

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

packages/svelte/src/reactivity/map.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { set, source, state } from '../internal/client/reactivity/sources.js';
44
import { label, tag } from '../internal/client/dev/tracing.js';
55
import { active_reaction, get, push_reaction_value } from '../internal/client/runtime.js';
66
import { increment } from './utils.js';
7+
import { teardown } from '../internal/client/reactivity/effects.js';
78

89
/**
910
* A reactive version of the built-in [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) object.
@@ -65,7 +66,14 @@ export class SvelteMap extends Map {
6566
constructor(value) {
6667
super();
6768

68-
this.#initial_reaction = active_reaction;
69+
if (active_reaction !== null) {
70+
this.#initial_reaction = active_reaction;
71+
// since we only need `initial_reaction` as long as we are in a derived/effect we can
72+
// safely create a teardown function that will reset it to null and allow for GC
73+
teardown(() => {
74+
this.#initial_reaction = null;
75+
});
76+
}
6977

7078
if (DEV) {
7179
// If the value is invalid then the native exception will fire here

packages/svelte/src/reactivity/set.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { source, set, state } from '../internal/client/reactivity/sources.js';
44
import { label, tag } from '../internal/client/dev/tracing.js';
55
import { active_reaction, get } from '../internal/client/runtime.js';
66
import { increment } from './utils.js';
7+
import { teardown } from '../internal/client/reactivity/effects.js';
78

89
var read_methods = ['forEach', 'isDisjointFrom', 'isSubsetOf', 'isSupersetOf'];
910
var set_like_methods = ['difference', 'intersection', 'symmetricDifference', 'union'];
@@ -60,7 +61,14 @@ export class SvelteSet extends Set {
6061
constructor(value) {
6162
super();
6263

63-
this.#initial_reaction = active_reaction;
64+
if (active_reaction !== null) {
65+
this.#initial_reaction = active_reaction;
66+
// since we only need `initial_reaction` as long as we are in a derived/effect we can
67+
// safely create a teardown function that will reset it to null and allow for GC
68+
teardown(() => {
69+
this.#initial_reaction = null;
70+
});
71+
}
6472

6573
if (DEV) {
6674
// If the value is invalid then the native exception will fire here

0 commit comments

Comments
 (0)