1
- import type { Action , PayloadAction , UnknownAction } from '@reduxjs/toolkit'
1
+ import type { PayloadAction } from '@reduxjs/toolkit'
2
2
import {
3
3
combineReducers ,
4
4
createAction ,
@@ -23,7 +23,6 @@ import type {
23
23
QueryCacheKey ,
24
24
SubscriptionState ,
25
25
ConfigState ,
26
- QueryKeys ,
27
26
InfiniteQuerySubState ,
28
27
InfiniteQueryDirection ,
29
28
} from './apiState'
@@ -36,18 +35,14 @@ import type {
36
35
MutationThunk ,
37
36
QueryThunk ,
38
37
QueryThunkArg ,
39
- RejectedAction ,
40
38
} from './buildThunks'
41
39
import { calculateProvidedByThunk } from './buildThunks'
42
40
import {
43
41
isInfiniteQueryDefinition ,
44
42
type AssertTagTypes ,
45
- type DefinitionType ,
46
43
type EndpointDefinitions ,
47
44
type FullTagDescription ,
48
- type QueryArgFrom ,
49
45
type QueryDefinition ,
50
- type ResultTypeFrom ,
51
46
} from '../endpointDefinitions'
52
47
import type { Patch } from 'immer'
53
48
import { isDraft } from 'immer'
@@ -61,6 +56,7 @@ import {
61
56
import type { ApiContext } from '../apiTypes'
62
57
import { isUpsertQuery } from './buildInitiate'
63
58
import type { InternalSerializeQueryArgs } from '../defaultSerializeQueryArgs'
59
+ import type { UnwrapPromise } from '../tsHelpers'
64
60
65
61
/**
66
62
* A typesafe single entry to be upserted into the cache
@@ -279,6 +275,7 @@ export function buildSlice({
279
275
substate . fulfilledTimeStamp = meta . fulfilledTimeStamp
280
276
} )
281
277
}
278
+
282
279
const querySlice = createSlice ( {
283
280
name : `${ reducerPath } /queries` ,
284
281
initialState : initialState as QueryState < any > ,
@@ -486,6 +483,11 @@ export function buildSlice({
486
483
} ,
487
484
} )
488
485
486
+ type CalculateProvidedByAction = UnwrapPromise <
487
+ | ReturnType < ReturnType < QueryThunk > >
488
+ | ReturnType < ReturnType < InfiniteQueryThunk < any > > >
489
+ >
490
+
489
491
const invalidationSlice = createSlice ( {
490
492
name : `${ reducerPath } /invalidation` ,
491
493
initialState : initialState as InvalidationState < string > ,
@@ -562,26 +564,51 @@ export function buildSlice({
562
564
. addMatcher (
563
565
isAnyOf ( isFulfilled ( queryThunk ) , isRejectedWithValue ( queryThunk ) ) ,
564
566
( draft , action ) => {
565
- const providedTags = calculateProvidedByThunk (
566
- action ,
567
- 'providesTags' ,
568
- definitions ,
569
- assertTagType ,
570
- )
571
- const { queryCacheKey } = action . meta . arg
567
+ writeProvidedTagsForQuery ( draft , action )
568
+ } ,
569
+ )
570
+ . addMatcher (
571
+ querySlice . actions . cacheEntriesUpserted . match ,
572
+ ( draft , action ) => {
573
+ for ( const { queryDescription : arg , value } of action . payload ) {
574
+ const action : CalculateProvidedByAction = {
575
+ type : 'UNKNOWN' ,
576
+ payload : value ,
577
+ meta : {
578
+ requestStatus : 'fulfilled' ,
579
+ requestId : 'UNKNOWN' ,
580
+ arg,
581
+ } ,
582
+ }
572
583
573
- invalidationSlice . caseReducers . updateProvidedBy (
574
- draft ,
575
- invalidationSlice . actions . updateProvidedBy ( {
576
- queryCacheKey,
577
- providedTags,
578
- } ) ,
579
- )
584
+ writeProvidedTagsForQuery ( draft , action )
585
+ }
580
586
} ,
581
587
)
582
588
} ,
583
589
} )
584
590
591
+ function writeProvidedTagsForQuery (
592
+ draft : InvalidationState < string > ,
593
+ action : CalculateProvidedByAction ,
594
+ ) {
595
+ const providedTags = calculateProvidedByThunk (
596
+ action ,
597
+ 'providesTags' ,
598
+ definitions ,
599
+ assertTagType ,
600
+ )
601
+ const { queryCacheKey } = action . meta . arg
602
+
603
+ invalidationSlice . caseReducers . updateProvidedBy (
604
+ draft ,
605
+ invalidationSlice . actions . updateProvidedBy ( {
606
+ queryCacheKey,
607
+ providedTags,
608
+ } ) ,
609
+ )
610
+ }
611
+
585
612
// Dummy slice to generate actions
586
613
const subscriptionSlice = createSlice ( {
587
614
name : `${ reducerPath } /subscriptions` ,
0 commit comments