File tree Expand file tree Collapse file tree 2 files changed +12
-16
lines changed
packages/svelte/src/reactivity Expand file tree Collapse file tree 2 files changed +12
-16
lines changed Original file line number Diff line number Diff line change @@ -57,7 +57,7 @@ export class SvelteMap extends Map {
57
57
#sources = new Map ( ) ;
58
58
#version = state ( 0 ) ;
59
59
#size = state ( 0 ) ;
60
- /**@type {Reaction | null } */
60
+ /**@type {WeakRef< Reaction> | null } */
61
61
#initial_reaction = null ;
62
62
63
63
/**
@@ -67,12 +67,10 @@ export class SvelteMap extends Map {
67
67
super ( ) ;
68
68
69
69
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 ) ;
76
74
}
77
75
78
76
if ( DEV ) {
@@ -101,7 +99,7 @@ export class SvelteMap extends Map {
101
99
* @returns {Source<T> }
102
100
*/
103
101
#source( value ) {
104
- if ( this . #initial_reaction === active_reaction ) {
102
+ if ( this . #initial_reaction !== null && this . #initial_reaction . deref ( ) === active_reaction ) {
105
103
return state ( value ) ;
106
104
}
107
105
return source ( value ) ;
Original file line number Diff line number Diff line change @@ -52,7 +52,7 @@ export class SvelteSet extends Set {
52
52
#version = state ( 0 ) ;
53
53
#size = state ( 0 ) ;
54
54
55
- /**@type {Reaction | null }*/
55
+ /**@type {WeakRef< Reaction> | null }*/
56
56
#initial_reaction = null ;
57
57
58
58
/**
@@ -62,12 +62,10 @@ export class SvelteSet extends Set {
62
62
super ( ) ;
63
63
64
64
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 ) ;
71
69
}
72
70
73
71
if ( DEV ) {
@@ -98,7 +96,7 @@ export class SvelteSet extends Set {
98
96
* @returns {Source<T> }
99
97
*/
100
98
#source( value ) {
101
- if ( this . #initial_reaction === active_reaction ) {
99
+ if ( this . #initial_reaction !== null && this . #initial_reaction . deref ( ) === active_reaction ) {
102
100
return state ( value ) ;
103
101
}
104
102
return source ( value ) ;
You can’t perform that action at this time.
0 commit comments