@depup/reduxjs__toolkit 2.12.0-depup.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (173) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +33 -0
  3. package/changes.json +18 -0
  4. package/dist/cjs/index.js +6 -0
  5. package/dist/cjs/redux-toolkit.development.cjs +2406 -0
  6. package/dist/cjs/redux-toolkit.development.cjs.map +1 -0
  7. package/dist/cjs/redux-toolkit.production.min.cjs +3 -0
  8. package/dist/cjs/redux-toolkit.production.min.cjs.map +1 -0
  9. package/dist/index.d.mts +2665 -0
  10. package/dist/index.d.ts +2665 -0
  11. package/dist/query/cjs/index.js +6 -0
  12. package/dist/query/cjs/rtk-query.development.cjs +3092 -0
  13. package/dist/query/cjs/rtk-query.development.cjs.map +1 -0
  14. package/dist/query/cjs/rtk-query.production.min.cjs +2 -0
  15. package/dist/query/cjs/rtk-query.production.min.cjs.map +1 -0
  16. package/dist/query/index.d.mts +3066 -0
  17. package/dist/query/index.d.ts +3066 -0
  18. package/dist/query/react/cjs/index.js +6 -0
  19. package/dist/query/react/cjs/rtk-query-react.development.cjs +748 -0
  20. package/dist/query/react/cjs/rtk-query-react.development.cjs.map +1 -0
  21. package/dist/query/react/cjs/rtk-query-react.production.min.cjs +2 -0
  22. package/dist/query/react/cjs/rtk-query-react.production.min.cjs.map +1 -0
  23. package/dist/query/react/index.d.mts +1009 -0
  24. package/dist/query/react/index.d.ts +1009 -0
  25. package/dist/query/react/rtk-query-react.browser.mjs +2 -0
  26. package/dist/query/react/rtk-query-react.browser.mjs.map +1 -0
  27. package/dist/query/react/rtk-query-react.legacy-esm.js +740 -0
  28. package/dist/query/react/rtk-query-react.legacy-esm.js.map +1 -0
  29. package/dist/query/react/rtk-query-react.modern.mjs +705 -0
  30. package/dist/query/react/rtk-query-react.modern.mjs.map +1 -0
  31. package/dist/query/rtk-query.browser.mjs +2 -0
  32. package/dist/query/rtk-query.browser.mjs.map +1 -0
  33. package/dist/query/rtk-query.legacy-esm.js +3117 -0
  34. package/dist/query/rtk-query.legacy-esm.js.map +1 -0
  35. package/dist/query/rtk-query.modern.mjs +3052 -0
  36. package/dist/query/rtk-query.modern.mjs.map +1 -0
  37. package/dist/react/cjs/index.js +6 -0
  38. package/dist/react/cjs/redux-toolkit-react.development.cjs +55 -0
  39. package/dist/react/cjs/redux-toolkit-react.development.cjs.map +1 -0
  40. package/dist/react/cjs/redux-toolkit-react.production.min.cjs +2 -0
  41. package/dist/react/cjs/redux-toolkit-react.production.min.cjs.map +1 -0
  42. package/dist/react/index.d.mts +22 -0
  43. package/dist/react/index.d.ts +22 -0
  44. package/dist/react/redux-toolkit-react.browser.mjs +2 -0
  45. package/dist/react/redux-toolkit-react.browser.mjs.map +1 -0
  46. package/dist/react/redux-toolkit-react.legacy-esm.js +47 -0
  47. package/dist/react/redux-toolkit-react.legacy-esm.js.map +1 -0
  48. package/dist/react/redux-toolkit-react.modern.mjs +28 -0
  49. package/dist/react/redux-toolkit-react.modern.mjs.map +1 -0
  50. package/dist/redux-toolkit.browser.mjs +3 -0
  51. package/dist/redux-toolkit.browser.mjs.map +1 -0
  52. package/dist/redux-toolkit.legacy-esm.js +2351 -0
  53. package/dist/redux-toolkit.legacy-esm.js.map +1 -0
  54. package/dist/redux-toolkit.modern.mjs +2330 -0
  55. package/dist/redux-toolkit.modern.mjs.map +1 -0
  56. package/dist/uncheckedindexed.ts +16 -0
  57. package/package.json +310 -0
  58. package/query/package.json +13 -0
  59. package/query/react/package.json +13 -0
  60. package/react/package.json +13 -0
  61. package/skills/build-modern-redux-apps/modern-redux/SKILL.md +304 -0
  62. package/skills/build-modern-redux-apps/modern-redux/references/store-lifetime.md +53 -0
  63. package/skills/build-modern-redux-apps/redux-dataflow/SKILL.md +264 -0
  64. package/skills/evolve-and-diagnose-redux-apps/debug-redux-toolkit-apps/SKILL.md +269 -0
  65. package/skills/evolve-and-diagnose-redux-apps/migrate-to-modern-redux/SKILL.md +226 -0
  66. package/skills/manage-server-data/adopt-rtk-query/SKILL.md +385 -0
  67. package/skills/manage-server-data/adopt-rtk-query/references/endpoint-lifecycle.md +36 -0
  68. package/skills/model-redux-state/build-slices-and-selectors/SKILL.md +364 -0
  69. package/skills/model-redux-state/build-slices-and-selectors/references/slice-patterns.md +59 -0
  70. package/skills/model-redux-state/design-state-ownership/SKILL.md +322 -0
  71. package/skills/model-redux-state/design-state-ownership/references/state-ownership.md +28 -0
  72. package/skills/orchestrate-side-effects/handle-side-effects/SKILL.md +271 -0
  73. package/skills/orchestrate-side-effects/handle-side-effects/references/listener-workflows.md +34 -0
  74. package/src/actionCreatorInvariantMiddleware.ts +34 -0
  75. package/src/autoBatchEnhancer.ts +146 -0
  76. package/src/combineSlices.ts +487 -0
  77. package/src/configureStore.ts +248 -0
  78. package/src/createAction.ts +324 -0
  79. package/src/createAsyncThunk.ts +791 -0
  80. package/src/createDraftSafeSelector.ts +30 -0
  81. package/src/createReducer.ts +217 -0
  82. package/src/createSlice.ts +1079 -0
  83. package/src/devtoolsExtension.ts +241 -0
  84. package/src/dynamicMiddleware/index.ts +93 -0
  85. package/src/dynamicMiddleware/react/index.ts +101 -0
  86. package/src/dynamicMiddleware/types.ts +82 -0
  87. package/src/entities/create_adapter.ts +47 -0
  88. package/src/entities/entity_state.ts +38 -0
  89. package/src/entities/index.ts +8 -0
  90. package/src/entities/models.ts +198 -0
  91. package/src/entities/sorted_state_adapter.ts +266 -0
  92. package/src/entities/state_adapter.ts +58 -0
  93. package/src/entities/state_selectors.ts +73 -0
  94. package/src/entities/unsorted_state_adapter.ts +204 -0
  95. package/src/entities/utils.ts +68 -0
  96. package/src/formatProdErrorMessage.ts +13 -0
  97. package/src/getDefaultEnhancers.ts +31 -0
  98. package/src/getDefaultMiddleware.ts +113 -0
  99. package/src/immerImports.ts +7 -0
  100. package/src/immutableStateInvariantMiddleware.ts +274 -0
  101. package/src/index.ts +213 -0
  102. package/src/listenerMiddleware/exceptions.ts +20 -0
  103. package/src/listenerMiddleware/index.ts +562 -0
  104. package/src/listenerMiddleware/task.ts +100 -0
  105. package/src/listenerMiddleware/types.ts +886 -0
  106. package/src/listenerMiddleware/utils.ts +30 -0
  107. package/src/mapBuilders.ts +298 -0
  108. package/src/matchers.ts +365 -0
  109. package/src/nanoid.ts +20 -0
  110. package/src/query/HandledError.ts +6 -0
  111. package/src/query/apiTypes.ts +120 -0
  112. package/src/query/baseQueryTypes.ts +101 -0
  113. package/src/query/core/apiState.ts +377 -0
  114. package/src/query/core/buildInitiate.ts +596 -0
  115. package/src/query/core/buildMiddleware/batchActions.ts +207 -0
  116. package/src/query/core/buildMiddleware/cacheCollection.ts +204 -0
  117. package/src/query/core/buildMiddleware/cacheLifecycle.ts +379 -0
  118. package/src/query/core/buildMiddleware/devMiddleware.ts +34 -0
  119. package/src/query/core/buildMiddleware/index.ts +162 -0
  120. package/src/query/core/buildMiddleware/invalidationByTags.ts +139 -0
  121. package/src/query/core/buildMiddleware/polling.ts +187 -0
  122. package/src/query/core/buildMiddleware/queryLifecycle.ts +508 -0
  123. package/src/query/core/buildMiddleware/types.ts +173 -0
  124. package/src/query/core/buildMiddleware/windowEventHandling.ts +64 -0
  125. package/src/query/core/buildSelectors.ts +413 -0
  126. package/src/query/core/buildSlice.ts +735 -0
  127. package/src/query/core/buildThunks.ts +1124 -0
  128. package/src/query/core/index.ts +58 -0
  129. package/src/query/core/module.ts +727 -0
  130. package/src/query/core/rtkImports.ts +24 -0
  131. package/src/query/core/setupListeners.ts +118 -0
  132. package/src/query/createApi.ts +506 -0
  133. package/src/query/defaultSerializeQueryArgs.ts +52 -0
  134. package/src/query/endpointDefinitions.ts +1717 -0
  135. package/src/query/fakeBaseQuery.ts +21 -0
  136. package/src/query/fetchBaseQuery.ts +385 -0
  137. package/src/query/index.ts +105 -0
  138. package/src/query/react/ApiProvider.tsx +69 -0
  139. package/src/query/react/buildHooks.ts +2297 -0
  140. package/src/query/react/constants.ts +2 -0
  141. package/src/query/react/index.ts +45 -0
  142. package/src/query/react/module.ts +275 -0
  143. package/src/query/react/namedHooks.ts +59 -0
  144. package/src/query/react/reactImports.ts +10 -0
  145. package/src/query/react/reactReduxImports.ts +1 -0
  146. package/src/query/react/rtkqImports.ts +8 -0
  147. package/src/query/react/useSerializedStableValue.ts +17 -0
  148. package/src/query/react/useShallowStableValue.ts +13 -0
  149. package/src/query/retry.ts +233 -0
  150. package/src/query/standardSchema.ts +35 -0
  151. package/src/query/tsHelpers.ts +48 -0
  152. package/src/query/utils/capitalize.ts +3 -0
  153. package/src/query/utils/copyWithStructuralSharing.ts +27 -0
  154. package/src/query/utils/countObjectKeys.ts +14 -0
  155. package/src/query/utils/filterMap.ts +27 -0
  156. package/src/query/utils/getCurrent.ts +6 -0
  157. package/src/query/utils/getOrInsert.ts +41 -0
  158. package/src/query/utils/immerImports.ts +9 -0
  159. package/src/query/utils/index.ts +11 -0
  160. package/src/query/utils/isAbsoluteUrl.ts +9 -0
  161. package/src/query/utils/isDocumentVisible.ts +12 -0
  162. package/src/query/utils/isNotNullish.ts +7 -0
  163. package/src/query/utils/isOnline.ts +12 -0
  164. package/src/query/utils/isValidUrl.ts +9 -0
  165. package/src/query/utils/joinUrls.ts +26 -0
  166. package/src/query/utils/signals.ts +33 -0
  167. package/src/react/index.ts +7 -0
  168. package/src/reduxImports.ts +8 -0
  169. package/src/reselectImports.ts +1 -0
  170. package/src/serializableStateInvariantMiddleware.ts +285 -0
  171. package/src/tsHelpers.ts +214 -0
  172. package/src/uncheckedindexed.ts +16 -0
  173. package/src/utils.ts +125 -0
