Skip to content

Commit 779c4ba

Browse files
committed
dispose of queryRefs faster on rejection
1 parent 978dda6 commit 779c4ba

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/react/internal/cache/QueryReference.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export interface PreloadedQueryRef<
7979
}
8080

8181
interface InternalQueryReferenceOptions {
82-
onDispose?: () => void;
82+
onDispose: (ref: InternalQueryReference<any, any>) => void;
8383
autoDisposeTimeoutMs?: number;
8484
}
8585

@@ -209,9 +209,10 @@ export class InternalQueryReference<
209209
this.dispose = this.dispose.bind(this);
210210
this.observable = observable;
211211

212-
if (options.onDispose) {
213-
this.onDispose = options.onDispose;
214-
}
212+
this.onDispose = () => {
213+
clearTimeout(this.autoDisposeTimeoutId);
214+
options.onDispose?.(this);
215+
};
215216

216217
this.setResult();
217218
this.subscribeToQuery();
@@ -351,7 +352,7 @@ export class InternalQueryReference<
351352
return this.initiateFetch(this.observable.fetchMore<TData>(options));
352353
}
353354

354-
private dispose() {
355+
public dispose() {
355356
this.subscription.unsubscribe();
356357
}
357358

src/react/internal/cache/SuspenseCache.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,30 @@ export class SuspenseCache {
4343
>(cacheKey: CacheKey, createObservable: () => ObservableQuery<TData>) {
4444
const ref = this.queryRefs.lookupArray(cacheKey) as {
4545
current?: InternalQueryReference<TData, TStates>;
46+
disposeTimeout?: ReturnType<typeof setTimeout>;
4647
};
4748

4849
if (!ref.current) {
4950
ref.current = new InternalQueryReference(createObservable(), {
5051
autoDisposeTimeoutMs: this.options.autoDisposeTimeoutMs,
51-
onDispose: () => {
52+
onDispose: (internalRef) => {
53+
if (internalRef !== ref.current) {
54+
return;
55+
}
5256
delete ref.current;
57+
if (ref.disposeTimeout) {
58+
clearTimeout(ref.disposeTimeout);
59+
delete ref.disposeTimeout;
60+
}
5361
},
5462
});
63+
} else if (ref.current.promise.status === "rejected") {
64+
if (ref.disposeTimeout) {
65+
clearTimeout(ref.disposeTimeout);
66+
}
67+
ref.disposeTimeout = setTimeout(() => {
68+
ref.current?.dispose();
69+
}, 1000);
5570
}
5671

5772
return ref.current;

0 commit comments

Comments
 (0)