1 | var __defProp = Object.defineProperty;
|
2 | var __defProps = Object.defineProperties;
|
3 | var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
4 | var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
5 | var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6 | var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
7 | var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
8 | var __spreadValues = (a, b) => {
|
9 | for (var prop in b || (b = {}))
|
10 | if (__hasOwnProp.call(b, prop))
|
11 | __defNormalProp(a, prop, b[prop]);
|
12 | if (__getOwnPropSymbols)
|
13 | for (var prop of __getOwnPropSymbols(b)) {
|
14 | if (__propIsEnum.call(b, prop))
|
15 | __defNormalProp(a, prop, b[prop]);
|
16 | }
|
17 | return a;
|
18 | };
|
19 | var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
20 |
|
21 | import { coreModule, buildCreateApi } from "@reduxjs/toolkit/query";
|
22 |
|
23 | import { createSelector } from "@reduxjs/toolkit";
|
24 | import { useCallback, useEffect as useEffect2, useLayoutEffect, useMemo, useRef as useRef2, useState } from "react";
|
25 | import { QueryStatus, skipToken } from "@reduxjs/toolkit/query";
|
26 | import { shallowEqual as shallowEqual2 } from "react-redux";
|
27 |
|
28 | import { useEffect, useRef } from "react";
|
29 | import { shallowEqual } from "react-redux";
|
30 | function useShallowStableValue(value) {
|
31 | const cache = useRef(value);
|
32 | useEffect(() => {
|
33 | if (!shallowEqual(cache.current, value)) {
|
34 | cache.current = value;
|
35 | }
|
36 | }, [value]);
|
37 | return shallowEqual(cache.current, value) ? cache.current : value;
|
38 | }
|
39 |
|
40 | var UNINITIALIZED_VALUE = Symbol();
|
41 |
|
42 | var useIsomorphicLayoutEffect = typeof window !== "undefined" && typeof window.document !== "undefined" && typeof window.document.createElement !== "undefined" ? useLayoutEffect : useEffect2;
|
43 | var defaultQueryStateSelector = (x) => x;
|
44 | var defaultMutationStateSelector = (x) => x;
|
45 | var queryStatePreSelector = (currentState, lastResult) => {
|
46 | let data = currentState.isSuccess ? currentState.data : lastResult == null ? void 0 : lastResult.data;
|
47 | if (data === void 0)
|
48 | data = currentState.data;
|
49 | const hasData = data !== void 0;
|
50 | const isFetching = currentState.isLoading;
|
51 | const isLoading = !hasData && isFetching;
|
52 | const isSuccess = currentState.isSuccess || isFetching && hasData;
|
53 | return __spreadProps(__spreadValues({}, currentState), {
|
54 | data,
|
55 | isFetching,
|
56 | isLoading,
|
57 | isSuccess
|
58 | });
|
59 | };
|
60 | var noPendingQueryStateSelector = (selected) => {
|
61 | if (selected.isUninitialized) {
|
62 | return __spreadProps(__spreadValues({}, selected), {
|
63 | isUninitialized: false,
|
64 | isFetching: true,
|
65 | isLoading: selected.data !== void 0 ? false : true,
|
66 | status: QueryStatus.pending
|
67 | });
|
68 | }
|
69 | return selected;
|
70 | };
|
71 | function buildHooks({ api, moduleOptions: { batch, useDispatch, useSelector, useStore } }) {
|
72 | return { buildQueryHooks, buildMutationHook, usePrefetch };
|
73 | function usePrefetch(endpointName, defaultOptions) {
|
74 | const dispatch = useDispatch();
|
75 | const stableDefaultOptions = useShallowStableValue(defaultOptions);
|
76 | return useCallback((arg, options) => dispatch(api.util.prefetch(endpointName, arg, __spreadValues(__spreadValues({}, stableDefaultOptions), options))), [endpointName, dispatch, stableDefaultOptions]);
|
77 | }
|
78 | function buildQueryHooks(name) {
|
79 | const useQuerySubscription = (arg, { refetchOnReconnect, refetchOnFocus, refetchOnMountOrArgChange, skip = false, pollingInterval = 0 } = {}) => {
|
80 | const { initiate } = api.endpoints[name];
|
81 | const dispatch = useDispatch();
|
82 | const stableArg = useShallowStableValue(skip ? skipToken : arg);
|
83 | const stableSubscriptionOptions = useShallowStableValue({
|
84 | refetchOnReconnect,
|
85 | refetchOnFocus,
|
86 | pollingInterval
|
87 | });
|
88 | const promiseRef = useRef2();
|
89 | useEffect2(() => {
|
90 | var _a;
|
91 | const lastPromise = promiseRef.current;
|
92 | if (stableArg === skipToken) {
|
93 | lastPromise == null ? void 0 : lastPromise.unsubscribe();
|
94 | promiseRef.current = void 0;
|
95 | return;
|
96 | }
|
97 | const lastSubscriptionOptions = (_a = promiseRef.current) == null ? void 0 : _a.subscriptionOptions;
|
98 | if (!lastPromise || lastPromise.arg !== stableArg) {
|
99 | lastPromise == null ? void 0 : lastPromise.unsubscribe();
|
100 | const promise = dispatch(initiate(stableArg, {
|
101 | subscriptionOptions: stableSubscriptionOptions,
|
102 | forceRefetch: refetchOnMountOrArgChange
|
103 | }));
|
104 | promiseRef.current = promise;
|
105 | }
|
106 | else if (stableSubscriptionOptions !== lastSubscriptionOptions) {
|
107 | lastPromise.updateSubscriptionOptions(stableSubscriptionOptions);
|
108 | }
|
109 | }, [
|
110 | dispatch,
|
111 | initiate,
|
112 | refetchOnMountOrArgChange,
|
113 | stableArg,
|
114 | stableSubscriptionOptions
|
115 | ]);
|
116 | useEffect2(() => {
|
117 | return () => {
|
118 | var _a;
|
119 | (_a = promiseRef.current) == null ? void 0 : _a.unsubscribe();
|
120 | promiseRef.current = void 0;
|
121 | };
|
122 | }, []);
|
123 | return useMemo(() => ({
|
124 | refetch: () => {
|
125 | var _a;
|
126 | return void ((_a = promiseRef.current) == null ? void 0 : _a.refetch());
|
127 | }
|
128 | }), []);
|
129 | };
|
130 | const useLazyQuerySubscription = ({ refetchOnReconnect, refetchOnFocus, pollingInterval = 0 } = {}) => {
|
131 | const { initiate } = api.endpoints[name];
|
132 | const dispatch = useDispatch();
|
133 | const [arg, setArg] = useState(UNINITIALIZED_VALUE);
|
134 | const promiseRef = useRef2();
|
135 | const stableSubscriptionOptions = useShallowStableValue({
|
136 | refetchOnReconnect,
|
137 | refetchOnFocus,
|
138 | pollingInterval
|
139 | });
|
140 | useEffect2(() => {
|
141 | var _a, _b;
|
142 | const lastSubscriptionOptions = (_a = promiseRef.current) == null ? void 0 : _a.subscriptionOptions;
|
143 | if (stableSubscriptionOptions !== lastSubscriptionOptions) {
|
144 | (_b = promiseRef.current) == null ? void 0 : _b.updateSubscriptionOptions(stableSubscriptionOptions);
|
145 | }
|
146 | }, [stableSubscriptionOptions]);
|
147 | const subscriptionOptionsRef = useRef2(stableSubscriptionOptions);
|
148 | useEffect2(() => {
|
149 | subscriptionOptionsRef.current = stableSubscriptionOptions;
|
150 | }, [stableSubscriptionOptions]);
|
151 | const trigger = useCallback(function (arg2, preferCacheValue = false) {
|
152 | batch(() => {
|
153 | var _a;
|
154 | (_a = promiseRef.current) == null ? void 0 : _a.unsubscribe();
|
155 | promiseRef.current = dispatch(initiate(arg2, {
|
156 | subscriptionOptions: subscriptionOptionsRef.current,
|
157 | forceRefetch: !preferCacheValue
|
158 | }));
|
159 | setArg(arg2);
|
160 | });
|
161 | }, [dispatch, initiate]);
|
162 | useEffect2(() => {
|
163 | return () => {
|
164 | var _a;
|
165 | (_a = promiseRef == null ? void 0 : promiseRef.current) == null ? void 0 : _a.unsubscribe();
|
166 | };
|
167 | }, []);
|
168 | useEffect2(() => {
|
169 | if (arg !== UNINITIALIZED_VALUE && !promiseRef.current) {
|
170 | trigger(arg, true);
|
171 | }
|
172 | }, [arg, trigger]);
|
173 | return useMemo(() => [trigger, arg], [trigger, arg]);
|
174 | };
|
175 | const useQueryState = (arg, { skip = false, selectFromResult = defaultQueryStateSelector } = {}) => {
|
176 | const { select } = api.endpoints[name];
|
177 | const stableArg = useShallowStableValue(skip ? skipToken : arg);
|
178 | const lastValue = useRef2();
|
179 | const selectDefaultResult = useMemo(() => createSelector([select(stableArg), (_, lastResult) => lastResult], queryStatePreSelector), [select, stableArg]);
|
180 | const querySelector = useMemo(() => createSelector([selectDefaultResult], selectFromResult), [selectDefaultResult, selectFromResult]);
|
181 | const currentState = useSelector((state) => querySelector(state, lastValue.current), shallowEqual2);
|
182 | const store = useStore();
|
183 | const newLastValue = selectDefaultResult(store.getState(), lastValue.current);
|
184 | useIsomorphicLayoutEffect(() => {
|
185 | lastValue.current = newLastValue;
|
186 | }, [newLastValue]);
|
187 | return currentState;
|
188 | };
|
189 | return {
|
190 | useQueryState,
|
191 | useQuerySubscription,
|
192 | useLazyQuerySubscription,
|
193 | useLazyQuery(options) {
|
194 | const [trigger, arg] = useLazyQuerySubscription(options);
|
195 | const queryStateResults = useQueryState(arg, __spreadProps(__spreadValues({}, options), {
|
196 | skip: arg === UNINITIALIZED_VALUE
|
197 | }));
|
198 | const info = useMemo(() => ({ lastArg: arg }), [arg]);
|
199 | return useMemo(() => [trigger, queryStateResults, info], [trigger, queryStateResults, info]);
|
200 | },
|
201 | useQuery(arg, options) {
|
202 | const querySubscriptionResults = useQuerySubscription(arg, options);
|
203 | const queryStateResults = useQueryState(arg, __spreadValues({
|
204 | selectFromResult: arg === skipToken || (options == null ? void 0 : options.skip) ? void 0 : noPendingQueryStateSelector
|
205 | }, options));
|
206 | return useMemo(() => __spreadValues(__spreadValues({}, queryStateResults), querySubscriptionResults), [queryStateResults, querySubscriptionResults]);
|
207 | }
|
208 | };
|
209 | }
|
210 | function buildMutationHook(name) {
|
211 | return ({ selectFromResult = defaultMutationStateSelector } = {}) => {
|
212 | var _a;
|
213 | const { select, initiate } = api.endpoints[name];
|
214 | const dispatch = useDispatch();
|
215 | const [requestId, setRequestId] = useState();
|
216 | const promiseRef = useRef2();
|
217 | useEffect2(() => {
|
218 | return () => {
|
219 | var _a2;
|
220 | (_a2 = promiseRef.current) == null ? void 0 : _a2.unsubscribe();
|
221 | promiseRef.current = void 0;
|
222 | };
|
223 | }, []);
|
224 | const triggerMutation = useCallback(function (arg) {
|
225 | let promise;
|
226 | batch(() => {
|
227 | var _a2;
|
228 | (_a2 = promiseRef == null ? void 0 : promiseRef.current) == null ? void 0 : _a2.unsubscribe();
|
229 | promise = dispatch(initiate(arg));
|
230 | promiseRef.current = promise;
|
231 | setRequestId(promise.requestId);
|
232 | });
|
233 | return promise;
|
234 | }, [dispatch, initiate]);
|
235 | const mutationSelector = useMemo(() => createSelector([select(requestId || skipToken)], (subState) => selectFromResult(subState)), [select, requestId, selectFromResult]);
|
236 | const currentState = useSelector(mutationSelector, shallowEqual2);
|
237 | const originalArgs = (_a = promiseRef.current) == null ? void 0 : _a.arg.originalArgs;
|
238 | const finalState = useMemo(() => __spreadProps(__spreadValues({}, currentState), {
|
239 | originalArgs
|
240 | }), [currentState, originalArgs]);
|
241 | return useMemo(() => [triggerMutation, finalState], [triggerMutation, finalState]);
|
242 | };
|
243 | }
|
244 | }
|
245 |
|
246 | var DefinitionType;
|
247 | (function (DefinitionType2) {
|
248 | DefinitionType2["query"] = "query";
|
249 | DefinitionType2["mutation"] = "mutation";
|
250 | })(DefinitionType || (DefinitionType = {}));
|
251 | function isQueryDefinition(e) {
|
252 | return e.type === DefinitionType.query;
|
253 | }
|
254 | function isMutationDefinition(e) {
|
255 | return e.type === DefinitionType.mutation;
|
256 | }
|
257 |
|
258 | function capitalize(str) {
|
259 | return str.replace(str[0], str[0].toUpperCase());
|
260 | }
|
261 |
|
262 | function safeAssign(target, ...args) {
|
263 | Object.assign(target, ...args);
|
264 | }
|
265 |
|
266 | import { useDispatch as rrUseDispatch, useSelector as rrUseSelector, useStore as rrUseStore, batch as rrBatch } from "react-redux";
|
267 | var reactHooksModuleName = Symbol();
|
268 | var reactHooksModule = ({ batch = rrBatch, useDispatch = rrUseDispatch, useSelector = rrUseSelector, useStore = rrUseStore } = {}) => ({
|
269 | name: reactHooksModuleName,
|
270 | init(api, options, context) {
|
271 | const anyApi = api;
|
272 | const { buildQueryHooks, buildMutationHook, usePrefetch } = buildHooks({
|
273 | api,
|
274 | moduleOptions: { batch, useDispatch, useSelector, useStore }
|
275 | });
|
276 | safeAssign(anyApi, { usePrefetch });
|
277 | safeAssign(context, { batch });
|
278 | return {
|
279 | injectEndpoint(endpointName, definition) {
|
280 | if (isQueryDefinition(definition)) {
|
281 | const { useQuery, useLazyQuery, useLazyQuerySubscription, useQueryState, useQuerySubscription } = buildQueryHooks(endpointName);
|
282 | safeAssign(anyApi.endpoints[endpointName], {
|
283 | useQuery,
|
284 | useLazyQuery,
|
285 | useLazyQuerySubscription,
|
286 | useQueryState,
|
287 | useQuerySubscription
|
288 | });
|
289 | api[`use${capitalize(endpointName)}Query`] = useQuery;
|
290 | api[`useLazy${capitalize(endpointName)}Query`] = useLazyQuery;
|
291 | }
|
292 | else if (isMutationDefinition(definition)) {
|
293 | const useMutation = buildMutationHook(endpointName);
|
294 | safeAssign(anyApi.endpoints[endpointName], {
|
295 | useMutation
|
296 | });
|
297 | api[`use${capitalize(endpointName)}Mutation`] = useMutation;
|
298 | }
|
299 | }
|
300 | };
|
301 | }
|
302 | });
|
303 |
|
304 | export * from "@reduxjs/toolkit/query";
|
305 |
|
306 | import { configureStore } from "@reduxjs/toolkit";
|
307 | import React from "react";
|
308 | import { Provider } from "react-redux";
|
309 | import { setupListeners } from "@reduxjs/toolkit/query";
|
310 | function ApiProvider(props) {
|
311 | const [store] = React.useState(() => configureStore({
|
312 | reducer: {
|
313 | [props.api.reducerPath]: props.api.reducer
|
314 | },
|
315 | middleware: (gDM) => gDM().concat(props.api.middleware)
|
316 | }));
|
317 | setupListeners(store.dispatch, props.setupListeners);
|
318 | return React.createElement(Provider, {
|
319 | store,
|
320 | context: props.context
|
321 | }, props.children);
|
322 | }
|
323 |
|
324 | var createApi = buildCreateApi(coreModule(), reactHooksModule());
|
325 | export { ApiProvider, createApi, reactHooksModule };
|
326 |
|
\ | No newline at end of file |