@@ -286,27 +286,33 @@ export class Query<
286286 this . errorListeners = [ ] ;
287287 this . doneListeners = [ ] ;
288288
289+ // simple type override to make typescript happy
290+ // and do less for javascript
291+ const qc = queryClient as unknown as Partial <
292+ Pick < QueryClient , 'queryFeatures' | 'hooks' >
293+ > ;
294+
289295 this . features = {
290- cumulativeQueryHash : config . cumulativeQueryHash ,
291- enableOnDemand : config . enableOnDemand ,
292- lazy : config . lazy ,
293- resetOnDestroy : config . resetOnDestroy ,
294- removeOnDestroy : config . removeOnDestroy ,
295- transformError : config . transformError ,
296- dynamicOptionsUpdateDelay : config . dynamicOptionsUpdateDelay ,
296+ cumulativeQueryHash :
297+ config . cumulativeQueryHash ?? qc . queryFeatures ?. cumulativeQueryHash ,
298+ enableOnDemand : config . enableOnDemand ?? qc . queryFeatures ?. enableOnDemand ,
299+ lazy : config . lazy ?? qc . queryFeatures ?. lazy ,
300+ resetOnDestroy :
301+ config . resetOnDestroy ??
302+ config . resetOnDispose ??
303+ qc . queryFeatures ?. resetOnDestroy ??
304+ qc . queryFeatures ?. resetOnDispose ,
305+ removeOnDestroy :
306+ config . removeOnDestroy ?? qc . queryFeatures ?. removeOnDestroy ,
307+ transformError : config . transformError ?? qc . queryFeatures ?. transformError ,
308+ dynamicOptionsUpdateDelay :
309+ config . dynamicOptionsUpdateDelay ??
310+ qc . queryFeatures ?. dynamicOptionsUpdateDelay ,
311+ autoRemovePreviousQuery :
312+ config . autoRemovePreviousQuery ??
313+ qc . queryFeatures ?. autoRemovePreviousQuery ,
297314 } ;
298-
299- if ( 'queryFeatures' in queryClient ) {
300- this . features . lazy ??= queryClient . queryFeatures . lazy ;
301- this . features . enableOnDemand ??=
302- queryClient . queryFeatures . enableOnDemand ??
303- queryClient . queryFeatures . resetOnDispose ;
304- this . features . cumulativeQueryHash ??=
305- queryClient . queryFeatures . cumulativeQueryHash ;
306- this . features . transformError ??= queryClient . queryFeatures . transformError ;
307-
308- this . hooks = queryClient . hooks ;
309- }
315+ this . hooks = qc . hooks ;
310316
311317 observable . deep ( this , '_result' ) ;
312318 observable . ref ( this , 'isResultRequsted' ) ;
@@ -537,7 +543,18 @@ export class Query<
537543 private processOptions (
538544 options : QueryOptions < TQueryFnData , TError , TData , TQueryData , TQueryKey > ,
539545 ) {
540- options . queryHash = this . createQueryHash ( options . queryKey , options ) ;
546+ const nextQueryHash = this . createQueryHash ( options . queryKey , options ) ;
547+
548+ if (
549+ this . features . autoRemovePreviousQuery &&
550+ options . queryHash !== nextQueryHash
551+ ) {
552+ this . queryClient . removeQueries ( {
553+ predicate : ( query ) => query . queryHash === options . queryHash ,
554+ } ) ;
555+ }
556+
557+ options . queryHash = nextQueryHash ;
541558
542559 if ( this . features . cumulativeQueryHash ) {
543560 this . cumulativeQueryKeyHashesSet . add ( options . queryHash ) ;
@@ -631,6 +648,17 @@ export class Query<
631648 } ) ;
632649 }
633650
651+ if ( params ?. safe ) {
652+ return this . queryClient . removeQueries ( {
653+ ...params ,
654+ predicate : ( query ) =>
655+ query . queryHash === this . options . queryHash &&
656+ ( query . observers . length === 0 ||
657+ ( query . observers . length === 1 &&
658+ query . observers [ 0 ] === this . queryObserver ) ) ,
659+ } ) ;
660+ }
661+
634662 return this . queryClient . removeQueries ( {
635663 queryKey : this . options . queryKey ,
636664 exact : true ,
@@ -677,7 +705,9 @@ export class Query<
677705 }
678706
679707 if ( this . features . removeOnDestroy ) {
680- this . remove ( ) ;
708+ this . remove ( {
709+ safe : this . features . removeOnDestroy === 'safe' ,
710+ } ) ;
681711 }
682712
683713 delete this . _observerSubscription ;
0 commit comments