Skip to content

Commit ed724fb

Browse files
committed
Refactor to return a dispose function from retain instead of dispose method on queryRef.
1 parent 820d241 commit ed724fb

File tree

3 files changed

+22
-23
lines changed

3 files changed

+22
-23
lines changed

src/react/cache/QueryReference.ts

+19-14
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ export class InternalQueryReference<TData = unknown> {
7373
this.handleError = this.handleError.bind(this);
7474
this.initiateFetch = this.initiateFetch.bind(this);
7575
this.dispose = this.dispose.bind(this);
76-
this.performDispose = this.performDispose.bind(this);
7776
this.observable = observable;
7877
this.result = observable.getCurrentResult(false);
7978
this.key = options.key;
@@ -108,7 +107,7 @@ export class InternalQueryReference<TData = unknown> {
108107
// helps prevent memory leaks when a component has unmounted before the
109108
// query has finished loading.
110109
this.autoDisposeTimeoutId = setTimeout(
111-
this.performDispose,
110+
this.dispose,
112111
options.autoDisposeTimeoutMs ?? 30_000
113112
);
114113
}
@@ -120,6 +119,23 @@ export class InternalQueryReference<TData = unknown> {
120119
retain() {
121120
this.references++;
122121
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+
};
123139
}
124140

125141
didChangeOptions(watchQueryOptions: WatchQueryOptions) {
@@ -179,18 +195,7 @@ export class InternalQueryReference<TData = unknown> {
179195
return promise;
180196
}
181197

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() {
194199
this.subscription.unsubscribe();
195200
this.onDispose();
196201
}

src/react/hooks/useBackgroundQuery.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,7 @@ export function useBackgroundQuery<
147147
promiseCache.set(queryRef.key, promise);
148148
}
149149

150-
React.useEffect(() => {
151-
queryRef.retain();
152-
153-
return () => {
154-
queryRef.dispose();
155-
};
156-
}, [queryRef]);
150+
React.useEffect(() => queryRef.retain(), [queryRef]);
157151

158152
const fetchMore: FetchMoreFunction<TData, TVariables> = React.useCallback(
159153
(options) => {

src/react/hooks/useSuspenseQuery.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ export function useSuspenseQuery<
183183
}
184184

185185
React.useEffect(() => {
186-
queryRef.retain();
186+
const dispose = queryRef.retain();
187187

188188
const removeListener = queryRef.listen((promise) => {
189189
setPromiseCache((promiseCache) =>
@@ -193,7 +193,7 @@ export function useSuspenseQuery<
193193

194194
return () => {
195195
removeListener();
196-
queryRef.dispose();
196+
dispose();
197197
};
198198
}, [queryRef]);
199199

0 commit comments

Comments
 (0)