Skip to content

Commit 3ab41d6

Browse files
authored
use update_version instead of WeakRef in SvelteSet/SvelteMap (#16324)
1 parent e3072c9 commit 3ab41d6

File tree

3 files changed

+14
-18
lines changed

3 files changed

+14
-18
lines changed

packages/svelte/src/internal/client/runtime.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ let write_version = 1;
132132
/** @type {number} Used to version each read of a source of derived to avoid duplicating depedencies inside a reaction */
133133
let read_version = 0;
134134

135+
export let update_version = read_version;
136+
135137
// If we are working with a get() chain that has no active container,
136138
// to prevent memory leaks, we skip adding the reaction.
137139
export let skip_reaction = false;
@@ -265,6 +267,7 @@ export function update_reaction(reaction) {
265267
var previous_reaction_sources = source_ownership;
266268
var previous_component_context = component_context;
267269
var previous_untracking = untracking;
270+
var previous_update_version = update_version;
268271

269272
var flags = reaction.f;
270273

@@ -278,7 +281,7 @@ export function update_reaction(reaction) {
278281
source_ownership = null;
279282
set_component_context(reaction.ctx);
280283
untracking = false;
281-
read_version++;
284+
update_version = ++read_version;
282285

283286
reaction.f |= EFFECT_IS_UPDATING;
284287

@@ -366,6 +369,7 @@ export function update_reaction(reaction) {
366369
source_ownership = previous_reaction_sources;
367370
set_component_context(previous_component_context);
368371
untracking = previous_untracking;
372+
update_version = previous_update_version;
369373

370374
reaction.f ^= EFFECT_IS_UPDATING;
371375
}

packages/svelte/src/reactivity/map.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { DEV } from 'esm-env';
33
import { set, source, state } from '../internal/client/reactivity/sources.js';
44
import { label, tag } from '../internal/client/dev/tracing.js';
5-
import { active_reaction, get } from '../internal/client/runtime.js';
5+
import { active_reaction, get, update_version } from '../internal/client/runtime.js';
66
import { increment } from './utils.js';
77
import { teardown } from '../internal/client/reactivity/effects.js';
88

@@ -57,8 +57,7 @@ export class SvelteMap extends Map {
5757
#sources = new Map();
5858
#version = state(0);
5959
#size = state(0);
60-
/**@type {WeakRef<Reaction> | null} */
61-
#initial_reaction = null;
60+
#update_version = -1;
6261

6362
/**
6463
* @param {Iterable<readonly [K, V]> | null | undefined} [value]
@@ -67,10 +66,7 @@ export class SvelteMap extends Map {
6766
super();
6867

6968
if (active_reaction !== null) {
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);
69+
this.#update_version = update_version;
7470
}
7571

7672
if (DEV) {
@@ -99,9 +95,10 @@ export class SvelteMap extends Map {
9995
* @returns {Source<T>}
10096
*/
10197
#source(value) {
102-
if (this.#initial_reaction !== null && this.#initial_reaction.deref() === active_reaction) {
98+
if (update_version === this.#update_version) {
10399
return state(value);
104100
}
101+
105102
return source(value);
106103
}
107104

packages/svelte/src/reactivity/set.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { DEV } from 'esm-env';
33
import { source, set, state } from '../internal/client/reactivity/sources.js';
44
import { label, tag } from '../internal/client/dev/tracing.js';
5-
import { active_reaction, get } from '../internal/client/runtime.js';
5+
import { active_reaction, get, update_version } from '../internal/client/runtime.js';
66
import { increment } from './utils.js';
77
import { teardown } from '../internal/client/reactivity/effects.js';
88

@@ -51,9 +51,7 @@ export class SvelteSet extends Set {
5151
#sources = new Map();
5252
#version = state(0);
5353
#size = state(0);
54-
55-
/**@type {WeakRef<Reaction> | null}*/
56-
#initial_reaction = null;
54+
#update_version = -1;
5755

5856
/**
5957
* @param {Iterable<T> | null | undefined} [value]
@@ -62,10 +60,7 @@ export class SvelteSet extends Set {
6260
super();
6361

6462
if (active_reaction !== null) {
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);
63+
this.#update_version = update_version;
6964
}
7065

7166
if (DEV) {
@@ -96,7 +91,7 @@ export class SvelteSet extends Set {
9691
* @returns {Source<T>}
9792
*/
9893
#source(value) {
99-
if (this.#initial_reaction !== null && this.#initial_reaction.deref() === active_reaction) {
94+
if (this.#update_version === update_version) {
10095
return state(value);
10196
}
10297
return source(value);

0 commit comments

Comments
 (0)