@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,748 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
21
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
+ // If the importer is in node compatibility mode or this is not an ESM
23
+ // file that has been converted to a CommonJS file using a Babel-
24
+ // compatible transform (i.e. "__esModule" has not been set), then set
25
+ // "default" to the CommonJS "module.exports" for node compatibility.
26
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
27
+ mod
28
+ ));
29
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
30
+
31
+ // src/query/react/index.ts
32
+ var react_exports = {};
33
+ __export(react_exports, {
34
+ ApiProvider: () => ApiProvider,
35
+ UNINITIALIZED_VALUE: () => UNINITIALIZED_VALUE,
36
+ createApi: () => createApi,
37
+ reactHooksModule: () => reactHooksModule,
38
+ reactHooksModuleName: () => reactHooksModuleName
39
+ });
40
+ module.exports = __toCommonJS(react_exports);
41
+
42
+ // src/query/react/rtkqImports.ts
43
+ var import_query = require("@reduxjs/toolkit/query");
44
+
45
+ // src/query/react/module.ts
46
+ var import_toolkit2 = require("@reduxjs/toolkit");
47
+ var import_react_redux2 = require("react-redux");
48
+ var import_reselect = require("reselect");
49
+
50
+ // src/query/utils/capitalize.ts
51
+ function capitalize(str) {
52
+ return str.replace(str[0], str[0].toUpperCase());
53
+ }
54
+
55
+ // src/query/utils/countObjectKeys.ts
56
+ function countObjectKeys(obj) {
57
+ let count = 0;
58
+ for (const _key in obj) {
59
+ count++;
60
+ }
61
+ return count;
62
+ }
63
+
64
+ // src/query/endpointDefinitions.ts
65
+ var ENDPOINT_QUERY = "query" /* query */;
66
+ var ENDPOINT_MUTATION = "mutation" /* mutation */;
67
+ var ENDPOINT_INFINITEQUERY = "infinitequery" /* infinitequery */;
68
+ function isQueryDefinition(e) {
69
+ return e.type === ENDPOINT_QUERY;
70
+ }
71
+ function isMutationDefinition(e) {
72
+ return e.type === ENDPOINT_MUTATION;
73
+ }
74
+ function isInfiniteQueryDefinition(e) {
75
+ return e.type === ENDPOINT_INFINITEQUERY;
76
+ }
77
+
78
+ // src/query/tsHelpers.ts
79
+ function safeAssign(target, ...args) {
80
+ return Object.assign(target, ...args);
81
+ }
82
+
83
+ // src/query/react/buildHooks.ts
84
+ var import_toolkit = require("@reduxjs/toolkit");
85
+
86
+ // src/query/react/constants.ts
87
+ var UNINITIALIZED_VALUE = /* @__PURE__ */ Symbol();
88
+
89
+ // src/query/react/reactImports.ts
90
+ var import_react = require("react");
91
+
92
+ // src/query/react/reactReduxImports.ts
93
+ var import_react_redux = require("react-redux");
94
+
95
+ // src/query/react/useSerializedStableValue.ts
96
+ function useStableQueryArgs(queryArgs) {
97
+ const cache = (0, import_react.useRef)(queryArgs);
98
+ const copy = (0, import_react.useMemo)(() => (0, import_query.copyWithStructuralSharing)(cache.current, queryArgs), [queryArgs]);
99
+ (0, import_react.useEffect)(() => {
100
+ if (cache.current !== copy) {
101
+ cache.current = copy;
102
+ }
103
+ }, [copy]);
104
+ return copy;
105
+ }
106
+
107
+ // src/query/react/useShallowStableValue.ts
108
+ function useShallowStableValue(value) {
109
+ const cache = (0, import_react.useRef)(value);
110
+ (0, import_react.useEffect)(() => {
111
+ if (!(0, import_react_redux.shallowEqual)(cache.current, value)) {
112
+ cache.current = value;
113
+ }
114
+ }, [value]);
115
+ return (0, import_react_redux.shallowEqual)(cache.current, value) ? cache.current : value;
116
+ }
117
+
118
+ // src/query/react/buildHooks.ts
119
+ var canUseDOM = () => !!(typeof window !== "undefined" && typeof window.document !== "undefined" && typeof window.document.createElement !== "undefined");
120
+ var isDOM = /* @__PURE__ */ canUseDOM();
121
+ var isRunningInReactNative = () => typeof navigator !== "undefined" && navigator.product === "ReactNative";
122
+ var isReactNative = /* @__PURE__ */ isRunningInReactNative();
123
+ var getUseIsomorphicLayoutEffect = () => isDOM || isReactNative ? import_react.useLayoutEffect : import_react.useEffect;
124
+ var useIsomorphicLayoutEffect = /* @__PURE__ */ getUseIsomorphicLayoutEffect();
125
+ var noPendingQueryStateSelector = (selected) => {
126
+ if (selected.isUninitialized) {
127
+ return {
128
+ ...selected,
129
+ isUninitialized: false,
130
+ isFetching: true,
131
+ isLoading: selected.data !== void 0 ? false : true,
132
+ // This is the one place where we still have to use `QueryStatus` as an enum,
133
+ // since it's the only reference in the React package and not in the core.
134
+ status: import_query.QueryStatus.pending
135
+ };
136
+ }
137
+ return selected;
138
+ };
139
+ function pick(obj, ...keys) {
140
+ const ret = {};
141
+ keys.forEach((key) => {
142
+ ret[key] = obj[key];
143
+ });
144
+ return ret;
145
+ }
146
+ var COMMON_HOOK_DEBUG_FIELDS = ["data", "status", "isLoading", "isSuccess", "isError", "error"];
147
+ function buildHooks({
148
+ api,
149
+ moduleOptions: {
150
+ batch,
151
+ hooks: {
152
+ useDispatch,
153
+ useSelector,
154
+ useStore
155
+ },
156
+ unstable__sideEffectsInRender,
157
+ createSelector
158
+ },
159
+ serializeQueryArgs,
160
+ context
161
+ }) {
162
+ const usePossiblyImmediateEffect = unstable__sideEffectsInRender ? (cb) => cb() : import_react.useEffect;
163
+ const unsubscribePromiseRef = (ref) => ref.current?.unsubscribe?.();
164
+ const endpointDefinitions = context.endpointDefinitions;
165
+ return {
166
+ buildQueryHooks,
167
+ buildInfiniteQueryHooks,
168
+ buildMutationHook,
169
+ usePrefetch
170
+ };
171
+ function queryStatePreSelector(currentState, lastResult, queryArgs) {
172
+ if (lastResult?.endpointName && currentState.isUninitialized) {
173
+ const {
174
+ endpointName
175
+ } = lastResult;
176
+ const endpointDefinition = endpointDefinitions[endpointName];
177
+ if (queryArgs !== import_query.skipToken && serializeQueryArgs({
178
+ queryArgs: lastResult.originalArgs,
179
+ endpointDefinition,
180
+ endpointName
181
+ }) === serializeQueryArgs({
182
+ queryArgs,
183
+ endpointDefinition,
184
+ endpointName
185
+ })) lastResult = void 0;
186
+ }
187
+ let data = currentState.isSuccess ? currentState.data : lastResult?.data;
188
+ if (data === void 0) data = currentState.data;
189
+ const hasData = data !== void 0;
190
+ const isFetching = currentState.isLoading;
191
+ const isLoading = (!lastResult || lastResult.isLoading || lastResult.isUninitialized) && !hasData && isFetching;
192
+ const isSuccess = currentState.isSuccess || hasData && (isFetching && !lastResult?.isError || currentState.isUninitialized);
193
+ return {
194
+ ...currentState,
195
+ data,
196
+ currentData: currentState.data,
197
+ isFetching,
198
+ isLoading,
199
+ isSuccess
200
+ };
201
+ }
202
+ function infiniteQueryStatePreSelector(currentState, lastResult, queryArgs) {
203
+ if (lastResult?.endpointName && currentState.isUninitialized) {
204
+ const {
205
+ endpointName
206
+ } = lastResult;
207
+ const endpointDefinition = endpointDefinitions[endpointName];
208
+ if (queryArgs !== import_query.skipToken && serializeQueryArgs({
209
+ queryArgs: lastResult.originalArgs,
210
+ endpointDefinition,
211
+ endpointName
212
+ }) === serializeQueryArgs({
213
+ queryArgs,
214
+ endpointDefinition,
215
+ endpointName
216
+ })) lastResult = void 0;
217
+ }
218
+ let data = currentState.isSuccess ? currentState.data : lastResult?.data;
219
+ if (data === void 0) data = currentState.data;
220
+ const hasData = data !== void 0;
221
+ const isFetching = currentState.isLoading;
222
+ const isLoading = (!lastResult || lastResult.isLoading || lastResult.isUninitialized) && !hasData && isFetching;
223
+ const isSuccess = currentState.isSuccess || hasData && (isFetching && !lastResult?.isError || currentState.isUninitialized);
224
+ return {
225
+ ...currentState,
226
+ data,
227
+ currentData: currentState.data,
228
+ isFetching,
229
+ isLoading,
230
+ isSuccess
231
+ };
232
+ }
233
+ function usePrefetch(endpointName, defaultOptions) {
234
+ const dispatch = useDispatch();
235
+ const stableDefaultOptions = useShallowStableValue(defaultOptions);
236
+ return (0, import_react.useCallback)((arg, options) => dispatch(api.util.prefetch(endpointName, arg, {
237
+ ...stableDefaultOptions,
238
+ ...options
239
+ })), [endpointName, dispatch, stableDefaultOptions]);
240
+ }
241
+ function useQuerySubscriptionCommonImpl(endpointName, arg, {
242
+ refetchOnReconnect,
243
+ refetchOnFocus,
244
+ refetchOnMountOrArgChange,
245
+ skip = false,
246
+ pollingInterval = 0,
247
+ skipPollingIfUnfocused = false,
248
+ ...rest
249
+ } = {}) {
250
+ const {
251
+ initiate
252
+ } = api.endpoints[endpointName];
253
+ const dispatch = useDispatch();
254
+ const subscriptionSelectorsRef = (0, import_react.useRef)(void 0);
255
+ if (!subscriptionSelectorsRef.current) {
256
+ const returnedValue = dispatch(api.internalActions.internal_getRTKQSubscriptions());
257
+ if (true) {
258
+ if (typeof returnedValue !== "object" || typeof returnedValue?.type === "string") {
259
+ throw new Error(false ? _formatProdErrorMessage(37) : `Warning: Middleware for RTK-Query API at reducerPath "${api.reducerPath}" has not been added to the store.
260
+ You must add the middleware for RTK-Query to function correctly!`);
261
+ }
262
+ }
263
+ subscriptionSelectorsRef.current = returnedValue;
264
+ }
265
+ const stableArg = useStableQueryArgs(skip ? import_query.skipToken : arg);
266
+ const stableSubscriptionOptions = useShallowStableValue({
267
+ refetchOnReconnect,
268
+ refetchOnFocus,
269
+ pollingInterval,
270
+ skipPollingIfUnfocused
271
+ });
272
+ const initialPageParam = rest.initialPageParam;
273
+ const stableInitialPageParam = useShallowStableValue(initialPageParam);
274
+ const refetchCachedPages = rest.refetchCachedPages;
275
+ const stableRefetchCachedPages = useShallowStableValue(refetchCachedPages);
276
+ const promiseRef = (0, import_react.useRef)(void 0);
277
+ let {
278
+ queryCacheKey,
279
+ requestId
280
+ } = promiseRef.current || {};
281
+ let currentRenderHasSubscription = false;
282
+ if (queryCacheKey && requestId) {
283
+ currentRenderHasSubscription = subscriptionSelectorsRef.current.isRequestSubscribed(queryCacheKey, requestId);
284
+ }
285
+ const subscriptionRemoved = !currentRenderHasSubscription && promiseRef.current !== void 0;
286
+ usePossiblyImmediateEffect(() => {
287
+ if (subscriptionRemoved) {
288
+ promiseRef.current = void 0;
289
+ }
290
+ }, [subscriptionRemoved]);
291
+ usePossiblyImmediateEffect(() => {
292
+ const lastPromise = promiseRef.current;
293
+ if (typeof process !== "undefined" && false) {
294
+ console.log(subscriptionRemoved);
295
+ }
296
+ if (stableArg === import_query.skipToken) {
297
+ lastPromise?.unsubscribe();
298
+ promiseRef.current = void 0;
299
+ return;
300
+ }
301
+ const lastSubscriptionOptions = promiseRef.current?.subscriptionOptions;
302
+ if (!lastPromise || lastPromise.arg !== stableArg) {
303
+ lastPromise?.unsubscribe();
304
+ const promise = dispatch(initiate(stableArg, {
305
+ subscriptionOptions: stableSubscriptionOptions,
306
+ forceRefetch: refetchOnMountOrArgChange,
307
+ ...isInfiniteQueryDefinition(endpointDefinitions[endpointName]) ? {
308
+ initialPageParam: stableInitialPageParam,
309
+ refetchCachedPages: stableRefetchCachedPages
310
+ } : {}
311
+ }));
312
+ promiseRef.current = promise;
313
+ } else if (stableSubscriptionOptions !== lastSubscriptionOptions) {
314
+ lastPromise.updateSubscriptionOptions(stableSubscriptionOptions);
315
+ }
316
+ }, [dispatch, initiate, refetchOnMountOrArgChange, stableArg, stableSubscriptionOptions, subscriptionRemoved, stableInitialPageParam, stableRefetchCachedPages, endpointName]);
317
+ return [promiseRef, dispatch, initiate, stableSubscriptionOptions];
318
+ }
319
+ function buildUseQueryState(endpointName, preSelector) {
320
+ const useQueryState = (arg, {
321
+ skip = false,
322
+ selectFromResult
323
+ } = {}) => {
324
+ const {
325
+ select
326
+ } = api.endpoints[endpointName];
327
+ const stableArg = useStableQueryArgs(skip ? import_query.skipToken : arg);
328
+ const lastValue = (0, import_react.useRef)(void 0);
329
+ const selectDefaultResult = (0, import_react.useMemo)(() => (
330
+ // Normally ts-ignores are bad and should be avoided, but we're
331
+ // already casting this selector to be `Selector<any>` anyway,
332
+ // so the inconsistencies don't matter here
333
+ // @ts-ignore
334
+ createSelector([
335
+ // @ts-ignore
336
+ select(stableArg),
337
+ (_, lastResult) => lastResult,
338
+ (_) => stableArg
339
+ ], preSelector, {
340
+ memoizeOptions: {
341
+ resultEqualityCheck: import_react_redux.shallowEqual
342
+ }
343
+ })
344
+ ), [select, stableArg]);
345
+ const querySelector = (0, import_react.useMemo)(() => selectFromResult ? createSelector([selectDefaultResult], selectFromResult, {
346
+ devModeChecks: {
347
+ identityFunctionCheck: "never"
348
+ }
349
+ }) : selectDefaultResult, [selectDefaultResult, selectFromResult]);
350
+ const currentState = useSelector((state) => querySelector(state, lastValue.current), import_react_redux.shallowEqual);
351
+ const store = useStore();
352
+ const newLastValue = selectDefaultResult(store.getState(), lastValue.current);
353
+ useIsomorphicLayoutEffect(() => {
354
+ lastValue.current = newLastValue;
355
+ }, [newLastValue]);
356
+ return currentState;
357
+ };
358
+ return useQueryState;
359
+ }
360
+ function usePromiseRefUnsubscribeOnUnmount(promiseRef) {
361
+ (0, import_react.useEffect)(() => {
362
+ return () => {
363
+ unsubscribePromiseRef(promiseRef);
364
+ promiseRef.current = void 0;
365
+ };
366
+ }, [promiseRef]);
367
+ }
368
+ function refetchOrErrorIfUnmounted(promiseRef) {
369
+ if (!promiseRef.current) throw new Error(false ? _formatProdErrorMessage2(38) : "Cannot refetch a query that has not been started yet.");
370
+ return promiseRef.current.refetch();
371
+ }
372
+ function buildQueryHooks(endpointName) {
373
+ const useQuerySubscription = (arg, options = {}) => {
374
+ const [promiseRef] = useQuerySubscriptionCommonImpl(endpointName, arg, options);
375
+ usePromiseRefUnsubscribeOnUnmount(promiseRef);
376
+ return (0, import_react.useMemo)(() => ({
377
+ /**
378
+ * A method to manually refetch data for the query
379
+ */
380
+ refetch: () => refetchOrErrorIfUnmounted(promiseRef)
381
+ }), [promiseRef]);
382
+ };
383
+ const useLazyQuerySubscription = ({
384
+ refetchOnReconnect,
385
+ refetchOnFocus,
386
+ pollingInterval = 0,
387
+ skipPollingIfUnfocused = false
388
+ } = {}) => {
389
+ const {
390
+ initiate
391
+ } = api.endpoints[endpointName];
392
+ const dispatch = useDispatch();
393
+ const [arg, setArg] = (0, import_react.useState)(UNINITIALIZED_VALUE);
394
+ const promiseRef = (0, import_react.useRef)(void 0);
395
+ const stableSubscriptionOptions = useShallowStableValue({
396
+ refetchOnReconnect,
397
+ refetchOnFocus,
398
+ pollingInterval,
399
+ skipPollingIfUnfocused
400
+ });
401
+ usePossiblyImmediateEffect(() => {
402
+ const lastSubscriptionOptions = promiseRef.current?.subscriptionOptions;
403
+ if (stableSubscriptionOptions !== lastSubscriptionOptions) {
404
+ promiseRef.current?.updateSubscriptionOptions(stableSubscriptionOptions);
405
+ }
406
+ }, [stableSubscriptionOptions]);
407
+ const subscriptionOptionsRef = (0, import_react.useRef)(stableSubscriptionOptions);
408
+ usePossiblyImmediateEffect(() => {
409
+ subscriptionOptionsRef.current = stableSubscriptionOptions;
410
+ }, [stableSubscriptionOptions]);
411
+ const trigger = (0, import_react.useCallback)(function(arg2, preferCacheValue = false) {
412
+ let promise;
413
+ batch(() => {
414
+ unsubscribePromiseRef(promiseRef);
415
+ promiseRef.current = promise = dispatch(initiate(arg2, {
416
+ subscriptionOptions: subscriptionOptionsRef.current,
417
+ forceRefetch: !preferCacheValue
418
+ }));
419
+ setArg(arg2);
420
+ });
421
+ return promise;
422
+ }, [dispatch, initiate]);
423
+ const reset = (0, import_react.useCallback)(() => {
424
+ if (promiseRef.current?.queryCacheKey) {
425
+ dispatch(api.internalActions.removeQueryResult({
426
+ queryCacheKey: promiseRef.current?.queryCacheKey
427
+ }));
428
+ }
429
+ }, [dispatch]);
430
+ (0, import_react.useEffect)(() => {
431
+ return () => {
432
+ unsubscribePromiseRef(promiseRef);
433
+ };
434
+ }, []);
435
+ (0, import_react.useEffect)(() => {
436
+ if (arg !== UNINITIALIZED_VALUE && !promiseRef.current) {
437
+ trigger(arg, true);
438
+ }
439
+ }, [arg, trigger]);
440
+ return (0, import_react.useMemo)(() => [trigger, arg, {
441
+ reset
442
+ }], [trigger, arg, reset]);
443
+ };
444
+ const useQueryState = buildUseQueryState(endpointName, queryStatePreSelector);
445
+ return {
446
+ useQueryState,
447
+ useQuerySubscription,
448
+ useLazyQuerySubscription,
449
+ useLazyQuery(options) {
450
+ const [trigger, arg, {
451
+ reset
452
+ }] = useLazyQuerySubscription(options);
453
+ const queryStateResults = useQueryState(arg, {
454
+ ...options,
455
+ skip: arg === UNINITIALIZED_VALUE
456
+ });
457
+ const info = (0, import_react.useMemo)(() => ({
458
+ lastArg: arg
459
+ }), [arg]);
460
+ return (0, import_react.useMemo)(() => [trigger, {
461
+ ...queryStateResults,
462
+ reset
463
+ }, info], [trigger, queryStateResults, reset, info]);
464
+ },
465
+ useQuery(arg, options) {
466
+ const querySubscriptionResults = useQuerySubscription(arg, options);
467
+ const queryStateResults = useQueryState(arg, {
468
+ selectFromResult: arg === import_query.skipToken || options?.skip ? void 0 : noPendingQueryStateSelector,
469
+ ...options
470
+ });
471
+ const debugValue = pick(queryStateResults, ...COMMON_HOOK_DEBUG_FIELDS);
472
+ (0, import_react.useDebugValue)(debugValue);
473
+ return (0, import_react.useMemo)(() => ({
474
+ ...queryStateResults,
475
+ ...querySubscriptionResults
476
+ }), [queryStateResults, querySubscriptionResults]);
477
+ }
478
+ };
479
+ }
480
+ function buildInfiniteQueryHooks(endpointName) {
481
+ const useInfiniteQuerySubscription = (arg, options = {}) => {
482
+ const [promiseRef, dispatch, initiate, stableSubscriptionOptions] = useQuerySubscriptionCommonImpl(endpointName, arg, options);
483
+ const subscriptionOptionsRef = (0, import_react.useRef)(stableSubscriptionOptions);
484
+ usePossiblyImmediateEffect(() => {
485
+ subscriptionOptionsRef.current = stableSubscriptionOptions;
486
+ }, [stableSubscriptionOptions]);
487
+ const hookRefetchCachedPages = options.refetchCachedPages;
488
+ const stableHookRefetchCachedPages = useShallowStableValue(hookRefetchCachedPages);
489
+ const trigger = (0, import_react.useCallback)(function(arg2, direction) {
490
+ let promise;
491
+ batch(() => {
492
+ unsubscribePromiseRef(promiseRef);
493
+ promiseRef.current = promise = dispatch(initiate(arg2, {
494
+ subscriptionOptions: subscriptionOptionsRef.current,
495
+ direction
496
+ }));
497
+ });
498
+ return promise;
499
+ }, [promiseRef, dispatch, initiate]);
500
+ usePromiseRefUnsubscribeOnUnmount(promiseRef);
501
+ const stableArg = useStableQueryArgs(options.skip ? import_query.skipToken : arg);
502
+ const refetch = (0, import_react.useCallback)((options2) => {
503
+ if (!promiseRef.current) throw new Error(false ? _formatProdErrorMessage3(38) : "Cannot refetch a query that has not been started yet.");
504
+ const mergedOptions = {
505
+ refetchCachedPages: options2?.refetchCachedPages ?? stableHookRefetchCachedPages
506
+ };
507
+ return promiseRef.current.refetch(mergedOptions);
508
+ }, [promiseRef, stableHookRefetchCachedPages]);
509
+ return (0, import_react.useMemo)(() => {
510
+ const fetchNextPage = () => {
511
+ return trigger(stableArg, "forward");
512
+ };
513
+ const fetchPreviousPage = () => {
514
+ return trigger(stableArg, "backward");
515
+ };
516
+ return {
517
+ trigger,
518
+ /**
519
+ * A method to manually refetch data for the query
520
+ */
521
+ refetch,
522
+ fetchNextPage,
523
+ fetchPreviousPage
524
+ };
525
+ }, [refetch, trigger, stableArg]);
526
+ };
527
+ const useInfiniteQueryState = buildUseQueryState(endpointName, infiniteQueryStatePreSelector);
528
+ return {
529
+ useInfiniteQueryState,
530
+ useInfiniteQuerySubscription,
531
+ useInfiniteQuery(arg, options) {
532
+ const {
533
+ refetch,
534
+ fetchNextPage,
535
+ fetchPreviousPage
536
+ } = useInfiniteQuerySubscription(arg, options);
537
+ const queryStateResults = useInfiniteQueryState(arg, {
538
+ selectFromResult: arg === import_query.skipToken || options?.skip ? void 0 : noPendingQueryStateSelector,
539
+ ...options
540
+ });
541
+ const debugValue = pick(queryStateResults, ...COMMON_HOOK_DEBUG_FIELDS, "hasNextPage", "hasPreviousPage");
542
+ (0, import_react.useDebugValue)(debugValue);
543
+ return (0, import_react.useMemo)(() => ({
544
+ ...queryStateResults,
545
+ fetchNextPage,
546
+ fetchPreviousPage,
547
+ refetch
548
+ }), [queryStateResults, fetchNextPage, fetchPreviousPage, refetch]);
549
+ }
550
+ };
551
+ }
552
+ function buildMutationHook(name) {
553
+ return ({
554
+ selectFromResult,
555
+ fixedCacheKey
556
+ } = {}) => {
557
+ const {
558
+ select,
559
+ initiate
560
+ } = api.endpoints[name];
561
+ const dispatch = useDispatch();
562
+ const [promise, setPromise] = (0, import_react.useState)();
563
+ (0, import_react.useEffect)(() => () => {
564
+ if (!promise?.arg.fixedCacheKey) {
565
+ promise?.reset();
566
+ }
567
+ }, [promise]);
568
+ const triggerMutation = (0, import_react.useCallback)(function(arg) {
569
+ const promise2 = dispatch(initiate(arg, {
570
+ fixedCacheKey
571
+ }));
572
+ setPromise(promise2);
573
+ return promise2;
574
+ }, [dispatch, initiate, fixedCacheKey]);
575
+ const {
576
+ requestId
577
+ } = promise || {};
578
+ const selectDefaultResult = (0, import_react.useMemo)(() => select({
579
+ fixedCacheKey,
580
+ requestId: promise?.requestId
581
+ }), [fixedCacheKey, promise, select]);
582
+ const mutationSelector = (0, import_react.useMemo)(() => selectFromResult ? createSelector([selectDefaultResult], selectFromResult) : selectDefaultResult, [selectFromResult, selectDefaultResult]);
583
+ const currentState = useSelector(mutationSelector, import_react_redux.shallowEqual);
584
+ const originalArgs = fixedCacheKey == null ? promise?.arg.originalArgs : void 0;
585
+ const reset = (0, import_react.useCallback)(() => {
586
+ batch(() => {
587
+ if (promise) {
588
+ setPromise(void 0);
589
+ }
590
+ if (fixedCacheKey) {
591
+ dispatch(api.internalActions.removeMutationResult({
592
+ requestId,
593
+ fixedCacheKey
594
+ }));
595
+ }
596
+ });
597
+ }, [dispatch, fixedCacheKey, promise, requestId]);
598
+ const debugValue = pick(currentState, ...COMMON_HOOK_DEBUG_FIELDS, "endpointName");
599
+ (0, import_react.useDebugValue)(debugValue);
600
+ const finalState = (0, import_react.useMemo)(() => ({
601
+ ...currentState,
602
+ originalArgs,
603
+ reset
604
+ }), [currentState, originalArgs, reset]);
605
+ return (0, import_react.useMemo)(() => [triggerMutation, finalState], [triggerMutation, finalState]);
606
+ };
607
+ }
608
+ }
609
+
610
+ // src/query/react/module.ts
611
+ var reactHooksModuleName = /* @__PURE__ */ Symbol();
612
+ var reactHooksModule = ({
613
+ batch = import_react_redux2.batch,
614
+ hooks = {
615
+ useDispatch: import_react_redux2.useDispatch,
616
+ useSelector: import_react_redux2.useSelector,
617
+ useStore: import_react_redux2.useStore
618
+ },
619
+ createSelector = import_reselect.createSelector,
620
+ unstable__sideEffectsInRender = false,
621
+ ...rest
622
+ } = {}) => {
623
+ if (true) {
624
+ const hookNames = ["useDispatch", "useSelector", "useStore"];
625
+ let warned = false;
626
+ for (const hookName of hookNames) {
627
+ if (countObjectKeys(rest) > 0) {
628
+ if (rest[hookName]) {
629
+ if (!warned) {
630
+ console.warn("As of RTK 2.0, the hooks now need to be specified as one object, provided under a `hooks` key:\n`reactHooksModule({ hooks: { useDispatch, useSelector, useStore } })`");
631
+ warned = true;
632
+ }
633
+ }
634
+ hooks[hookName] = rest[hookName];
635
+ }
636
+ if (typeof hooks[hookName] !== "function") {
637
+ throw new Error(false ? _formatProdErrorMessage4(36) : `When using custom hooks for context, all ${hookNames.length} hooks need to be provided: ${hookNames.join(", ")}.
638
+ Hook ${hookName} was either not provided or not a function.`);
639
+ }
640
+ }
641
+ }
642
+ return {
643
+ name: reactHooksModuleName,
644
+ init(api, {
645
+ serializeQueryArgs
646
+ }, context) {
647
+ const anyApi = api;
648
+ const {
649
+ buildQueryHooks,
650
+ buildInfiniteQueryHooks,
651
+ buildMutationHook,
652
+ usePrefetch
653
+ } = buildHooks({
654
+ api,
655
+ moduleOptions: {
656
+ batch,
657
+ hooks,
658
+ unstable__sideEffectsInRender,
659
+ createSelector
660
+ },
661
+ serializeQueryArgs,
662
+ context
663
+ });
664
+ safeAssign(anyApi, {
665
+ usePrefetch
666
+ });
667
+ safeAssign(context, {
668
+ batch
669
+ });
670
+ return {
671
+ injectEndpoint(endpointName, definition) {
672
+ if (isQueryDefinition(definition)) {
673
+ const {
674
+ useQuery,
675
+ useLazyQuery,
676
+ useLazyQuerySubscription,
677
+ useQueryState,
678
+ useQuerySubscription
679
+ } = buildQueryHooks(endpointName);
680
+ safeAssign(anyApi.endpoints[endpointName], {
681
+ useQuery,
682
+ useLazyQuery,
683
+ useLazyQuerySubscription,
684
+ useQueryState,
685
+ useQuerySubscription
686
+ });
687
+ api[`use${capitalize(endpointName)}Query`] = useQuery;
688
+ api[`useLazy${capitalize(endpointName)}Query`] = useLazyQuery;
689
+ }
690
+ if (isMutationDefinition(definition)) {
691
+ const useMutation = buildMutationHook(endpointName);
692
+ safeAssign(anyApi.endpoints[endpointName], {
693
+ useMutation
694
+ });
695
+ api[`use${capitalize(endpointName)}Mutation`] = useMutation;
696
+ } else if (isInfiniteQueryDefinition(definition)) {
697
+ const {
698
+ useInfiniteQuery,
699
+ useInfiniteQuerySubscription,
700
+ useInfiniteQueryState
701
+ } = buildInfiniteQueryHooks(endpointName);
702
+ safeAssign(anyApi.endpoints[endpointName], {
703
+ useInfiniteQuery,
704
+ useInfiniteQuerySubscription,
705
+ useInfiniteQueryState
706
+ });
707
+ api[`use${capitalize(endpointName)}InfiniteQuery`] = useInfiniteQuery;
708
+ }
709
+ }
710
+ };
711
+ }
712
+ };
713
+ };
714
+
715
+ // src/query/react/index.ts
716
+ __reExport(react_exports, require("@reduxjs/toolkit/query"), module.exports);
717
+
718
+ // src/query/react/ApiProvider.tsx
719
+ var import_toolkit3 = require("@reduxjs/toolkit");
720
+ var React = __toESM(require("react"));
721
+ function ApiProvider(props) {
722
+ const context = props.context || import_react_redux.ReactReduxContext;
723
+ const existingContext = (0, import_react.useContext)(context);
724
+ if (existingContext) {
725
+ throw new Error(false ? _formatProdErrorMessage5(35) : "Existing Redux context detected. If you already have a store set up, please use the traditional Redux setup.");
726
+ }
727
+ const [store] = React.useState(() => (0, import_toolkit3.configureStore)({
728
+ reducer: {
729
+ [props.api.reducerPath]: props.api.reducer
730
+ },
731
+ middleware: (gDM) => gDM().concat(props.api.middleware)
732
+ }));
733
+ (0, import_react.useEffect)(() => props.setupListeners === false ? void 0 : (0, import_query.setupListeners)(store.dispatch, props.setupListeners), [props.setupListeners, store.dispatch]);
734
+ return /* @__PURE__ */ React.createElement(import_react_redux.Provider, { store, context }, props.children);
735
+ }
736
+
737
+ // src/query/react/index.ts
738
+ var createApi = /* @__PURE__ */ (0, import_query.buildCreateApi)((0, import_query.coreModule)(), reactHooksModule());
739
+ // Annotate the CommonJS export names for ESM import in node:
740
+ 0 && (module.exports = {
741
+ ApiProvider,
742
+ UNINITIALIZED_VALUE,
743
+ createApi,
744
+ reactHooksModule,
745
+ reactHooksModuleName,
746
+ ...require("@reduxjs/toolkit/query")
747
+ });
748
+ //# sourceMappingURL=rtk-query-react.development.cjs.map