fix: cleanup `#initial_reaction` on `teardown` to free memory

source-to-state-in-class-belt-and-braces
paoloricciuti 2 months ago
parent 2e69ed4f27
commit fb627c097b

@ -4,6 +4,7 @@ import { set, source, state } from '../internal/client/reactivity/sources.js';
import { label, tag } from '../internal/client/dev/tracing.js';
import { active_reaction, get, push_reaction_value } from '../internal/client/runtime.js';
import { increment } from './utils.js';
import { teardown } from '../internal/client/reactivity/effects.js';
/**
* 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 {
constructor(value) {
super();
this.#initial_reaction = active_reaction;
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;
});
}
if (DEV) {
// If the value is invalid then the native exception will fire here

@ -4,6 +4,7 @@ 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 { increment } from './utils.js';
import { teardown } from '../internal/client/reactivity/effects.js';
var read_methods = ['forEach', 'isDisjointFrom', 'isSubsetOf', 'isSupersetOf'];
var set_like_methods = ['difference', 'intersection', 'symmetricDifference', 'union'];
@ -60,7 +61,14 @@ export class SvelteSet extends Set {
constructor(value) {
super();
this.#initial_reaction = active_reaction;
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;
});
}
if (DEV) {
// If the value is invalid then the native exception will fire here

Loading…
Cancel
Save