@@ -73,7 +73,6 @@ export class InternalQueryReference<TData = unknown> {
73
73
this . handleError = this . handleError . bind ( this ) ;
74
74
this . initiateFetch = this . initiateFetch . bind ( this ) ;
75
75
this . dispose = this . dispose . bind ( this ) ;
76
- this . performDispose = this . performDispose . bind ( this ) ;
77
76
this . observable = observable ;
78
77
this . result = observable . getCurrentResult ( false ) ;
79
78
this . key = options . key ;
@@ -108,7 +107,7 @@ export class InternalQueryReference<TData = unknown> {
108
107
// helps prevent memory leaks when a component has unmounted before the
109
108
// query has finished loading.
110
109
this . autoDisposeTimeoutId = setTimeout (
111
- this . performDispose ,
110
+ this . dispose ,
112
111
options . autoDisposeTimeoutMs ?? 30_000
113
112
) ;
114
113
}
@@ -120,6 +119,23 @@ export class InternalQueryReference<TData = unknown> {
120
119
retain ( ) {
121
120
this . references ++ ;
122
121
clearTimeout ( this . autoDisposeTimeoutId ) ;
122
+ let disposed = false ;
123
+
124
+ return ( ) => {
125
+ if ( disposed ) {
126
+ return ;
127
+ }
128
+
129
+ disposed = true ;
130
+ this . references -- ;
131
+
132
+ // Wait before fully disposing in case the app is running in strict mode.
133
+ setTimeout ( ( ) => {
134
+ if ( ! this . references ) {
135
+ this . dispose ( ) ;
136
+ }
137
+ } ) ;
138
+ } ;
123
139
}
124
140
125
141
didChangeOptions ( watchQueryOptions : WatchQueryOptions ) {
@@ -179,18 +195,7 @@ export class InternalQueryReference<TData = unknown> {
179
195
return promise ;
180
196
}
181
197
182
- dispose ( ) {
183
- this . references -- ;
184
-
185
- // Wait before fully disposing in case the app is running in strict mode.
186
- setTimeout ( ( ) => {
187
- if ( ! this . references ) {
188
- this . performDispose ( ) ;
189
- }
190
- } ) ;
191
- }
192
-
193
- private performDispose ( ) {
198
+ private dispose ( ) {
194
199
this . subscription . unsubscribe ( ) ;
195
200
this . onDispose ( ) ;
196
201
}
0 commit comments