1
1
import type { BaseQueryFn } from '../../baseQueryTypes'
2
2
import type { QueryDefinition } from '../../endpointDefinitions'
3
3
import type { ConfigState , QueryCacheKey } from '../apiState'
4
- import { QuerySubstateIdentifier } from '../apiState'
5
4
import type {
6
5
QueryStateMeta ,
7
6
SubMiddlewareApi ,
8
- SubMiddlewareBuilder ,
9
7
TimeoutId ,
8
+ InternalHandlerBuilder ,
9
+ ApiMiddlewareInternalHandler ,
10
10
} from './types'
11
11
12
12
export type ReferenceCacheCollection = never
@@ -45,7 +45,11 @@ declare module '../../endpointDefinitions' {
45
45
export const THIRTY_TWO_BIT_MAX_INT = 2_147_483_647
46
46
export const THIRTY_TWO_BIT_MAX_TIMER_SECONDS = 2_147_483_647 / 1_000 - 1
47
47
48
- export const build : SubMiddlewareBuilder = ( { reducerPath, api, context } ) => {
48
+ export const buildCacheCollectionHandler : InternalHandlerBuilder = ( {
49
+ reducerPath,
50
+ api,
51
+ context,
52
+ } ) => {
49
53
const { removeQueryResult, unsubscribeQueryResult } = api . internalActions
50
54
51
55
function anySubscriptionsRemainingForKey (
@@ -57,88 +61,83 @@ export const build: SubMiddlewareBuilder = ({ reducerPath, api, context }) => {
57
61
return ! ! subscriptions && ! isObjectEmpty ( subscriptions )
58
62
}
59
63
60
- return ( mwApi ) => {
61
- const currentRemovalTimeouts : QueryStateMeta < TimeoutId > = { }
64
+ const currentRemovalTimeouts : QueryStateMeta < TimeoutId > = { }
62
65
63
- return ( next ) =>
64
- ( action ) : any => {
65
- const result = next ( action )
66
+ const handler : ApiMiddlewareInternalHandler = ( action , mwApi ) => {
67
+ if ( unsubscribeQueryResult . match ( action ) ) {
68
+ const state = mwApi . getState ( ) [ reducerPath ]
69
+ const { queryCacheKey } = action . payload
66
70
67
- if ( unsubscribeQueryResult . match ( action ) ) {
68
- const state = mwApi . getState ( ) [ reducerPath ]
69
- const { queryCacheKey } = action . payload
70
-
71
- handleUnsubscribe (
72
- queryCacheKey ,
73
- state . queries [ queryCacheKey ] ?. endpointName ,
74
- mwApi ,
75
- state . config
76
- )
77
- }
78
-
79
- if ( api . util . resetApiState . match ( action ) ) {
80
- for ( const [ key , timeout ] of Object . entries ( currentRemovalTimeouts ) ) {
81
- if ( timeout ) clearTimeout ( timeout )
82
- delete currentRemovalTimeouts [ key ]
83
- }
84
- }
85
-
86
- if ( context . hasRehydrationInfo ( action ) ) {
87
- const state = mwApi . getState ( ) [ reducerPath ]
88
- const { queries } = context . extractRehydrationInfo ( action ) !
89
- for ( const [ queryCacheKey , queryState ] of Object . entries ( queries ) ) {
90
- // Gotcha:
91
- // If rehydrating before the endpoint has been injected,the global `keepUnusedDataFor`
92
- // will be used instead of the endpoint-specific one.
93
- handleUnsubscribe (
94
- queryCacheKey as QueryCacheKey ,
95
- queryState ?. endpointName ,
96
- mwApi ,
97
- state . config
98
- )
99
- }
100
- }
71
+ handleUnsubscribe (
72
+ queryCacheKey ,
73
+ state . queries [ queryCacheKey ] ?. endpointName ,
74
+ mwApi ,
75
+ state . config
76
+ )
77
+ }
101
78
102
- return result
79
+ if ( api . util . resetApiState . match ( action ) ) {
80
+ for ( const [ key , timeout ] of Object . entries ( currentRemovalTimeouts ) ) {
81
+ if ( timeout ) clearTimeout ( timeout )
82
+ delete currentRemovalTimeouts [ key ]
103
83
}
84
+ }
104
85
105
- function handleUnsubscribe (
106
- queryCacheKey : QueryCacheKey ,
107
- endpointName : string | undefined ,
108
- api : SubMiddlewareApi ,
109
- config : ConfigState < string >
110
- ) {
111
- const endpointDefinition = context . endpointDefinitions [
112
- endpointName !
113
- ] as QueryDefinition < any , any , any , any >
114
- const keepUnusedDataFor =
115
- endpointDefinition ?. keepUnusedDataFor ?? config . keepUnusedDataFor
116
-
117
- if ( keepUnusedDataFor === Infinity ) {
118
- // Hey, user said keep this forever!
119
- return
86
+ if ( context . hasRehydrationInfo ( action ) ) {
87
+ const state = mwApi . getState ( ) [ reducerPath ]
88
+ const { queries } = context . extractRehydrationInfo ( action ) !
89
+ for ( const [ queryCacheKey , queryState ] of Object . entries ( queries ) ) {
90
+ // Gotcha:
91
+ // If rehydrating before the endpoint has been injected,the global `keepUnusedDataFor`
92
+ // will be used instead of the endpoint-specific one.
93
+ handleUnsubscribe (
94
+ queryCacheKey as QueryCacheKey ,
95
+ queryState ?. endpointName ,
96
+ mwApi ,
97
+ state . config
98
+ )
120
99
}
121
- // Prevent `setTimeout` timers from overflowing a 32-bit internal int, by
122
- // clamping the max value to be at most 1000ms less than the 32-bit max.
123
- // Look, a 24.8-day keepalive ought to be enough for anybody, right? :)
124
- // Also avoid negative values too.
125
- const finalKeepUnusedDataFor = Math . max (
126
- 0 ,
127
- Math . min ( keepUnusedDataFor , THIRTY_TWO_BIT_MAX_TIMER_SECONDS )
128
- )
100
+ }
101
+ }
129
102
130
- if ( ! anySubscriptionsRemainingForKey ( queryCacheKey , api ) ) {
131
- const currentTimeout = currentRemovalTimeouts [ queryCacheKey ]
132
- if ( currentTimeout ) {
133
- clearTimeout ( currentTimeout )
134
- }
135
- currentRemovalTimeouts [ queryCacheKey ] = setTimeout ( ( ) => {
136
- if ( ! anySubscriptionsRemainingForKey ( queryCacheKey , api ) ) {
137
- api . dispatch ( removeQueryResult ( { queryCacheKey } ) )
138
- }
139
- delete currentRemovalTimeouts ! [ queryCacheKey ]
140
- } , finalKeepUnusedDataFor * 1000 )
103
+ function handleUnsubscribe (
104
+ queryCacheKey : QueryCacheKey ,
105
+ endpointName : string | undefined ,
106
+ api : SubMiddlewareApi ,
107
+ config : ConfigState < string >
108
+ ) {
109
+ const endpointDefinition = context . endpointDefinitions [
110
+ endpointName !
111
+ ] as QueryDefinition < any , any , any , any >
112
+ const keepUnusedDataFor =
113
+ endpointDefinition ?. keepUnusedDataFor ?? config . keepUnusedDataFor
114
+
115
+ if ( keepUnusedDataFor === Infinity ) {
116
+ // Hey, user said keep this forever!
117
+ return
118
+ }
119
+ // Prevent `setTimeout` timers from overflowing a 32-bit internal int, by
120
+ // clamping the max value to be at most 1000ms less than the 32-bit max.
121
+ // Look, a 24.8-day keepalive ought to be enough for anybody, right? :)
122
+ // Also avoid negative values too.
123
+ const finalKeepUnusedDataFor = Math . max (
124
+ 0 ,
125
+ Math . min ( keepUnusedDataFor , THIRTY_TWO_BIT_MAX_TIMER_SECONDS )
126
+ )
127
+
128
+ if ( ! anySubscriptionsRemainingForKey ( queryCacheKey , api ) ) {
129
+ const currentTimeout = currentRemovalTimeouts [ queryCacheKey ]
130
+ if ( currentTimeout ) {
131
+ clearTimeout ( currentTimeout )
141
132
}
133
+ currentRemovalTimeouts [ queryCacheKey ] = setTimeout ( ( ) => {
134
+ if ( ! anySubscriptionsRemainingForKey ( queryCacheKey , api ) ) {
135
+ api . dispatch ( removeQueryResult ( { queryCacheKey } ) )
136
+ }
137
+ delete currentRemovalTimeouts ! [ queryCacheKey ]
138
+ } , finalKeepUnusedDataFor * 1000 )
142
139
}
143
140
}
141
+
142
+ return handler
144
143
}
0 commit comments