Skip to content

chore: use update_version instead of WeakRef in SvelteSet/SvelteMap #16324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ let write_version = 1;
/** @type {number} Used to version each read of a source of derived to avoid duplicating depedencies inside a reaction */
let read_version = 0;

export let update_version = read_version;

// If we are working with a get() chain that has no active container,
// to prevent memory leaks, we skip adding the reaction.
export let skip_reaction = false;
Expand Down Expand Up @@ -265,6 +267,7 @@ export function update_reaction(reaction) {
var previous_reaction_sources = source_ownership;
var previous_component_context = component_context;
var previous_untracking = untracking;
var previous_update_version = update_version;

var flags = reaction.f;

Expand All @@ -278,7 +281,7 @@ export function update_reaction(reaction) {
source_ownership = null;
set_component_context(reaction.ctx);
untracking = false;
read_version++;
update_version = ++read_version;

reaction.f |= EFFECT_IS_UPDATING;

Expand Down Expand Up @@ -366,6 +369,7 @@ export function update_reaction(reaction) {
source_ownership = previous_reaction_sources;
set_component_context(previous_component_context);
untracking = previous_untracking;
update_version = previous_update_version;

reaction.f ^= EFFECT_IS_UPDATING;
}
Expand Down
13 changes: 5 additions & 8 deletions packages/svelte/src/reactivity/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { DEV } from 'esm-env';
import { set, source, state } from '../internal/client/reactivity/sources.js';
import { label, tag } from '../internal/client/dev/tracing.js';
import { active_reaction, get } from '../internal/client/runtime.js';
import { active_reaction, get, update_version } from '../internal/client/runtime.js';
import { increment } from './utils.js';
import { teardown } from '../internal/client/reactivity/effects.js';

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

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

if (active_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);
this.#update_version = update_version;
}

if (DEV) {
Expand Down Expand Up @@ -99,9 +95,10 @@ export class SvelteMap extends Map {
* @returns {Source<T>}
*/
#source(value) {
if (this.#initial_reaction !== null && this.#initial_reaction.deref() === active_reaction) {
if (update_version === this.#update_version) {
return state(value);
}

return source(value);
}

Expand Down
13 changes: 4 additions & 9 deletions packages/svelte/src/reactivity/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { DEV } from 'esm-env';
import { source, set, state } from '../internal/client/reactivity/sources.js';
import { label, tag } from '../internal/client/dev/tracing.js';
import { active_reaction, get } from '../internal/client/runtime.js';
import { active_reaction, get, update_version } from '../internal/client/runtime.js';
import { increment } from './utils.js';
import { teardown } from '../internal/client/reactivity/effects.js';

Expand Down Expand Up @@ -51,9 +51,7 @@ export class SvelteSet extends Set {
#sources = new Map();
#version = state(0);
#size = state(0);

/**@type {WeakRef<Reaction> | null}*/
#initial_reaction = null;
#update_version = -1;

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

if (active_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);
this.#update_version = update_version;
}

if (DEV) {
Expand Down Expand Up @@ -96,7 +91,7 @@ export class SvelteSet extends Set {
* @returns {Source<T>}
*/
#source(value) {
if (this.#initial_reaction !== null && this.#initial_reaction.deref() === active_reaction) {
if (this.#update_version === update_version) {
return state(value);
}
return source(value);
Expand Down