| [a762898] | 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/reactImports.ts
|
|---|
| 87 | var import_react = require("react");
|
|---|
| 88 |
|
|---|
| 89 | // src/query/react/reactReduxImports.ts
|
|---|
| 90 | var import_react_redux = require("react-redux");
|
|---|
| 91 |
|
|---|
| 92 | // src/query/react/constants.ts
|
|---|
| 93 | var UNINITIALIZED_VALUE = Symbol();
|
|---|
| 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 || isFetching && hasData;
|
|---|
| 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 |
|---|