@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,241 @@
1
+ import type { Action, ActionCreator, StoreEnhancer } from 'redux'
2
+ import { compose } from './reduxImports'
3
+
4
+ /**
5
+ * @public
6
+ */
7
+ export interface DevToolsEnhancerOptions {
8
+ /**
9
+ * the instance name to be showed on the monitor page. Default value is `document.title`.
10
+ * If not specified and there's no document title, it will consist of `tabId` and `instanceId`.
11
+ */
12
+ name?: string
13
+ /**
14
+ * action creators functions to be available in the Dispatcher.
15
+ */
16
+ actionCreators?: ActionCreator<any>[] | { [key: string]: ActionCreator<any> }
17
+ /**
18
+ * if more than one action is dispatched in the indicated interval, all new actions will be collected and sent at once.
19
+ * It is the joint between performance and speed. When set to `0`, all actions will be sent instantly.
20
+ * Set it to a higher value when experiencing perf issues (also `maxAge` to a lower value).
21
+ *
22
+ * @default 500 ms.
23
+ */
24
+ latency?: number
25
+ /**
26
+ * (> 1) - maximum allowed actions to be stored in the history tree. The oldest actions are removed once maxAge is reached. It's critical for performance.
27
+ *
28
+ * @default 50
29
+ */
30
+ maxAge?: number
31
+ /**
32
+ * Customizes how actions and state are serialized and deserialized. Can be a boolean or object. If given a boolean, the behavior is the same as if you
33
+ * were to pass an object and specify `options` as a boolean. Giving an object allows fine-grained customization using the `replacer` and `reviver`
34
+ * functions.
35
+ */
36
+ serialize?:
37
+ | boolean
38
+ | {
39
+ /**
40
+ * - `undefined` - will use regular `JSON.stringify` to send data (it's the fast mode).
41
+ * - `false` - will handle also circular references.
42
+ * - `true` - will handle also date, regex, undefined, error objects, symbols, maps, sets and functions.
43
+ * - object, which contains `date`, `regex`, `undefined`, `error`, `symbol`, `map`, `set` and `function` keys.
44
+ * For each of them you can indicate if to include (by setting as `true`).
45
+ * For `function` key you can also specify a custom function which handles serialization.
46
+ * See [`jsan`](https://github.com/kolodny/jsan) for more details.
47
+ */
48
+ options?:
49
+ | undefined
50
+ | boolean
51
+ | {
52
+ date?: true
53
+ regex?: true
54
+ undefined?: true
55
+ error?: true
56
+ symbol?: true
57
+ map?: true
58
+ set?: true
59
+ function?: true | ((fn: (...args: any[]) => any) => string)
60
+ }
61
+ /**
62
+ * [JSON replacer function](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter) used for both actions and states stringify.
63
+ * In addition, you can specify a data type by adding a [`__serializedType__`](https://github.com/zalmoxisus/remotedev-serialize/blob/master/helpers/index.js#L4)
64
+ * key. So you can deserialize it back while importing or persisting data.
65
+ * Moreover, it will also [show a nice preview showing the provided custom type](https://cloud.githubusercontent.com/assets/7957859/21814330/a17d556a-d761-11e6-85ef-159dd12f36c5.png):
66
+ */
67
+ replacer?: (key: string, value: unknown) => any
68
+ /**
69
+ * [JSON `reviver` function](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Using_the_reviver_parameter)
70
+ * used for parsing the imported actions and states. See [`remotedev-serialize`](https://github.com/zalmoxisus/remotedev-serialize/blob/master/immutable/serialize.js#L8-L41)
71
+ * as an example on how to serialize special data types and get them back.
72
+ */
73
+ reviver?: (key: string, value: unknown) => any
74
+ /**
75
+ * Automatically serialize/deserialize immutablejs via [remotedev-serialize](https://github.com/zalmoxisus/remotedev-serialize).
76
+ * Just pass the Immutable library. It will support all ImmutableJS structures. You can even export them into a file and get them back.
77
+ * The only exception is `Record` class, for which you should pass this in addition the references to your classes in `refs`.
78
+ */
79
+ immutable?: any
80
+ /**
81
+ * ImmutableJS `Record` classes used to make possible restore its instances back when importing, persisting...
82
+ */
83
+ refs?: any
84
+ }
85
+ /**
86
+ * function which takes `action` object and id number as arguments, and should return `action` object back.
87
+ */
88
+ actionSanitizer?: <A extends Action>(action: A, id: number) => A
89
+ /**
90
+ * function which takes `state` object and index as arguments, and should return `state` object back.
91
+ */
92
+ stateSanitizer?: <S>(state: S, index: number) => S
93
+ /**
94
+ * *string or array of strings as regex* - actions types to be hidden / shown in the monitors (while passed to the reducers).
95
+ * If `actionsAllowlist` specified, `actionsDenylist` is ignored.
96
+ */
97
+ actionsDenylist?: string | string[]
98
+ /**
99
+ * *string or array of strings as regex* - actions types to be hidden / shown in the monitors (while passed to the reducers).
100
+ * If `actionsAllowlist` specified, `actionsDenylist` is ignored.
101
+ */
102
+ actionsAllowlist?: string | string[]
103
+ /**
104
+ * called for every action before sending, takes `state` and `action` object, and returns `true` in case it allows sending the current data to the monitor.
105
+ * Use it as a more advanced version of `actionsDenylist`/`actionsAllowlist` parameters.
106
+ */
107
+ predicate?: <S, A extends Action>(state: S, action: A) => boolean
108
+ /**
109
+ * if specified as `false`, it will not record the changes till clicking on `Start recording` button.
110
+ * Available only for Redux enhancer, for others use `autoPause`.
111
+ *
112
+ * @default true
113
+ */
114
+ shouldRecordChanges?: boolean
115
+ /**
116
+ * if specified, whenever clicking on `Pause recording` button and there are actions in the history log, will add this action type.
117
+ * If not specified, will commit when paused. Available only for Redux enhancer.
118
+ *
119
+ * @default "@@PAUSED""
120
+ */
121
+ pauseActionType?: string
122
+ /**
123
+ * auto pauses when the extension’s window is not opened, and so has zero impact on your app when not in use.
124
+ * Not available for Redux enhancer (as it already does it but storing the data to be sent).
125
+ *
126
+ * @default false
127
+ */
128
+ autoPause?: boolean
129
+ /**
130
+ * if specified as `true`, it will not allow any non-monitor actions to be dispatched till clicking on `Unlock changes` button.
131
+ * Available only for Redux enhancer.
132
+ *
133
+ * @default false
134
+ */
135
+ shouldStartLocked?: boolean
136
+ /**
137
+ * if set to `false`, will not recompute the states on hot reloading (or on replacing the reducers). Available only for Redux enhancer.
138
+ *
139
+ * @default true
140
+ */
141
+ shouldHotReload?: boolean
142
+ /**
143
+ * if specified as `true`, whenever there's an exception in reducers, the monitors will show the error message, and next actions will not be dispatched.
144
+ *
145
+ * @default false
146
+ */
147
+ shouldCatchErrors?: boolean
148
+ /**
149
+ * If you want to restrict the extension, specify the features you allow.
150
+ * If not specified, all of the features are enabled. When set as an object, only those included as `true` will be allowed.
151
+ * Note that except `true`/`false`, `import` and `export` can be set as `custom` (which is by default for Redux enhancer), meaning that the importing/exporting occurs on the client side.
152
+ * Otherwise, you'll get/set the data right from the monitor part.
153
+ */
154
+ features?: {
155
+ /**
156
+ * start/pause recording of dispatched actions
157
+ */
158
+ pause?: boolean
159
+ /**
160
+ * lock/unlock dispatching actions and side effects
161
+ */
162
+ lock?: boolean
163
+ /**
164
+ * persist states on page reloading
165
+ */
166
+ persist?: boolean
167
+ /**
168
+ * export history of actions in a file
169
+ */
170
+ export?: boolean | 'custom'
171
+ /**
172
+ * import history of actions from a file
173
+ */
174
+ import?: boolean | 'custom'
175
+ /**
176
+ * jump back and forth (time traveling)
177
+ */
178
+ jump?: boolean
179
+ /**
180
+ * skip (cancel) actions
181
+ */
182
+ skip?: boolean
183
+ /**
184
+ * drag and drop actions in the history list
185
+ */
186
+ reorder?: boolean
187
+ /**
188
+ * dispatch custom actions or action creators
189
+ */
190
+ dispatch?: boolean
191
+ /**
192
+ * generate tests for the selected actions
193
+ */
194
+ test?: boolean
195
+ }
196
+ /**
197
+ * Set to true or a stacktrace-returning function to record call stack traces for dispatched actions.
198
+ * Defaults to false.
199
+ */
200
+ trace?: boolean | (<A extends Action>(action: A) => string)
201
+ /**
202
+ * The maximum number of stack trace entries to record per action. Defaults to 10.
203
+ */
204
+ traceLimit?: number
205
+ }
206
+
207
+ type Compose = typeof compose
208
+
209
+ interface ComposeWithDevTools {
210
+ (options: DevToolsEnhancerOptions): Compose
211
+ <StoreExt extends {}>(
212
+ ...funcs: StoreEnhancer<StoreExt>[]
213
+ ): StoreEnhancer<StoreExt>
214
+ }
215
+
216
+ /**
217
+ * @public
218
+ */
219
+ export const composeWithDevTools: ComposeWithDevTools =
220
+ typeof window !== 'undefined' &&
221
+ (window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
222
+ ? (window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
223
+ : function () {
224
+ if (arguments.length === 0) return undefined
225
+ if (typeof arguments[0] === 'object') return compose
226
+ return compose.apply(null, arguments as any as Function[])
227
+ }
228
+
229
+ /**
230
+ * @public
231
+ */
232
+ export const devToolsEnhancer: {
233
+ (options: DevToolsEnhancerOptions): StoreEnhancer<any>
234
+ } =
235
+ typeof window !== 'undefined' && (window as any).__REDUX_DEVTOOLS_EXTENSION__
236
+ ? (window as any).__REDUX_DEVTOOLS_EXTENSION__
237
+ : function () {
238
+ return function (noop) {
239
+ return noop
240
+ }
241
+ }
@@ -0,0 +1,93 @@
1
+ import type { Dispatch, Middleware, UnknownAction } from 'redux'
2
+ import { compose } from '../reduxImports'
3
+ import { createAction } from '../createAction'
4
+ import { isAllOf } from '../matchers'
5
+ import { nanoid } from '../nanoid'
6
+ import { getOrInsertComputed } from '../utils'
7
+ import type {
8
+ AddMiddleware,
9
+ DynamicMiddleware,
10
+ DynamicMiddlewareInstance,
11
+ MiddlewareEntry,
12
+ WithMiddleware,
13
+ } from './types'
14
+ export type {
15
+ DynamicMiddlewareInstance,
16
+ GetDispatchType as GetDispatch,
17
+ MiddlewareApiConfig,
18
+ } from './types'
19
+
20
+ const createMiddlewareEntry = <
21
+ State = any,
22
+ DispatchType extends Dispatch<UnknownAction> = Dispatch<UnknownAction>,
23
+ >(
24
+ middleware: Middleware<any, State, DispatchType>,
25
+ ): MiddlewareEntry<State, DispatchType> => ({
26
+ middleware,
27
+ applied: new Map(),
28
+ })
29
+
30
+ const matchInstance =
31
+ (instanceId: string) =>
32
+ (action: any): action is { meta: { instanceId: string } } =>
33
+ action?.meta?.instanceId === instanceId
34
+
35
+ export const createDynamicMiddleware = <
36
+ State = any,
37
+ DispatchType extends Dispatch<UnknownAction> = Dispatch<UnknownAction>,
38
+ >(): DynamicMiddlewareInstance<State, DispatchType> => {
39
+ const instanceId = nanoid()
40
+ const middlewareMap = new Map<
41
+ Middleware<any, State, DispatchType>,
42
+ MiddlewareEntry<State, DispatchType>
43
+ >()
44
+
45
+ const withMiddleware = Object.assign(
46
+ createAction(
47
+ 'dynamicMiddleware/add',
48
+ (...middlewares: Middleware<any, State, DispatchType>[]) => ({
49
+ payload: middlewares,
50
+ meta: {
51
+ instanceId,
52
+ },
53
+ }),
54
+ ),
55
+ { withTypes: () => withMiddleware },
56
+ ) as WithMiddleware<State, DispatchType>
57
+
58
+ const addMiddleware = Object.assign(
59
+ function addMiddleware(
60
+ ...middlewares: Middleware<any, State, DispatchType>[]
61
+ ) {
62
+ middlewares.forEach((middleware) => {
63
+ getOrInsertComputed(middlewareMap, middleware, createMiddlewareEntry)
64
+ })
65
+ },
66
+ { withTypes: () => addMiddleware },
67
+ ) as AddMiddleware<State, DispatchType>
68
+
69
+ const getFinalMiddleware: Middleware<{}, State, DispatchType> = (api) => {
70
+ const appliedMiddleware = Array.from(middlewareMap.values()).map((entry) =>
71
+ getOrInsertComputed(entry.applied, api, entry.middleware),
72
+ )
73
+ return compose(...appliedMiddleware)
74
+ }
75
+
76
+ const isWithMiddleware = isAllOf(withMiddleware, matchInstance(instanceId))
77
+
78
+ const middleware: DynamicMiddleware<State, DispatchType> =
79
+ (api) => (next) => (action) => {
80
+ if (isWithMiddleware(action)) {
81
+ addMiddleware(...action.payload)
82
+ return api.dispatch
83
+ }
84
+ return getFinalMiddleware(api)(next)(action)
85
+ }
86
+
87
+ return {
88
+ middleware,
89
+ addMiddleware,
90
+ withMiddleware,
91
+ instanceId,
92
+ }
93
+ }
@@ -0,0 +1,101 @@
1
+ import type {
2
+ DynamicMiddlewareInstance,
3
+ GetDispatch,
4
+ GetState,
5
+ MiddlewareApiConfig,
6
+ TSHelpersExtractDispatchExtensions,
7
+ } from '@reduxjs/toolkit'
8
+ import { createDynamicMiddleware as cDM } from '@reduxjs/toolkit'
9
+ import type { Context } from 'react'
10
+ import type { ReactReduxContextValue } from 'react-redux'
11
+ import {
12
+ createDispatchHook,
13
+ ReactReduxContext,
14
+ useDispatch as useDefaultDispatch,
15
+ } from 'react-redux'
16
+ import type { Action, Dispatch, Middleware, UnknownAction } from 'redux'
17
+
18
+ export type UseDispatchWithMiddlewareHook<
19
+ Middlewares extends Middleware<any, State, DispatchType>[] = [],
20
+ State = any,
21
+ DispatchType extends Dispatch<UnknownAction> = Dispatch<UnknownAction>,
22
+ > = () => TSHelpersExtractDispatchExtensions<Middlewares> & DispatchType
23
+
24
+ export type CreateDispatchWithMiddlewareHook<
25
+ State = any,
26
+ DispatchType extends Dispatch<UnknownAction> = Dispatch<UnknownAction>,
27
+ > = {
28
+ <
29
+ Middlewares extends [
30
+ Middleware<any, State, DispatchType>,
31
+ ...Middleware<any, State, DispatchType>[],
32
+ ],
33
+ >(
34
+ ...middlewares: Middlewares
35
+ ): UseDispatchWithMiddlewareHook<Middlewares, State, DispatchType>
36
+ withTypes<
37
+ MiddlewareConfig extends MiddlewareApiConfig,
38
+ >(): CreateDispatchWithMiddlewareHook<
39
+ GetState<MiddlewareConfig>,
40
+ GetDispatch<MiddlewareConfig>
41
+ >
42
+ }
43
+
44
+ type ActionFromDispatch<DispatchType extends Dispatch<Action>> =
45
+ DispatchType extends Dispatch<infer Action> ? Action : never
46
+
47
+ type ReactDynamicMiddlewareInstance<
48
+ State = any,
49
+ DispatchType extends Dispatch<UnknownAction> = Dispatch<UnknownAction>,
50
+ > = DynamicMiddlewareInstance<State, DispatchType> & {
51
+ createDispatchWithMiddlewareHookFactory: (
52
+ context?: Context<ReactReduxContextValue<
53
+ State,
54
+ ActionFromDispatch<DispatchType>
55
+ > | null>,
56
+ ) => CreateDispatchWithMiddlewareHook<State, DispatchType>
57
+ createDispatchWithMiddlewareHook: CreateDispatchWithMiddlewareHook<
58
+ State,
59
+ DispatchType
60
+ >
61
+ }
62
+
63
+ export const createDynamicMiddleware = <
64
+ State = any,
65
+ DispatchType extends Dispatch<UnknownAction> = Dispatch<UnknownAction>,
66
+ >(): ReactDynamicMiddlewareInstance<State, DispatchType> => {
67
+ const instance = cDM<State, DispatchType>()
68
+ const createDispatchWithMiddlewareHookFactory = (
69
+ // @ts-ignore
70
+ context: Context<ReactReduxContextValue<
71
+ State,
72
+ ActionFromDispatch<DispatchType>
73
+ > | null> = ReactReduxContext,
74
+ ) => {
75
+ const useDispatch =
76
+ context === ReactReduxContext
77
+ ? useDefaultDispatch
78
+ : createDispatchHook(context)
79
+ function createDispatchWithMiddlewareHook<
80
+ Middlewares extends Middleware<any, State, DispatchType>[],
81
+ >(...middlewares: Middlewares) {
82
+ instance.addMiddleware(...middlewares)
83
+ return useDispatch
84
+ }
85
+ createDispatchWithMiddlewareHook.withTypes = () =>
86
+ createDispatchWithMiddlewareHook
87
+ return createDispatchWithMiddlewareHook as CreateDispatchWithMiddlewareHook<
88
+ State,
89
+ DispatchType
90
+ >
91
+ }
92
+
93
+ const createDispatchWithMiddlewareHook =
94
+ createDispatchWithMiddlewareHookFactory()
95
+
96
+ return {
97
+ ...instance,
98
+ createDispatchWithMiddlewareHookFactory,
99
+ createDispatchWithMiddlewareHook,
100
+ }
101
+ }
@@ -0,0 +1,82 @@
1
+ import type { Dispatch, Middleware, MiddlewareAPI, UnknownAction } from 'redux'
2
+ import type { BaseActionCreator, PayloadAction } from '../createAction'
3
+ import type { GetState } from '../createAsyncThunk'
4
+ import type { ExtractDispatchExtensions, FallbackIfUnknown } from '../tsHelpers'
5
+
6
+ export type GetMiddlewareApi<MiddlewareApiConfig> = MiddlewareAPI<
7
+ GetDispatchType<MiddlewareApiConfig>,
8
+ GetState<MiddlewareApiConfig>
9
+ >
10
+
11
+ export type MiddlewareApiConfig = {
12
+ state?: unknown
13
+ dispatch?: Dispatch
14
+ }
15
+
16
+ // TODO: consolidate with cAT helpers?
17
+ export type GetDispatchType<MiddlewareApiConfig> = MiddlewareApiConfig extends {
18
+ dispatch: infer DispatchType
19
+ }
20
+ ? FallbackIfUnknown<DispatchType, Dispatch>
21
+ : Dispatch
22
+
23
+ export type AddMiddleware<
24
+ State = any,
25
+ DispatchType extends Dispatch<UnknownAction> = Dispatch<UnknownAction>,
26
+ > = {
27
+ (...middlewares: Middleware<any, State, DispatchType>[]): void
28
+ withTypes<MiddlewareConfig extends MiddlewareApiConfig>(): AddMiddleware<
29
+ GetState<MiddlewareConfig>,
30
+ GetDispatchType<MiddlewareConfig>
31
+ >
32
+ }
33
+
34
+ export type WithMiddleware<
35
+ State = any,
36
+ DispatchType extends Dispatch<UnknownAction> = Dispatch<UnknownAction>,
37
+ > = BaseActionCreator<
38
+ Middleware<any, State, DispatchType>[],
39
+ 'dynamicMiddleware/add',
40
+ { instanceId: string }
41
+ > & {
42
+ <Middlewares extends Middleware<any, State, DispatchType>[]>(
43
+ ...middlewares: Middlewares
44
+ ): PayloadAction<Middlewares, 'dynamicMiddleware/add', { instanceId: string }>
45
+ withTypes<MiddlewareConfig extends MiddlewareApiConfig>(): WithMiddleware<
46
+ GetState<MiddlewareConfig>,
47
+ GetDispatchType<MiddlewareConfig>
48
+ >
49
+ }
50
+
51
+ export interface DynamicDispatch {
52
+ // return a version of dispatch that knows about middleware
53
+ <Middlewares extends Middleware<any>[]>(
54
+ action: PayloadAction<Middlewares, 'dynamicMiddleware/add'>,
55
+ ): ExtractDispatchExtensions<Middlewares> & this
56
+ }
57
+
58
+ export type MiddlewareEntry<
59
+ State = unknown,
60
+ DispatchType extends Dispatch<UnknownAction> = Dispatch<UnknownAction>,
61
+ > = {
62
+ middleware: Middleware<any, State, DispatchType>
63
+ applied: Map<
64
+ MiddlewareAPI<DispatchType, State>,
65
+ ReturnType<Middleware<any, State, DispatchType>>
66
+ >
67
+ }
68
+
69
+ export type DynamicMiddleware<
70
+ State = unknown,
71
+ DispatchType extends Dispatch<UnknownAction> = Dispatch<UnknownAction>,
72
+ > = Middleware<DynamicDispatch, State, DispatchType>
73
+
74
+ export type DynamicMiddlewareInstance<
75
+ State = unknown,
76
+ DispatchType extends Dispatch<UnknownAction> = Dispatch<UnknownAction>,
77
+ > = {
78
+ middleware: DynamicMiddleware<State, DispatchType>
79
+ addMiddleware: AddMiddleware<State, DispatchType>
80
+ withMiddleware: WithMiddleware<State, DispatchType>
81
+ instanceId: string
82
+ }
@@ -0,0 +1,47 @@
1
+ import type { EntityAdapter, EntityId, EntityAdapterOptions } from './models'
2
+ import { createInitialStateFactory } from './entity_state'
3
+ import { createSelectorsFactory } from './state_selectors'
4
+ import { createSortedStateAdapter } from './sorted_state_adapter'
5
+ import { createUnsortedStateAdapter } from './unsorted_state_adapter'
6
+ import type { WithRequiredProp } from '../tsHelpers'
7
+
8
+ export function createEntityAdapter<T, Id extends EntityId>(
9
+ options: WithRequiredProp<EntityAdapterOptions<T, Id>, 'selectId'>,
10
+ ): EntityAdapter<T, Id>
11
+
12
+ export function createEntityAdapter<T extends { id: EntityId }>(
13
+ options?: Omit<EntityAdapterOptions<T, T['id']>, 'selectId'>,
14
+ ): EntityAdapter<T, T['id']>
15
+
16
+ /**
17
+ *
18
+ * @param options
19
+ *
20
+ * @public
21
+ */
22
+ export function createEntityAdapter<T>(
23
+ options: EntityAdapterOptions<T, EntityId> = {},
24
+ ): EntityAdapter<T, EntityId> {
25
+ const {
26
+ selectId,
27
+ sortComparer,
28
+ }: Required<EntityAdapterOptions<T, EntityId>> = {
29
+ sortComparer: false,
30
+ selectId: (instance: any) => instance.id,
31
+ ...options,
32
+ }
33
+
34
+ const stateAdapter = sortComparer
35
+ ? createSortedStateAdapter(selectId, sortComparer)
36
+ : createUnsortedStateAdapter(selectId)
37
+ const stateFactory = createInitialStateFactory(stateAdapter)
38
+ const selectorsFactory = createSelectorsFactory<T, EntityId>()
39
+
40
+ return {
41
+ selectId,
42
+ sortComparer,
43
+ ...stateFactory,
44
+ ...selectorsFactory,
45
+ ...stateAdapter,
46
+ }
47
+ }
@@ -0,0 +1,38 @@
1
+ import type {
2
+ EntityId,
3
+ EntityState,
4
+ EntityStateAdapter,
5
+ EntityStateFactory,
6
+ } from './models'
7
+
8
+ export function getInitialEntityState<T, Id extends EntityId>(): EntityState<
9
+ T,
10
+ Id
11
+ > {
12
+ return {
13
+ ids: [],
14
+ entities: {} as Record<Id, T>,
15
+ }
16
+ }
17
+
18
+ export function createInitialStateFactory<T, Id extends EntityId>(
19
+ stateAdapter: EntityStateAdapter<T, Id>,
20
+ ): EntityStateFactory<T, Id> {
21
+ function getInitialState(
22
+ state?: undefined,
23
+ entities?: readonly T[] | Record<Id, T>,
24
+ ): EntityState<T, Id>
25
+ function getInitialState<S extends object>(
26
+ additionalState: S,
27
+ entities?: readonly T[] | Record<Id, T>,
28
+ ): EntityState<T, Id> & S
29
+ function getInitialState(
30
+ additionalState: any = {},
31
+ entities?: readonly T[] | Record<Id, T>,
32
+ ): any {
33
+ const state = Object.assign(getInitialEntityState(), additionalState)
34
+ return entities ? stateAdapter.setAll(state, entities) : state
35
+ }
36
+
37
+ return { getInitialState }
38
+ }
@@ -0,0 +1,8 @@
1
+ export { createEntityAdapter } from './create_adapter'
2
+ export type {
3
+ EntityState,
4
+ EntityAdapter,
5
+ Update,
6
+ IdSelector,
7
+ Comparer,
8
+ } from './models'