File tree Expand file tree Collapse file tree 2 files changed +50
-5
lines changed Expand file tree Collapse file tree 2 files changed +50
-5
lines changed Original file line number Diff line number Diff line change @@ -350,6 +350,47 @@ describe('streamedQuery', () => {
350
350
unsubscribe ( )
351
351
} )
352
352
353
+ test ( 'should abort when unsubscribed' , async ( ) => {
354
+ const key = queryKey ( )
355
+ const observer = new QueryObserver ( queryClient , {
356
+ queryKey : key ,
357
+ queryFn : streamedQuery ( {
358
+ queryFn : ( context ) => {
359
+ // just consume the signal
360
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
361
+ const numbers = context . signal ? 3 : 0
362
+ return createAsyncNumberGenerator ( numbers )
363
+ } ,
364
+ } ) ,
365
+ } )
366
+
367
+ const unsubscribe = observer . subscribe ( vi . fn ( ) )
368
+
369
+ expect ( queryClient . getQueryState ( key ) ) . toMatchObject ( {
370
+ status : 'pending' ,
371
+ fetchStatus : 'fetching' ,
372
+ data : undefined ,
373
+ } )
374
+
375
+ await vi . advanceTimersByTimeAsync ( 60 )
376
+
377
+ expect ( queryClient . getQueryState ( key ) ) . toMatchObject ( {
378
+ status : 'success' ,
379
+ fetchStatus : 'fetching' ,
380
+ data : [ 0 ] ,
381
+ } )
382
+
383
+ unsubscribe ( )
384
+
385
+ await vi . advanceTimersByTimeAsync ( 10 )
386
+
387
+ expect ( queryClient . getQueryState ( key ) ) . toMatchObject ( {
388
+ status : 'success' ,
389
+ fetchStatus : 'idle' ,
390
+ data : [ 0 ] ,
391
+ } )
392
+ } )
393
+
353
394
test ( 'should support maxChunks' , async ( ) => {
354
395
const key = queryKey ( )
355
396
const observer = new QueryObserver ( queryClient , {
Original file line number Diff line number Diff line change @@ -604,22 +604,26 @@ export class Query<
604
604
fetchMeta : action . meta ?? null ,
605
605
}
606
606
case 'success' :
607
- // If fetching ends successfully, we don't need revertState as a fallback anymore.
608
- this . #revertState = undefined
609
- return {
607
+ const newState = {
610
608
...state ,
611
609
data : action . data ,
612
610
dataUpdateCount : state . dataUpdateCount + 1 ,
613
611
dataUpdatedAt : action . dataUpdatedAt ?? Date . now ( ) ,
614
612
error : null ,
615
613
isInvalidated : false ,
616
- status : 'success' ,
614
+ status : 'success' as const ,
617
615
...( ! action . manual && {
618
- fetchStatus : 'idle' ,
616
+ fetchStatus : 'idle' as const ,
619
617
fetchFailureCount : 0 ,
620
618
fetchFailureReason : null ,
621
619
} ) ,
622
620
}
621
+
622
+ // If fetching ends successfully, we don't need revertState as a fallback anymore.
623
+ // For manual updates, capture the state to revert to it in case of a cancellation.
624
+ this . #revertState = action . manual ? newState : undefined
625
+
626
+ return newState
623
627
case 'error' :
624
628
const error = action . error
625
629
You can’t perform that action at this time.
0 commit comments