Skip to content

Commit e3072c9

Browse files
committed
chore: use WeakRef
1 parent f261686 commit e3072c9

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

packages/svelte/src/reactivity/map.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class SvelteMap extends Map {
5757
#sources = new Map();
5858
#version = state(0);
5959
#size = state(0);
60-
/**@type {Reaction | null} */
60+
/**@type {WeakRef<Reaction> | null} */
6161
#initial_reaction = null;
6262

6363
/**
@@ -67,12 +67,10 @@ export class SvelteMap extends Map {
6767
super();
6868

6969
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-
});
70+
// we use a WeakRef (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef)
71+
// so that if this Map is somehow stored outside of the active reaction,
72+
// it will not prevent the reaction from being garbage collected.
73+
this.#initial_reaction = new WeakRef(active_reaction);
7674
}
7775

7876
if (DEV) {
@@ -101,7 +99,7 @@ export class SvelteMap extends Map {
10199
* @returns {Source<T>}
102100
*/
103101
#source(value) {
104-
if (this.#initial_reaction === active_reaction) {
102+
if (this.#initial_reaction !== null && this.#initial_reaction.deref() === active_reaction) {
105103
return state(value);
106104
}
107105
return source(value);

packages/svelte/src/reactivity/set.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class SvelteSet extends Set {
5252
#version = state(0);
5353
#size = state(0);
5454

55-
/**@type {Reaction | null}*/
55+
/**@type {WeakRef<Reaction> | null}*/
5656
#initial_reaction = null;
5757

5858
/**
@@ -62,12 +62,10 @@ export class SvelteSet extends Set {
6262
super();
6363

6464
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-
});
65+
// we use a WeakRef (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef)
66+
// so that if this Map is somehow stored outside of the active reaction,
67+
// it will not prevent the reaction from being garbage collected.
68+
this.#initial_reaction = new WeakRef(active_reaction);
7169
}
7270

7371
if (DEV) {
@@ -98,7 +96,7 @@ export class SvelteSet extends Set {
9896
* @returns {Source<T>}
9997
*/
10098
#source(value) {
101-
if (this.#initial_reaction === active_reaction) {
99+
if (this.#initial_reaction !== null && this.#initial_reaction.deref() === active_reaction) {
102100
return state(value);
103101
}
104102
return source(value);

0 commit comments

Comments
 (0)