From e3072c9906ecd8f5e8280ed32a193fdb2f0240d4 Mon Sep 17 00:00:00 2001 From: paoloricciuti Date: Mon, 7 Jul 2025 22:35:08 +0200 Subject: [PATCH] chore: use WeakRef --- packages/svelte/src/reactivity/map.js | 14 ++++++-------- packages/svelte/src/reactivity/set.js | 14 ++++++-------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/packages/svelte/src/reactivity/map.js b/packages/svelte/src/reactivity/map.js index 64eb2a6276..35946c5b08 100644 --- a/packages/svelte/src/reactivity/map.js +++ b/packages/svelte/src/reactivity/map.js @@ -57,7 +57,7 @@ export class SvelteMap extends Map { #sources = new Map(); #version = state(0); #size = state(0); - /**@type {Reaction | null} */ + /**@type {WeakRef | null} */ #initial_reaction = null; /** @@ -67,12 +67,10 @@ export class SvelteMap extends Map { super(); if (active_reaction !== null) { - this.#initial_reaction = active_reaction; - // since we only need `initial_reaction` as long as we are in a derived/effect we can - // safely create a teardown function that will reset it to null and allow for GC - teardown(() => { - this.#initial_reaction = null; - }); + // we use a WeakRef (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef) + // so that if this Map is somehow stored outside of the active reaction, + // it will not prevent the reaction from being garbage collected. + this.#initial_reaction = new WeakRef(active_reaction); } if (DEV) { @@ -101,7 +99,7 @@ export class SvelteMap extends Map { * @returns {Source} */ #source(value) { - if (this.#initial_reaction === active_reaction) { + if (this.#initial_reaction !== null && this.#initial_reaction.deref() === active_reaction) { return state(value); } return source(value); diff --git a/packages/svelte/src/reactivity/set.js b/packages/svelte/src/reactivity/set.js index e7c429c22f..66c0151142 100644 --- a/packages/svelte/src/reactivity/set.js +++ b/packages/svelte/src/reactivity/set.js @@ -52,7 +52,7 @@ export class SvelteSet extends Set { #version = state(0); #size = state(0); - /**@type {Reaction | null}*/ + /**@type {WeakRef | null}*/ #initial_reaction = null; /** @@ -62,12 +62,10 @@ export class SvelteSet extends Set { super(); if (active_reaction !== null) { - this.#initial_reaction = active_reaction; - // since we only need `initial_reaction` as long as we are in a derived/effect we can - // safely create a teardown function that will reset it to null and allow for GC - teardown(() => { - this.#initial_reaction = null; - }); + // we use a WeakRef (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef) + // so that if this Map is somehow stored outside of the active reaction, + // it will not prevent the reaction from being garbage collected. + this.#initial_reaction = new WeakRef(active_reaction); } if (DEV) { @@ -98,7 +96,7 @@ export class SvelteSet extends Set { * @returns {Source} */ #source(value) { - if (this.#initial_reaction === active_reaction) { + if (this.#initial_reaction !== null && this.#initial_reaction.deref() === active_reaction) { return state(value); } return source(value);