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