@@ -0,0 +1,886 @@
1
+ import type {
2
+ Action,
3
+ Dispatch,
4
+ Middleware,
5
+ MiddlewareAPI,
6
+ UnknownAction,
7
+ } from 'redux'
8
+ import type { ThunkDispatch } from 'redux-thunk'
9
+ import type { BaseActionCreator, PayloadAction } from '../createAction'
10
+ import type { TypedActionCreator } from '../mapBuilders'
11
+ import type { TaskAbortError } from './exceptions'
12
+
13
+ /**
14
+ * Types copied from RTK
15
+ */
16
+
17
+ /** @internal */
18
+ type TypedActionCreatorWithMatchFunction<Type extends string> =
19
+ TypedActionCreator<Type> & {
20
+ match: MatchFunction<any>
21
+ }
22
+
23
+ /** @internal */
24
+ export type AnyListenerPredicate<State> = (
25
+ action: UnknownAction,
26
+ currentState: State,
27
+ originalState: State,
28
+ ) => boolean
29
+
30
+ /** @public */
31
+ export type ListenerPredicate<ActionType extends Action, State> = (
32
+ action: UnknownAction,
33
+ currentState: State,
34
+ originalState: State,
35
+ ) => action is ActionType
36
+
37
+ /** @public */
38
+ export interface ConditionFunction<State> {
39
+ (predicate: AnyListenerPredicate<State>, timeout?: number): Promise<boolean>
40
+ (predicate: AnyListenerPredicate<State>, timeout?: number): Promise<boolean>
41
+ (predicate: () => boolean, timeout?: number): Promise<boolean>
42
+ }
43
+
44
+ /** @internal */
45
+ export type MatchFunction<T> = (v: any) => v is T
46
+
47
+ /** @public */
48
+ export interface ForkedTaskAPI {
49
+ /**
50
+ * Returns a promise that resolves when `waitFor` resolves or
51
+ * rejects if the task or the parent listener has been cancelled or is completed.
52
+ */
53
+ pause<W>(waitFor: Promise<W>): Promise<W>
54
+ /**
55
+ * Returns a promise that resolves after `timeoutMs` or
56
+ * rejects if the task or the parent listener has been cancelled or is completed.
57
+ * @param timeoutMs
58
+ */
59
+ delay(timeoutMs: number): Promise<void>
60
+ /**
61
+ * An abort signal whose `aborted` property is set to `true`
62
+ * if the task execution is either aborted or completed.
63
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
64
+ */
65
+ signal: AbortSignal
66
+ }
67
+
68
+ /** @public */
69
+ export interface AsyncTaskExecutor<T> {
70
+ (forkApi: ForkedTaskAPI): Promise<T>
71
+ }
72
+
73
+ /** @public */
74
+ export interface SyncTaskExecutor<T> {
75
+ (forkApi: ForkedTaskAPI): T
76
+ }
77
+
78
+ /** @public */
79
+ export type ForkedTaskExecutor<T> = AsyncTaskExecutor<T> | SyncTaskExecutor<T>
80
+
81
+ /** @public */
82
+ export type TaskResolved<T> = {
83
+ readonly status: 'ok'
84
+ readonly value: T
85
+ }
86
+
87
+ /** @public */
88
+ export type TaskRejected = {
89
+ readonly status: 'rejected'
90
+ readonly error: unknown
91
+ }
92
+
93
+ /** @public */
94
+ export type TaskCancelled = {
95
+ readonly status: 'cancelled'
96
+ readonly error: TaskAbortError
97
+ }
98
+
99
+ /** @public */
100
+ export type TaskResult<Value> =
101
+ | TaskResolved<Value>
102
+ | TaskRejected
103
+ | TaskCancelled
104
+
105
+ /** @public */
106
+ export interface ForkedTask<T> {
107
+ /**
108
+ * A promise that resolves when the task is either completed or cancelled or rejects
109
+ * if parent listener execution is cancelled or completed.
110
+ *
111
+ * ### Example
112
+ * ```ts
113
+ * const result = await fork(async (forkApi) => Promise.resolve(4)).result;
114
+ *
115
+ * if (result.status === 'ok') {
116
+ * console.log(result.value); // logs 4
117
+ * }
118
+ * ```
119
+ */
120
+ result: Promise<TaskResult<T>>
121
+ /**
122
+ * Cancel task if it is in progress or not yet started,
123
+ * it is noop otherwise.
124
+ */
125
+ cancel(): void
126
+ }
127
+
128
+ /** @public */
129
+ export interface ForkOptions {
130
+ /**
131
+ * If true, causes the parent task to not be marked as complete until
132
+ * all autoJoined forks have completed or failed.
133
+ */
134
+ autoJoin: boolean
135
+ }
136
+
137
+ /** @public */
138
+ export interface ListenerEffectAPI<
139
+ State,
140
+ DispatchType extends Dispatch,
141
+ ExtraArgument = unknown,
142
+ > extends MiddlewareAPI<DispatchType, State> {
143
+ /**
144
+ * Returns the store state as it existed when the action was originally dispatched, _before_ the reducers ran.
145
+ *
146
+ * ### Synchronous invocation
147
+ *
148
+ * This function can **only** be invoked **synchronously**, it throws error otherwise.
149
+ *
150
+ * @example
151
+ *
152
+ * ```ts
153
+ * middleware.startListening({
154
+ * predicate: () => true,
155
+ * async effect(_, { getOriginalState }) {
156
+ * getOriginalState(); // sync: OK!
157
+ *
158
+ * setTimeout(getOriginalState, 0); // async: throws Error
159
+ *
160
+ * await Promise().resolve();
161
+ *
162
+ * getOriginalState(); // async: throws Error
163
+ * },
164
+ * });
165
+ * ```
166
+ */
167
+ getOriginalState: () => State
168
+ /**
169
+ * Removes the listener entry from the middleware and prevent future instances of the listener from running.
170
+ *
171
+ * It does **not** cancel any active instances.
172
+ */
173
+ unsubscribe(): void
174
+ /**
175
+ * It will subscribe a listener if it was previously removed, noop otherwise.
176
+ */
177
+ subscribe(): void
178
+ /**
179
+ * Returns a promise that resolves when the input predicate returns `true` or
180
+ * rejects if the listener has been cancelled or is completed.
181
+ *
182
+ * The return value is `true` if the predicate succeeds or `false` if a timeout is provided and expires first.
183
+ *
184
+ * ### Example
185
+ *
186
+ * ```ts
187
+ * import { createAction } from '@reduxjs/toolkit';
188
+ *
189
+ * const updateBy = createAction<number>('counter/updateBy');
190
+ *
191
+ * middleware.startListening({
192
+ * actionCreator: updateBy,
193
+ * async effect(_, { condition }) {
194
+ * // wait at most 3s for `updateBy` actions.
195
+ * if (await condition(updateBy.match, 3_000)) {
196
+ * // `updateBy` has been dispatched twice in less than 3s.
197
+ * }
198
+ * },
199
+ * });
200
+ * ```
201
+ */
202
+ condition: ConditionFunction<State>
203
+ /**
204
+ * Returns a promise that resolves when the input predicate returns `true` or
205
+ * rejects if the listener has been cancelled or is completed.
206
+ *
207
+ * The return value is the `[action, currentState, previousState]` combination that the predicate saw as arguments.
208
+ *
209
+ * The promise resolves to null if a timeout is provided and expires first,
210
+ *
211
+ * ### Example
212
+ *
213
+ * ```ts
214
+ * const updateBy = createAction<number>('counter/updateBy');
215
+ *
216
+ * middleware.startListening({
217
+ * actionCreator: updateBy,
218
+ * async effect(_, { take }) {
219
+ * const [{ payload }] = await take(updateBy.match);
220
+ * console.log(payload); // logs 5;
221
+ * }
222
+ * })
223
+ *
224
+ * store.dispatch(updateBy(5));
225
+ * ```
226
+ */
227
+ take: TakePattern<State>
228
+ /**
229
+ * Cancels all other running instances of this same listener except for the one that made this call.
230
+ */
231
+ cancelActiveListeners: () => void
232
+ /**
233
+ * Cancels the instance of this listener that made this call.
234
+ */
235
+ cancel: () => void
236
+ /**
237
+ * Throws a `TaskAbortError` if this listener has been cancelled
238
+ */
239
+ throwIfCancelled: () => void
240
+ /**
241
+ * An abort signal whose `aborted` property is set to `true`
242
+ * if the listener execution is either aborted or completed.
243
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
244
+ */
245
+ signal: AbortSignal
246
+ /**
247
+ * Returns a promise that resolves after `timeoutMs` or
248
+ * rejects if the listener has been cancelled or is completed.
249
+ */
250
+ delay(timeoutMs: number): Promise<void>
251
+ /**
252
+ * Queues in the next microtask the execution of a task.
253
+ * @param executor
254
+ * @param options
255
+ */
256
+ fork<T>(executor: ForkedTaskExecutor<T>, options?: ForkOptions): ForkedTask<T>
257
+ /**
258
+ * Returns a promise that resolves when `waitFor` resolves or
259
+ * rejects if the listener has been cancelled or is completed.
260
+ * @param promise
261
+ */
262
+ pause<M>(promise: Promise<M>): Promise<M>
263
+ extra: ExtraArgument
264
+ }
265
+
266
+ /** @public */
267
+ export type ListenerEffect<
268
+ ActionType extends Action,
269
+ State,
270
+ DispatchType extends Dispatch,
271
+ ExtraArgument = unknown,
272
+ > = (
273
+ action: ActionType,
274
+ api: ListenerEffectAPI<State, DispatchType, ExtraArgument>,
275
+ ) => void | Promise<void>
276
+
277
+ /**
278
+ * Additional infos regarding the error raised.
279
+ * @public
280
+ */
281
+ export interface ListenerErrorInfo {
282
+ /**
283
+ * Which function has generated the exception.
284
+ */
285
+ raisedBy: 'effect' | 'predicate'
286
+ }
287
+
288
+ /**
289
+ * Gets notified with synchronous and asynchronous errors raised by `listeners` or `predicates`.
290
+ * @param error The thrown error.
291
+ * @param errorInfo Additional information regarding the thrown error.
292
+ * @public
293
+ */
294
+ export interface ListenerErrorHandler {
295
+ (error: unknown, errorInfo: ListenerErrorInfo): void
296
+ }
297
+
298
+ /** @public */
299
+ export interface CreateListenerMiddlewareOptions<ExtraArgument = unknown> {
300
+ extra?: ExtraArgument
301
+ /**
302
+ * Receives synchronous errors that are raised by `listener` and `listenerOption.predicate`.
303
+ */
304
+ onError?: ListenerErrorHandler
305
+ }
306
+
307
+ /** @public */
308
+ export type ListenerMiddleware<
309
+ State = unknown,
310
+ DispatchType extends ThunkDispatch<State, unknown, Action> = ThunkDispatch<
311
+ State,
312
+ unknown,
313
+ UnknownAction
314
+ >,
315
+ ExtraArgument = unknown,
316
+ > = Middleware<
317
+ {
318
+ (action: Action<'listenerMiddleware/add'>): UnsubscribeListener
319
+ },
320
+ State,
321
+ DispatchType
322
+ >
323
+
324
+ /** @public */
325
+ export interface ListenerMiddlewareInstance<
326
+ StateType = unknown,
327
+ DispatchType extends ThunkDispatch<
328
+ StateType,
329
+ unknown,
330
+ Action
331
+ > = ThunkDispatch<StateType, unknown, UnknownAction>,
332
+ ExtraArgument = unknown,
333
+ > {
334
+ middleware: ListenerMiddleware<StateType, DispatchType, ExtraArgument>
335
+
336
+ startListening: AddListenerOverloads<
337
+ UnsubscribeListener,
338
+ StateType,
339
+ DispatchType,
340
+ ExtraArgument
341
+ > &
342
+ TypedStartListening<StateType, DispatchType, ExtraArgument>
343
+
344
+ stopListening: RemoveListenerOverloads<StateType, DispatchType> &
345
+ TypedStopListening<StateType, DispatchType>
346
+
347
+ /**
348
+ * Unsubscribes all listeners, cancels running listeners and tasks.
349
+ */
350
+ clearListeners: () => void
351
+ }
352
+
353
+ /**
354
+ * API Function Overloads
355
+ */
356
+
357
+ /** @public */
358
+ export type TakePatternOutputWithoutTimeout<
359
+ State,
360
+ Predicate extends AnyListenerPredicate<State>,
361
+ > =
362
+ Predicate extends MatchFunction<infer ActionType>
363
+ ? Promise<[ActionType, State, State]>
364
+ : Promise<[UnknownAction, State, State]>
365
+
366
+ /** @public */
367
+ export type TakePatternOutputWithTimeout<
368
+ State,
369
+ Predicate extends AnyListenerPredicate<State>,
370
+ > =
371
+ Predicate extends MatchFunction<infer ActionType>
372
+ ? Promise<[ActionType, State, State] | null>
373
+ : Promise<[UnknownAction, State, State] | null>
374
+
375
+ /** @public */
376
+ export interface TakePattern<State> {
377
+ <Predicate extends AnyListenerPredicate<State>>(
378
+ predicate: Predicate,
379
+ ): TakePatternOutputWithoutTimeout<State, Predicate>
380
+ <Predicate extends AnyListenerPredicate<State>>(
381
+ predicate: Predicate,
382
+ timeout: number,
383
+ ): TakePatternOutputWithTimeout<State, Predicate>
384
+ <Predicate extends AnyListenerPredicate<State>>(
385
+ predicate: Predicate,
386
+ timeout?: number | undefined,
387
+ ): TakePatternOutputWithTimeout<State, Predicate>
388
+ }
389
+
390
+ /** @public */
391
+ export interface UnsubscribeListenerOptions {
392
+ cancelActive?: true
393
+ }
394
+
395
+ /** @public */
396
+ export type UnsubscribeListener = (
397
+ unsubscribeOptions?: UnsubscribeListenerOptions,
398
+ ) => void
399
+
400
+ /**
401
+ * The possible overloads and options for defining a listener.
402
+ * The return type of each function is specified as a generic arg,
403
+ * so the overloads can be reused for multiple different functions.
404
+ *
405
+ * @public
406
+ */
407
+ export type AddListenerOverloads<
408
+ Return,
409
+ StateType = unknown,
410
+ DispatchType extends Dispatch = ThunkDispatch<
411
+ StateType,
412
+ unknown,
413
+ UnknownAction
414
+ >,
415
+ ExtraArgument = unknown,
416
+ AdditionalOptions = unknown,
417
+ > = {
418
+ /** Accepts a "listener predicate" that is also a TS type predicate for the action*/
419
+ <
420
+ MiddlewareActionType extends UnknownAction,
421
+ ListenerPredicateType extends ListenerPredicate<
422
+ MiddlewareActionType,
423
+ StateType
424
+ >,
425
+ >(
426
+ options: {
427
+ actionCreator?: never
428
+ type?: never
429
+ matcher?: never
430
+ predicate: ListenerPredicateType
431
+ effect: ListenerEffect<
432
+ ListenerPredicateGuardedActionType<ListenerPredicateType>,
433
+ StateType,
434
+ DispatchType,
435
+ ExtraArgument
436
+ >
437
+ } & AdditionalOptions,
438
+ ): Return
439
+
440
+ /** Accepts an RTK action creator, like `incrementByAmount` */
441
+ <ActionCreatorType extends TypedActionCreatorWithMatchFunction<any>>(
442
+ options: {
443
+ actionCreator: ActionCreatorType
444
+ type?: never
445
+ matcher?: never
446
+ predicate?: never
447
+ effect: ListenerEffect<
448
+ ReturnType<ActionCreatorType>,
449
+ StateType,
450
+ DispatchType,
451
+ ExtraArgument
452
+ >
453
+ } & AdditionalOptions,
454
+ ): Return
455
+
456
+ /** Accepts a specific action type string */
457
+ <T extends string>(
458
+ options: {
459
+ actionCreator?: never
460
+ type: T
461
+ matcher?: never
462
+ predicate?: never
463
+ effect: ListenerEffect<Action<T>, StateType, DispatchType, ExtraArgument>
464
+ } & AdditionalOptions,
465
+ ): Return
466
+
467
+ /** Accepts an RTK matcher function, such as `incrementByAmount.match` */
468
+ <MatchFunctionType extends MatchFunction<Action>>(
469
+ options: {
470
+ actionCreator?: never
471
+ type?: never
472
+ matcher: MatchFunctionType
473
+ predicate?: never
474
+ effect: ListenerEffect<
475
+ GuardedType<MatchFunctionType>,
476
+ StateType,
477
+ DispatchType,
478
+ ExtraArgument
479
+ >
480
+ } & AdditionalOptions,
481
+ ): Return
482
+
483
+ /** Accepts a "listener predicate" that just returns a boolean, no type assertion */
484
+ <ListenerPredicateType extends AnyListenerPredicate<StateType>>(
485
+ options: {
486
+ actionCreator?: never
487
+ type?: never
488
+ matcher?: never
489
+ predicate: ListenerPredicateType
490
+ effect: ListenerEffect<
491
+ UnknownAction,
492
+ StateType,
493
+ DispatchType,
494
+ ExtraArgument
495
+ >
496
+ } & AdditionalOptions,
497
+ ): Return
498
+ }
499
+
500
+ /** @public */
501
+ export type RemoveListenerOverloads<
502
+ StateType = unknown,
503
+ DispatchType extends Dispatch = ThunkDispatch<
504
+ StateType,
505
+ unknown,
506
+ UnknownAction
507
+ >,
508
+ ExtraArgument = unknown,
509
+ > = AddListenerOverloads<
510
+ boolean,
511
+ StateType,
512
+ DispatchType,
513
+ ExtraArgument,
514
+ UnsubscribeListenerOptions
515
+ >
516
+
517
+ /** @public */
518
+ export interface RemoveListenerAction<
519
+ ActionType extends UnknownAction,
520
+ State,
521
+ DispatchType extends Dispatch,
522
+ > {
523
+ type: 'listenerMiddleware/remove'
524
+ payload: {
525
+ type: string
526
+ listener: ListenerEffect<ActionType, State, DispatchType>
527
+ }
528
+ }
529
+
530
+ /**
531
+ * A "pre-typed" version of `addListenerAction`, so the listener args are well-typed
532
+ *
533
+ * @public
534
+ */
535
+ export type TypedAddListener<
536
+ StateType,
537
+ DispatchType extends Dispatch = ThunkDispatch<
538
+ StateType,
539
+ unknown,
540
+ UnknownAction
541
+ >,
542
+ ExtraArgument = unknown,
543
+ Payload = ListenerEntry<StateType, DispatchType>,
544
+ T extends string = 'listenerMiddleware/add',
545
+ > = BaseActionCreator<Payload, T> &
546
+ AddListenerOverloads<
547
+ PayloadAction<Payload, T>,
548
+ StateType,
549
+ DispatchType,
550
+ ExtraArgument
551
+ > & {
552
+ /**
553
+ * Creates a "pre-typed" version of `addListener`
554
+ * where the `state`, `dispatch` and `extra` types are predefined.
555
+ *
556
+ * This allows you to set the `state`, `dispatch` and `extra` types once,
557
+ * eliminating the need to specify them with every `addListener` call.
558
+ *
559
+ * @returns A pre-typed `addListener` with the state, dispatch and extra types already defined.
560
+ *
561
+ * @example
562
+ * ```ts
563
+ * import { addListener } from '@reduxjs/toolkit';
564
+ *
565
+ * export const addAppListener = addListener.withTypes<
566
+ * RootState,
567
+ * AppDispatch,
568
+ * ExtraArguments
569
+ * >();
570
+ * ```
571
+ *
572
+ * @template OverrideStateType - The specific type of state the middleware listener operates on.
573
+ * @template OverrideDispatchType - The specific type of the dispatch function.
574
+ * @template OverrideExtraArgument - The specific type of the extra object.
575
+ *
576
+ * @since 2.1.0
577
+ */
578
+ withTypes: <
579
+ OverrideStateType extends StateType,
580
+ OverrideDispatchType extends Dispatch = ThunkDispatch<
581
+ OverrideStateType,
582
+ unknown,
583
+ UnknownAction
584
+ >,
585
+ OverrideExtraArgument = unknown,
586
+ >() => TypedAddListener<
587
+ OverrideStateType,
588
+ OverrideDispatchType,
589
+ OverrideExtraArgument
590
+ >
591
+ }
592
+
593
+ /**
594
+ * A "pre-typed" version of `removeListenerAction`, so the listener args are well-typed
595
+ *
596
+ * @public
597
+ */
598
+ export type TypedRemoveListener<
599
+ StateType,
600
+ DispatchType extends Dispatch = ThunkDispatch<
601
+ StateType,
602
+ unknown,
603
+ UnknownAction
604
+ >,
605
+ ExtraArgument = unknown,
606
+ Payload = ListenerEntry<StateType, DispatchType>,
607
+ T extends string = 'listenerMiddleware/remove',
608
+ > = BaseActionCreator<Payload, T> &
609
+ AddListenerOverloads<
610
+ PayloadAction<Payload, T>,
611
+ StateType,
612
+ DispatchType,
613
+ ExtraArgument,
614
+ UnsubscribeListenerOptions
615
+ > & {
616
+ /**
617
+ * Creates a "pre-typed" version of `removeListener`
618
+ * where the `state`, `dispatch` and `extra` types are predefined.
619
+ *
620
+ * This allows you to set the `state`, `dispatch` and `extra` types once,
621
+ * eliminating the need to specify them with every `removeListener` call.
622
+ *
623
+ * @returns A pre-typed `removeListener` with the state, dispatch and extra
624
+ * types already defined.
625
+ *
626
+ * @example
627
+ * ```ts
628
+ * import { removeListener } from '@reduxjs/toolkit'
629
+ *
630
+ * export const removeAppListener = removeListener.withTypes<
631
+ * RootState,
632
+ * AppDispatch,
633
+ * ExtraArguments
634
+ * >()
635
+ * ```
636
+ *
637
+ * @template OverrideStateType - The specific type of state the middleware listener operates on.
638
+ * @template OverrideDispatchType - The specific type of the dispatch function.
639
+ * @template OverrideExtraArgument - The specific type of the extra object.
640
+ *
641
+ * @since 2.1.0
642
+ */
643
+ withTypes: <
644
+ OverrideStateType extends StateType,
645
+ OverrideDispatchType extends Dispatch = ThunkDispatch<
646
+ OverrideStateType,
647
+ unknown,
648
+ UnknownAction
649
+ >,
650
+ OverrideExtraArgument = unknown,
651
+ >() => TypedRemoveListener<
652
+ OverrideStateType,
653
+ OverrideDispatchType,
654
+ OverrideExtraArgument
655
+ >
656
+ }
657
+
658
+ /**
659
+ * A "pre-typed" version of `middleware.startListening`, so the listener args are well-typed
660
+ *
661
+ * @public
662
+ */
663
+ export type TypedStartListening<
664
+ StateType,
665
+ DispatchType extends Dispatch = ThunkDispatch<
666
+ StateType,
667
+ unknown,
668
+ UnknownAction
669
+ >,
670
+ ExtraArgument = unknown,
671
+ > = AddListenerOverloads<
672
+ UnsubscribeListener,
673
+ StateType,
674
+ DispatchType,
675
+ ExtraArgument
676
+ > & {
677
+ /**
678
+ * Creates a "pre-typed" version of
679
+ * {@linkcode ListenerMiddlewareInstance.startListening | startListening}
680
+ * where the `state`, `dispatch` and `extra` types are predefined.
681
+ *
682
+ * This allows you to set the `state`, `dispatch` and `extra` types once,
683
+ * eliminating the need to specify them with every
684
+ * {@linkcode ListenerMiddlewareInstance.startListening | startListening}
685
+ * call.
686
+ *
687
+ * @returns A pre-typed `startListening` with the state, dispatch and extra types already defined.
688
+ *
689
+ * @example
690
+ * ```ts
691
+ * import { createListenerMiddleware } from '@reduxjs/toolkit'
692
+ *
693
+ * const listenerMiddleware = createListenerMiddleware()
694
+ *
695
+ * export const startAppListening = listenerMiddleware.startListening.withTypes<
696
+ * RootState,
697
+ * AppDispatch,
698
+ * ExtraArguments
699
+ * >()
700
+ * ```
701
+ *
702
+ * @template OverrideStateType - The specific type of state the middleware listener operates on.
703
+ * @template OverrideDispatchType - The specific type of the dispatch function.
704
+ * @template OverrideExtraArgument - The specific type of the extra object.
705
+ *
706
+ * @since 2.1.0
707
+ */
708
+ withTypes: <
709
+ OverrideStateType extends StateType,
710
+ OverrideDispatchType extends Dispatch = ThunkDispatch<
711
+ OverrideStateType,
712
+ unknown,
713
+ UnknownAction
714
+ >,
715
+ OverrideExtraArgument = unknown,
716
+ >() => TypedStartListening<
717
+ OverrideStateType,
718
+ OverrideDispatchType,
719
+ OverrideExtraArgument
720
+ >
721
+ }
722
+
723
+ /**
724
+ * A "pre-typed" version of `middleware.stopListening`, so the listener args are well-typed
725
+ *
726
+ * @public
727
+ */
728
+ export type TypedStopListening<
729
+ StateType,
730
+ DispatchType extends Dispatch = ThunkDispatch<
731
+ StateType,
732
+ unknown,
733
+ UnknownAction
734
+ >,
735
+ ExtraArgument = unknown,
736
+ > = RemoveListenerOverloads<StateType, DispatchType, ExtraArgument> & {
737
+ /**
738
+ * Creates a "pre-typed" version of
739
+ * {@linkcode ListenerMiddlewareInstance.stopListening | stopListening}
740
+ * where the `state`, `dispatch` and `extra` types are predefined.
741
+ *
742
+ * This allows you to set the `state`, `dispatch` and `extra` types once,
743
+ * eliminating the need to specify them with every
744
+ * {@linkcode ListenerMiddlewareInstance.stopListening | stopListening} call.
745
+ *
746
+ * @returns A pre-typed `stopListening` with the state, dispatch and extra types already defined.
747
+ *
748
+ * @example
749
+ * ```ts
750
+ * import { createListenerMiddleware } from '@reduxjs/toolkit'
751
+ *
752
+ * const listenerMiddleware = createListenerMiddleware()
753
+ *
754
+ * export const stopAppListening = listenerMiddleware.stopListening.withTypes<
755
+ * RootState,
756
+ * AppDispatch,
757
+ * ExtraArguments
758
+ * >()
759
+ * ```
760
+ *
761
+ * @template OverrideStateType - The specific type of state the middleware listener operates on.
762
+ * @template OverrideDispatchType - The specific type of the dispatch function.
763
+ * @template OverrideExtraArgument - The specific type of the extra object.
764
+ *
765
+ * @since 2.1.0
766
+ */
767
+ withTypes: <
768
+ OverrideStateType extends StateType,
769
+ OverrideDispatchType extends Dispatch = ThunkDispatch<
770
+ OverrideStateType,
771
+ unknown,
772
+ UnknownAction
773
+ >,
774
+ OverrideExtraArgument = unknown,
775
+ >() => TypedStopListening<
776
+ OverrideStateType,
777
+ OverrideDispatchType,
778
+ OverrideExtraArgument
779
+ >
780
+ }
781
+
782
+ /**
783
+ * A "pre-typed" version of `createListenerEntry`, so the listener args are well-typed
784
+ *
785
+ * @public
786
+ */
787
+ export type TypedCreateListenerEntry<
788
+ StateType,
789
+ DispatchType extends Dispatch = ThunkDispatch<
790
+ StateType,
791
+ unknown,
792
+ UnknownAction
793
+ >,
794
+ ExtraArgument = unknown,
795
+ > = AddListenerOverloads<
796
+ ListenerEntry<StateType, DispatchType>,
797
+ StateType,
798
+ DispatchType,
799
+ ExtraArgument
800
+ > & {
801
+ /**
802
+ * Creates a "pre-typed" version of `createListenerEntry`
803
+ * where the `state`, `dispatch` and `extra` types are predefined.
804
+ *
805
+ * This allows you to set the `state`, `dispatch` and `extra` types once, eliminating
806
+ * the need to specify them with every `createListenerEntry` call.
807
+ *
808
+ * @returns A pre-typed `createListenerEntry` with the state, dispatch and extra
809
+ * types already defined.
810
+ *
811
+ * @example
812
+ * ```ts
813
+ * import { createListenerEntry } from '@reduxjs/toolkit'
814
+ *
815
+ * export const createAppListenerEntry = createListenerEntry.withTypes<
816
+ * RootState,
817
+ * AppDispatch,
818
+ * ExtraArguments
819
+ * >()
820
+ * ```
821
+ *
822
+ * @template OverrideStateType - The specific type of state the middleware listener operates on.
823
+ * @template OverrideDispatchType - The specific type of the dispatch function.
824
+ * @template OverrideExtraArgument - The specific type of the extra object.
825
+ *
826
+ * @since 2.1.0
827
+ */
828
+ withTypes: <
829
+ OverrideStateType extends StateType,
830
+ OverrideDispatchType extends Dispatch = ThunkDispatch<
831
+ OverrideStateType,
832
+ unknown,
833
+ UnknownAction
834
+ >,
835
+ OverrideExtraArgument = unknown,
836
+ >() => TypedStopListening<
837
+ OverrideStateType,
838
+ OverrideDispatchType,
839
+ OverrideExtraArgument
840
+ >
841
+ }
842
+
843
+ /**
844
+ * Internal Types
845
+ */
846
+
847
+ /**
848
+ * An single listener entry
849
+ *
850
+ * @internal
851
+ */
852
+ export type ListenerEntry<
853
+ State = unknown,
854
+ DispatchType extends Dispatch = Dispatch,
855
+ > = {
856
+ id: string
857
+ effect: ListenerEffect<any, State, DispatchType>
858
+ unsubscribe: () => void
859
+ pending: Set<AbortController>
860
+ type?: string
861
+ predicate: ListenerPredicate<UnknownAction, State>
862
+ }
863
+
864
+ /**
865
+ * A shorthand form of the accepted args, solely so that `createListenerEntry` has validly-typed conditional logic when checking the options contents
866
+ * @internal
867
+ */
868
+ export type FallbackAddListenerOptions = {
869
+ actionCreator?: TypedActionCreatorWithMatchFunction<string>
870
+ type?: string
871
+ matcher?: MatchFunction<any>
872
+ predicate?: ListenerPredicate<any, any>
873
+ } & { effect: ListenerEffect<any, any, any> }
874
+
875
+ /**
876
+ * Utility Types
877
+ */
878
+
879
+ /** @public */
880
+ export type GuardedType<T> = T extends (x: any, ...args: any[]) => x is infer T
881
+ ? T
882
+ : never
883
+
884
+ /** @public */
885
+ export type ListenerPredicateGuardedActionType<T> =
886
+ T extends ListenerPredicate<infer ActionType, any> ? ActionType : never