| 1 | "use strict";
|
|---|
| 2 | var __defProp = Object.defineProperty;
|
|---|
| 3 | var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|---|
| 4 | var __getOwnPropNames = Object.getOwnPropertyNames;
|
|---|
| 5 | var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|---|
| 6 | var __export = (target, all) => {
|
|---|
| 7 | for (var name in all)
|
|---|
| 8 | __defProp(target, name, { get: all[name], enumerable: true });
|
|---|
| 9 | };
|
|---|
| 10 | var __copyProps = (to, from, except, desc) => {
|
|---|
| 11 | if (from && typeof from === "object" || typeof from === "function") {
|
|---|
| 12 | for (let key of __getOwnPropNames(from))
|
|---|
| 13 | if (!__hasOwnProp.call(to, key) && key !== except)
|
|---|
| 14 | __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|---|
| 15 | }
|
|---|
| 16 | return to;
|
|---|
| 17 | };
|
|---|
| 18 | var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|---|
| 19 |
|
|---|
| 20 | // src/query/index.ts
|
|---|
| 21 | var query_exports = {};
|
|---|
| 22 | __export(query_exports, {
|
|---|
| 23 | NamedSchemaError: () => NamedSchemaError,
|
|---|
| 24 | QueryStatus: () => QueryStatus,
|
|---|
| 25 | _NEVER: () => _NEVER,
|
|---|
| 26 | buildCreateApi: () => buildCreateApi,
|
|---|
| 27 | copyWithStructuralSharing: () => copyWithStructuralSharing,
|
|---|
| 28 | coreModule: () => coreModule,
|
|---|
| 29 | coreModuleName: () => coreModuleName,
|
|---|
| 30 | createApi: () => createApi,
|
|---|
| 31 | defaultSerializeQueryArgs: () => defaultSerializeQueryArgs,
|
|---|
| 32 | fakeBaseQuery: () => fakeBaseQuery,
|
|---|
| 33 | fetchBaseQuery: () => fetchBaseQuery,
|
|---|
| 34 | retry: () => retry,
|
|---|
| 35 | setupListeners: () => setupListeners,
|
|---|
| 36 | skipToken: () => skipToken
|
|---|
| 37 | });
|
|---|
| 38 | module.exports = __toCommonJS(query_exports);
|
|---|
| 39 |
|
|---|
| 40 | // src/query/core/apiState.ts
|
|---|
| 41 | var QueryStatus = /* @__PURE__ */ ((QueryStatus7) => {
|
|---|
| 42 | QueryStatus7["uninitialized"] = "uninitialized";
|
|---|
| 43 | QueryStatus7["pending"] = "pending";
|
|---|
| 44 | QueryStatus7["fulfilled"] = "fulfilled";
|
|---|
| 45 | QueryStatus7["rejected"] = "rejected";
|
|---|
| 46 | return QueryStatus7;
|
|---|
| 47 | })(QueryStatus || {});
|
|---|
| 48 | var STATUS_UNINITIALIZED = "uninitialized" /* uninitialized */;
|
|---|
| 49 | var STATUS_PENDING = "pending" /* pending */;
|
|---|
| 50 | var STATUS_FULFILLED = "fulfilled" /* fulfilled */;
|
|---|
| 51 | var STATUS_REJECTED = "rejected" /* rejected */;
|
|---|
| 52 | function getRequestStatusFlags(status) {
|
|---|
| 53 | return {
|
|---|
| 54 | status,
|
|---|
| 55 | isUninitialized: status === STATUS_UNINITIALIZED,
|
|---|
| 56 | isLoading: status === STATUS_PENDING,
|
|---|
| 57 | isSuccess: status === STATUS_FULFILLED,
|
|---|
| 58 | isError: status === STATUS_REJECTED
|
|---|
| 59 | };
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | // src/query/core/rtkImports.ts
|
|---|
| 63 | var import_toolkit = require("@reduxjs/toolkit");
|
|---|
| 64 |
|
|---|
| 65 | // src/query/utils/copyWithStructuralSharing.ts
|
|---|
| 66 | var isPlainObject2 = import_toolkit.isPlainObject;
|
|---|
| 67 | function copyWithStructuralSharing(oldObj, newObj) {
|
|---|
| 68 | if (oldObj === newObj || !(isPlainObject2(oldObj) && isPlainObject2(newObj) || Array.isArray(oldObj) && Array.isArray(newObj))) {
|
|---|
| 69 | return newObj;
|
|---|
| 70 | }
|
|---|
| 71 | const newKeys = Object.keys(newObj);
|
|---|
| 72 | const oldKeys = Object.keys(oldObj);
|
|---|
| 73 | let isSameObject = newKeys.length === oldKeys.length;
|
|---|
| 74 | const mergeObj = Array.isArray(newObj) ? [] : {};
|
|---|
| 75 | for (const key of newKeys) {
|
|---|
| 76 | mergeObj[key] = copyWithStructuralSharing(oldObj[key], newObj[key]);
|
|---|
| 77 | if (isSameObject) isSameObject = oldObj[key] === mergeObj[key];
|
|---|
| 78 | }
|
|---|
| 79 | return isSameObject ? oldObj : mergeObj;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | // src/query/utils/filterMap.ts
|
|---|
| 83 | function filterMap(array, predicate, mapper) {
|
|---|
| 84 | return array.reduce((acc, item, i) => {
|
|---|
| 85 | if (predicate(item, i)) {
|
|---|
| 86 | acc.push(mapper(item, i));
|
|---|
| 87 | }
|
|---|
| 88 | return acc;
|
|---|
| 89 | }, []).flat();
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | // src/query/utils/isAbsoluteUrl.ts
|
|---|
| 93 | function isAbsoluteUrl(url) {
|
|---|
| 94 | return new RegExp(`(^|:)//`).test(url);
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | // src/query/utils/isDocumentVisible.ts
|
|---|
| 98 | function isDocumentVisible() {
|
|---|
| 99 | if (typeof document === "undefined") {
|
|---|
| 100 | return true;
|
|---|
| 101 | }
|
|---|
| 102 | return document.visibilityState !== "hidden";
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | // src/query/utils/isNotNullish.ts
|
|---|
| 106 | function isNotNullish(v) {
|
|---|
| 107 | return v != null;
|
|---|
| 108 | }
|
|---|
| 109 | function filterNullishValues(map) {
|
|---|
| 110 | return [...map?.values() ?? []].filter(isNotNullish);
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | // src/query/utils/isOnline.ts
|
|---|
| 114 | function isOnline() {
|
|---|
| 115 | return typeof navigator === "undefined" ? true : navigator.onLine === void 0 ? true : navigator.onLine;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | // src/query/utils/joinUrls.ts
|
|---|
| 119 | var withoutTrailingSlash = (url) => url.replace(/\/$/, "");
|
|---|
| 120 | var withoutLeadingSlash = (url) => url.replace(/^\//, "");
|
|---|
| 121 | function joinUrls(base, url) {
|
|---|
| 122 | if (!base) {
|
|---|
| 123 | return url;
|
|---|
| 124 | }
|
|---|
| 125 | if (!url) {
|
|---|
| 126 | return base;
|
|---|
| 127 | }
|
|---|
| 128 | if (isAbsoluteUrl(url)) {
|
|---|
| 129 | return url;
|
|---|
| 130 | }
|
|---|
| 131 | const delimiter = base.endsWith("/") || !url.startsWith("?") ? "/" : "";
|
|---|
| 132 | base = withoutTrailingSlash(base);
|
|---|
| 133 | url = withoutLeadingSlash(url);
|
|---|
| 134 | return `${base}${delimiter}${url}`;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | // src/query/utils/getOrInsert.ts
|
|---|
| 138 | function getOrInsertComputed(map, key, compute) {
|
|---|
| 139 | if (map.has(key)) return map.get(key);
|
|---|
| 140 | return map.set(key, compute(key)).get(key);
|
|---|
| 141 | }
|
|---|
| 142 | var createNewMap = () => /* @__PURE__ */ new Map();
|
|---|
| 143 |
|
|---|
| 144 | // src/query/utils/signals.ts
|
|---|
| 145 | var timeoutSignal = (milliseconds) => {
|
|---|
| 146 | const abortController = new AbortController();
|
|---|
| 147 | setTimeout(() => {
|
|---|
| 148 | const message = "signal timed out";
|
|---|
| 149 | const name = "TimeoutError";
|
|---|
| 150 | abortController.abort(
|
|---|
| 151 | // some environments (React Native, Node) don't have DOMException
|
|---|
| 152 | typeof DOMException !== "undefined" ? new DOMException(message, name) : Object.assign(new Error(message), {
|
|---|
| 153 | name
|
|---|
| 154 | })
|
|---|
| 155 | );
|
|---|
| 156 | }, milliseconds);
|
|---|
| 157 | return abortController.signal;
|
|---|
| 158 | };
|
|---|
| 159 | var anySignal = (...signals) => {
|
|---|
| 160 | for (const signal of signals) if (signal.aborted) return AbortSignal.abort(signal.reason);
|
|---|
| 161 | const abortController = new AbortController();
|
|---|
| 162 | for (const signal of signals) {
|
|---|
| 163 | signal.addEventListener("abort", () => abortController.abort(signal.reason), {
|
|---|
| 164 | signal: abortController.signal,
|
|---|
| 165 | once: true
|
|---|
| 166 | });
|
|---|
| 167 | }
|
|---|
| 168 | return abortController.signal;
|
|---|
| 169 | };
|
|---|
| 170 |
|
|---|
| 171 | // src/query/fetchBaseQuery.ts
|
|---|
| 172 | var defaultFetchFn = (...args) => fetch(...args);
|
|---|
| 173 | var defaultValidateStatus = (response) => response.status >= 200 && response.status <= 299;
|
|---|
| 174 | var defaultIsJsonContentType = (headers) => (
|
|---|
| 175 | /*applicat*/
|
|---|
| 176 | /ion\/(vnd\.api\+)?json/.test(headers.get("content-type") || "")
|
|---|
| 177 | );
|
|---|
| 178 | function stripUndefined(obj) {
|
|---|
| 179 | if (!(0, import_toolkit.isPlainObject)(obj)) {
|
|---|
| 180 | return obj;
|
|---|
| 181 | }
|
|---|
| 182 | const copy = {
|
|---|
| 183 | ...obj
|
|---|
| 184 | };
|
|---|
| 185 | for (const [k, v] of Object.entries(copy)) {
|
|---|
| 186 | if (v === void 0) delete copy[k];
|
|---|
| 187 | }
|
|---|
| 188 | return copy;
|
|---|
| 189 | }
|
|---|
| 190 | var isJsonifiable = (body) => typeof body === "object" && ((0, import_toolkit.isPlainObject)(body) || Array.isArray(body) || typeof body.toJSON === "function");
|
|---|
| 191 | function fetchBaseQuery({
|
|---|
| 192 | baseUrl,
|
|---|
| 193 | prepareHeaders = (x) => x,
|
|---|
| 194 | fetchFn = defaultFetchFn,
|
|---|
| 195 | paramsSerializer,
|
|---|
| 196 | isJsonContentType = defaultIsJsonContentType,
|
|---|
| 197 | jsonContentType = "application/json",
|
|---|
| 198 | jsonReplacer,
|
|---|
| 199 | timeout: defaultTimeout,
|
|---|
| 200 | responseHandler: globalResponseHandler,
|
|---|
| 201 | validateStatus: globalValidateStatus,
|
|---|
| 202 | ...baseFetchOptions
|
|---|
| 203 | } = {}) {
|
|---|
| 204 | if (typeof fetch === "undefined" && fetchFn === defaultFetchFn) {
|
|---|
| 205 | console.warn("Warning: `fetch` is not available. Please supply a custom `fetchFn` property to use `fetchBaseQuery` on SSR environments.");
|
|---|
| 206 | }
|
|---|
| 207 | return async (arg, api, extraOptions) => {
|
|---|
| 208 | const {
|
|---|
| 209 | getState,
|
|---|
| 210 | extra,
|
|---|
| 211 | endpoint,
|
|---|
| 212 | forced,
|
|---|
| 213 | type
|
|---|
| 214 | } = api;
|
|---|
| 215 | let meta;
|
|---|
| 216 | let {
|
|---|
| 217 | url,
|
|---|
| 218 | headers = new Headers(baseFetchOptions.headers),
|
|---|
| 219 | params = void 0,
|
|---|
| 220 | responseHandler = globalResponseHandler ?? "json",
|
|---|
| 221 | validateStatus = globalValidateStatus ?? defaultValidateStatus,
|
|---|
| 222 | timeout = defaultTimeout,
|
|---|
| 223 | ...rest
|
|---|
| 224 | } = typeof arg == "string" ? {
|
|---|
| 225 | url: arg
|
|---|
| 226 | } : arg;
|
|---|
| 227 | let config = {
|
|---|
| 228 | ...baseFetchOptions,
|
|---|
| 229 | signal: timeout ? anySignal(api.signal, timeoutSignal(timeout)) : api.signal,
|
|---|
| 230 | ...rest
|
|---|
| 231 | };
|
|---|
| 232 | headers = new Headers(stripUndefined(headers));
|
|---|
| 233 | config.headers = await prepareHeaders(headers, {
|
|---|
| 234 | getState,
|
|---|
| 235 | arg,
|
|---|
| 236 | extra,
|
|---|
| 237 | endpoint,
|
|---|
| 238 | forced,
|
|---|
| 239 | type,
|
|---|
| 240 | extraOptions
|
|---|
| 241 | }) || headers;
|
|---|
| 242 | const bodyIsJsonifiable = isJsonifiable(config.body);
|
|---|
| 243 | if (config.body != null && !bodyIsJsonifiable && typeof config.body !== "string") {
|
|---|
| 244 | config.headers.delete("content-type");
|
|---|
| 245 | }
|
|---|
| 246 | if (!config.headers.has("content-type") && bodyIsJsonifiable) {
|
|---|
| 247 | config.headers.set("content-type", jsonContentType);
|
|---|
| 248 | }
|
|---|
| 249 | if (bodyIsJsonifiable && isJsonContentType(config.headers)) {
|
|---|
| 250 | config.body = JSON.stringify(config.body, jsonReplacer);
|
|---|
| 251 | }
|
|---|
| 252 | if (!config.headers.has("accept")) {
|
|---|
| 253 | if (responseHandler === "json") {
|
|---|
| 254 | config.headers.set("accept", "application/json");
|
|---|
| 255 | } else if (responseHandler === "text") {
|
|---|
| 256 | config.headers.set("accept", "text/plain, text/html, */*");
|
|---|
| 257 | }
|
|---|
| 258 | }
|
|---|
| 259 | if (params) {
|
|---|
| 260 | const divider = ~url.indexOf("?") ? "&" : "?";
|
|---|
| 261 | const query = paramsSerializer ? paramsSerializer(params) : new URLSearchParams(stripUndefined(params));
|
|---|
| 262 | url += divider + query;
|
|---|
| 263 | }
|
|---|
| 264 | url = joinUrls(baseUrl, url);
|
|---|
| 265 | const request = new Request(url, config);
|
|---|
| 266 | const requestClone = new Request(url, config);
|
|---|
| 267 | meta = {
|
|---|
| 268 | request: requestClone
|
|---|
| 269 | };
|
|---|
| 270 | let response;
|
|---|
| 271 | try {
|
|---|
| 272 | response = await fetchFn(request);
|
|---|
| 273 | } catch (e) {
|
|---|
| 274 | return {
|
|---|
| 275 | error: {
|
|---|
| 276 | status: (e instanceof Error || typeof DOMException !== "undefined" && e instanceof DOMException) && e.name === "TimeoutError" ? "TIMEOUT_ERROR" : "FETCH_ERROR",
|
|---|
| 277 | error: String(e)
|
|---|
| 278 | },
|
|---|
| 279 | meta
|
|---|
| 280 | };
|
|---|
| 281 | }
|
|---|
| 282 | const responseClone = response.clone();
|
|---|
| 283 | meta.response = responseClone;
|
|---|
| 284 | let resultData;
|
|---|
| 285 | let responseText = "";
|
|---|
| 286 | try {
|
|---|
| 287 | let handleResponseError;
|
|---|
| 288 | await Promise.all([
|
|---|
| 289 | handleResponse(response, responseHandler).then((r) => resultData = r, (e) => handleResponseError = e),
|
|---|
| 290 | // see https://github.com/node-fetch/node-fetch/issues/665#issuecomment-538995182
|
|---|
| 291 | // we *have* to "use up" both streams at the same time or they will stop running in node-fetch scenarios
|
|---|
| 292 | responseClone.text().then((r) => responseText = r, () => {
|
|---|
| 293 | })
|
|---|
| 294 | ]);
|
|---|
| 295 | if (handleResponseError) throw handleResponseError;
|
|---|
| 296 | } catch (e) {
|
|---|
| 297 | return {
|
|---|
| 298 | error: {
|
|---|
| 299 | status: "PARSING_ERROR",
|
|---|
| 300 | originalStatus: response.status,
|
|---|
| 301 | data: responseText,
|
|---|
| 302 | error: String(e)
|
|---|
| 303 | },
|
|---|
| 304 | meta
|
|---|
| 305 | };
|
|---|
| 306 | }
|
|---|
| 307 | return validateStatus(response, resultData) ? {
|
|---|
| 308 | data: resultData,
|
|---|
| 309 | meta
|
|---|
| 310 | } : {
|
|---|
| 311 | error: {
|
|---|
| 312 | status: response.status,
|
|---|
| 313 | data: resultData
|
|---|
| 314 | },
|
|---|
| 315 | meta
|
|---|
| 316 | };
|
|---|
| 317 | };
|
|---|
| 318 | async function handleResponse(response, responseHandler) {
|
|---|
| 319 | if (typeof responseHandler === "function") {
|
|---|
| 320 | return responseHandler(response);
|
|---|
| 321 | }
|
|---|
| 322 | if (responseHandler === "content-type") {
|
|---|
| 323 | responseHandler = isJsonContentType(response.headers) ? "json" : "text";
|
|---|
| 324 | }
|
|---|
| 325 | if (responseHandler === "json") {
|
|---|
| 326 | const text = await response.text();
|
|---|
| 327 | return text.length ? JSON.parse(text) : null;
|
|---|
| 328 | }
|
|---|
| 329 | return response.text();
|
|---|
| 330 | }
|
|---|
| 331 | }
|
|---|
| 332 |
|
|---|
| 333 | // src/query/HandledError.ts
|
|---|
| 334 | var HandledError = class {
|
|---|
| 335 | constructor(value, meta = void 0) {
|
|---|
| 336 | this.value = value;
|
|---|
| 337 | this.meta = meta;
|
|---|
| 338 | }
|
|---|
| 339 | };
|
|---|
| 340 |
|
|---|
| 341 | // src/query/retry.ts
|
|---|
| 342 | async function defaultBackoff(attempt = 0, maxRetries = 5, signal) {
|
|---|
| 343 | const attempts = Math.min(attempt, maxRetries);
|
|---|
| 344 | const timeout = ~~((Math.random() + 0.4) * (300 << attempts));
|
|---|
| 345 | await new Promise((resolve, reject) => {
|
|---|
| 346 | const timeoutId = setTimeout(() => resolve(), timeout);
|
|---|
| 347 | if (signal) {
|
|---|
| 348 | const abortHandler = () => {
|
|---|
| 349 | clearTimeout(timeoutId);
|
|---|
| 350 | reject(new Error("Aborted"));
|
|---|
| 351 | };
|
|---|
| 352 | if (signal.aborted) {
|
|---|
| 353 | clearTimeout(timeoutId);
|
|---|
| 354 | reject(new Error("Aborted"));
|
|---|
| 355 | } else {
|
|---|
| 356 | signal.addEventListener("abort", abortHandler, {
|
|---|
| 357 | once: true
|
|---|
| 358 | });
|
|---|
| 359 | }
|
|---|
| 360 | }
|
|---|
| 361 | });
|
|---|
| 362 | }
|
|---|
| 363 | function fail(error, meta) {
|
|---|
| 364 | throw Object.assign(new HandledError({
|
|---|
| 365 | error,
|
|---|
| 366 | meta
|
|---|
| 367 | }), {
|
|---|
| 368 | throwImmediately: true
|
|---|
| 369 | });
|
|---|
| 370 | }
|
|---|
| 371 | function failIfAborted(signal) {
|
|---|
| 372 | if (signal.aborted) {
|
|---|
| 373 | fail({
|
|---|
| 374 | status: "CUSTOM_ERROR",
|
|---|
| 375 | error: "Aborted"
|
|---|
| 376 | });
|
|---|
| 377 | }
|
|---|
| 378 | }
|
|---|
| 379 | var EMPTY_OPTIONS = {};
|
|---|
| 380 | var retryWithBackoff = (baseQuery, defaultOptions) => async (args, api, extraOptions) => {
|
|---|
| 381 | const possibleMaxRetries = [5, (defaultOptions || EMPTY_OPTIONS).maxRetries, (extraOptions || EMPTY_OPTIONS).maxRetries].filter((x) => x !== void 0);
|
|---|
| 382 | const [maxRetries] = possibleMaxRetries.slice(-1);
|
|---|
| 383 | const defaultRetryCondition = (_, __, {
|
|---|
| 384 | attempt
|
|---|
| 385 | }) => attempt <= maxRetries;
|
|---|
| 386 | const options = {
|
|---|
| 387 | maxRetries,
|
|---|
| 388 | backoff: defaultBackoff,
|
|---|
| 389 | retryCondition: defaultRetryCondition,
|
|---|
| 390 | ...defaultOptions,
|
|---|
| 391 | ...extraOptions
|
|---|
| 392 | };
|
|---|
| 393 | let retry2 = 0;
|
|---|
| 394 | while (true) {
|
|---|
| 395 | failIfAborted(api.signal);
|
|---|
| 396 | try {
|
|---|
| 397 | const result = await baseQuery(args, api, extraOptions);
|
|---|
| 398 | if (result.error) {
|
|---|
| 399 | throw new HandledError(result);
|
|---|
| 400 | }
|
|---|
| 401 | return result;
|
|---|
| 402 | } catch (e) {
|
|---|
| 403 | retry2++;
|
|---|
| 404 | if (e.throwImmediately) {
|
|---|
| 405 | if (e instanceof HandledError) {
|
|---|
| 406 | return e.value;
|
|---|
| 407 | }
|
|---|
| 408 | throw e;
|
|---|
| 409 | }
|
|---|
| 410 | if (e instanceof HandledError) {
|
|---|
| 411 | if (!options.retryCondition(e.value.error, args, {
|
|---|
| 412 | attempt: retry2,
|
|---|
| 413 | baseQueryApi: api,
|
|---|
| 414 | extraOptions
|
|---|
| 415 | })) {
|
|---|
| 416 | return e.value;
|
|---|
| 417 | }
|
|---|
| 418 | } else {
|
|---|
| 419 | if (retry2 > options.maxRetries) {
|
|---|
| 420 | return {
|
|---|
| 421 | error: e
|
|---|
| 422 | };
|
|---|
| 423 | }
|
|---|
| 424 | }
|
|---|
| 425 | failIfAborted(api.signal);
|
|---|
| 426 | try {
|
|---|
| 427 | await options.backoff(retry2, options.maxRetries, api.signal);
|
|---|
| 428 | } catch (backoffError) {
|
|---|
| 429 | failIfAborted(api.signal);
|
|---|
| 430 | throw backoffError;
|
|---|
| 431 | }
|
|---|
| 432 | }
|
|---|
| 433 | }
|
|---|
| 434 | };
|
|---|
| 435 | var retry = /* @__PURE__ */ Object.assign(retryWithBackoff, {
|
|---|
| 436 | fail
|
|---|
| 437 | });
|
|---|
| 438 |
|
|---|
| 439 | // src/query/core/setupListeners.ts
|
|---|
| 440 | var INTERNAL_PREFIX = "__rtkq/";
|
|---|
| 441 | var ONLINE = "online";
|
|---|
| 442 | var OFFLINE = "offline";
|
|---|
| 443 | var FOCUS = "focus";
|
|---|
| 444 | var FOCUSED = "focused";
|
|---|
| 445 | var VISIBILITYCHANGE = "visibilitychange";
|
|---|
| 446 | var onFocus = /* @__PURE__ */ (0, import_toolkit.createAction)(`${INTERNAL_PREFIX}${FOCUSED}`);
|
|---|
| 447 | var onFocusLost = /* @__PURE__ */ (0, import_toolkit.createAction)(`${INTERNAL_PREFIX}un${FOCUSED}`);
|
|---|
| 448 | var onOnline = /* @__PURE__ */ (0, import_toolkit.createAction)(`${INTERNAL_PREFIX}${ONLINE}`);
|
|---|
| 449 | var onOffline = /* @__PURE__ */ (0, import_toolkit.createAction)(`${INTERNAL_PREFIX}${OFFLINE}`);
|
|---|
| 450 | var actions = {
|
|---|
| 451 | onFocus,
|
|---|
| 452 | onFocusLost,
|
|---|
| 453 | onOnline,
|
|---|
| 454 | onOffline
|
|---|
| 455 | };
|
|---|
| 456 | var initialized = false;
|
|---|
| 457 | function setupListeners(dispatch, customHandler) {
|
|---|
| 458 | function defaultHandler() {
|
|---|
| 459 | const [handleFocus, handleFocusLost, handleOnline, handleOffline] = [onFocus, onFocusLost, onOnline, onOffline].map((action) => () => dispatch(action()));
|
|---|
| 460 | const handleVisibilityChange = () => {
|
|---|
| 461 | if (window.document.visibilityState === "visible") {
|
|---|
| 462 | handleFocus();
|
|---|
| 463 | } else {
|
|---|
| 464 | handleFocusLost();
|
|---|
| 465 | }
|
|---|
| 466 | };
|
|---|
| 467 | let unsubscribe = () => {
|
|---|
| 468 | initialized = false;
|
|---|
| 469 | };
|
|---|
| 470 | if (!initialized) {
|
|---|
| 471 | if (typeof window !== "undefined" && window.addEventListener) {
|
|---|
| 472 | let updateListeners2 = function(add) {
|
|---|
| 473 | Object.entries(handlers).forEach(([event, handler]) => {
|
|---|
| 474 | if (add) {
|
|---|
| 475 | window.addEventListener(event, handler, false);
|
|---|
| 476 | } else {
|
|---|
| 477 | window.removeEventListener(event, handler);
|
|---|
| 478 | }
|
|---|
| 479 | });
|
|---|
| 480 | };
|
|---|
| 481 | var updateListeners = updateListeners2;
|
|---|
| 482 | const handlers = {
|
|---|
| 483 | [FOCUS]: handleFocus,
|
|---|
| 484 | [VISIBILITYCHANGE]: handleVisibilityChange,
|
|---|
| 485 | [ONLINE]: handleOnline,
|
|---|
| 486 | [OFFLINE]: handleOffline
|
|---|
| 487 | };
|
|---|
| 488 | updateListeners2(true);
|
|---|
| 489 | initialized = true;
|
|---|
| 490 | unsubscribe = () => {
|
|---|
| 491 | updateListeners2(false);
|
|---|
| 492 | initialized = false;
|
|---|
| 493 | };
|
|---|
| 494 | }
|
|---|
| 495 | }
|
|---|
| 496 | return unsubscribe;
|
|---|
| 497 | }
|
|---|
| 498 | return customHandler ? customHandler(dispatch, actions) : defaultHandler();
|
|---|
| 499 | }
|
|---|
| 500 |
|
|---|
| 501 | // src/query/endpointDefinitions.ts
|
|---|
| 502 | var ENDPOINT_QUERY = "query" /* query */;
|
|---|
| 503 | var ENDPOINT_MUTATION = "mutation" /* mutation */;
|
|---|
| 504 | var ENDPOINT_INFINITEQUERY = "infinitequery" /* infinitequery */;
|
|---|
| 505 | function isQueryDefinition(e) {
|
|---|
| 506 | return e.type === ENDPOINT_QUERY;
|
|---|
| 507 | }
|
|---|
| 508 | function isMutationDefinition(e) {
|
|---|
| 509 | return e.type === ENDPOINT_MUTATION;
|
|---|
| 510 | }
|
|---|
| 511 | function isInfiniteQueryDefinition(e) {
|
|---|
| 512 | return e.type === ENDPOINT_INFINITEQUERY;
|
|---|
| 513 | }
|
|---|
| 514 | function isAnyQueryDefinition(e) {
|
|---|
| 515 | return isQueryDefinition(e) || isInfiniteQueryDefinition(e);
|
|---|
| 516 | }
|
|---|
| 517 | function calculateProvidedBy(description, result, error, queryArg, meta, assertTagTypes) {
|
|---|
| 518 | const finalDescription = isFunction(description) ? description(result, error, queryArg, meta) : description;
|
|---|
| 519 | if (finalDescription) {
|
|---|
| 520 | return filterMap(finalDescription, isNotNullish, (tag) => assertTagTypes(expandTagDescription(tag)));
|
|---|
| 521 | }
|
|---|
| 522 | return [];
|
|---|
| 523 | }
|
|---|
| 524 | function isFunction(t) {
|
|---|
| 525 | return typeof t === "function";
|
|---|
| 526 | }
|
|---|
| 527 | function expandTagDescription(description) {
|
|---|
| 528 | return typeof description === "string" ? {
|
|---|
| 529 | type: description
|
|---|
| 530 | } : description;
|
|---|
| 531 | }
|
|---|
| 532 |
|
|---|
| 533 | // src/query/utils/immerImports.ts
|
|---|
| 534 | var import_immer = require("immer");
|
|---|
| 535 |
|
|---|
| 536 | // src/query/core/buildInitiate.ts
|
|---|
| 537 | var import_toolkit2 = require("@reduxjs/toolkit");
|
|---|
| 538 |
|
|---|
| 539 | // src/tsHelpers.ts
|
|---|
| 540 | function asSafePromise(promise, fallback) {
|
|---|
| 541 | return promise.catch(fallback);
|
|---|
| 542 | }
|
|---|
| 543 |
|
|---|
| 544 | // src/query/apiTypes.ts
|
|---|
| 545 | var getEndpointDefinition = (context, endpointName) => context.endpointDefinitions[endpointName];
|
|---|
| 546 |
|
|---|
| 547 | // src/query/core/buildInitiate.ts
|
|---|
| 548 | var forceQueryFnSymbol = Symbol("forceQueryFn");
|
|---|
| 549 | var isUpsertQuery = (arg) => typeof arg[forceQueryFnSymbol] === "function";
|
|---|
| 550 | function buildInitiate({
|
|---|
| 551 | serializeQueryArgs,
|
|---|
| 552 | queryThunk,
|
|---|
| 553 | infiniteQueryThunk,
|
|---|
| 554 | mutationThunk,
|
|---|
| 555 | api,
|
|---|
| 556 | context,
|
|---|
| 557 | getInternalState
|
|---|
| 558 | }) {
|
|---|
| 559 | const getRunningQueries = (dispatch) => getInternalState(dispatch)?.runningQueries;
|
|---|
| 560 | const getRunningMutations = (dispatch) => getInternalState(dispatch)?.runningMutations;
|
|---|
| 561 | const {
|
|---|
| 562 | unsubscribeQueryResult,
|
|---|
| 563 | removeMutationResult,
|
|---|
| 564 | updateSubscriptionOptions
|
|---|
| 565 | } = api.internalActions;
|
|---|
| 566 | return {
|
|---|
| 567 | buildInitiateQuery,
|
|---|
| 568 | buildInitiateInfiniteQuery,
|
|---|
| 569 | buildInitiateMutation,
|
|---|
| 570 | getRunningQueryThunk,
|
|---|
| 571 | getRunningMutationThunk,
|
|---|
| 572 | getRunningQueriesThunk,
|
|---|
| 573 | getRunningMutationsThunk
|
|---|
| 574 | };
|
|---|
| 575 | function getRunningQueryThunk(endpointName, queryArgs) {
|
|---|
| 576 | return (dispatch) => {
|
|---|
| 577 | const endpointDefinition = getEndpointDefinition(context, endpointName);
|
|---|
| 578 | const queryCacheKey = serializeQueryArgs({
|
|---|
| 579 | queryArgs,
|
|---|
| 580 | endpointDefinition,
|
|---|
| 581 | endpointName
|
|---|
| 582 | });
|
|---|
| 583 | return getRunningQueries(dispatch)?.get(queryCacheKey);
|
|---|
| 584 | };
|
|---|
| 585 | }
|
|---|
| 586 | function getRunningMutationThunk(_endpointName, fixedCacheKeyOrRequestId) {
|
|---|
| 587 | return (dispatch) => {
|
|---|
| 588 | return getRunningMutations(dispatch)?.get(fixedCacheKeyOrRequestId);
|
|---|
| 589 | };
|
|---|
| 590 | }
|
|---|
| 591 | function getRunningQueriesThunk() {
|
|---|
| 592 | return (dispatch) => filterNullishValues(getRunningQueries(dispatch));
|
|---|
| 593 | }
|
|---|
| 594 | function getRunningMutationsThunk() {
|
|---|
| 595 | return (dispatch) => filterNullishValues(getRunningMutations(dispatch));
|
|---|
| 596 | }
|
|---|
| 597 | function middlewareWarning(dispatch) {
|
|---|
| 598 | if (true) {
|
|---|
| 599 | if (middlewareWarning.triggered) return;
|
|---|
| 600 | const returnedValue = dispatch(api.internalActions.internal_getRTKQSubscriptions());
|
|---|
| 601 | middlewareWarning.triggered = true;
|
|---|
| 602 | if (typeof returnedValue !== "object" || typeof returnedValue?.type === "string") {
|
|---|
| 603 | throw new Error(false ? _formatProdErrorMessage(34) : `Warning: Middleware for RTK-Query API at reducerPath "${api.reducerPath}" has not been added to the store.
|
|---|
| 604 | You must add the middleware for RTK-Query to function correctly!`);
|
|---|
| 605 | }
|
|---|
| 606 | }
|
|---|
| 607 | }
|
|---|
| 608 | function buildInitiateAnyQuery(endpointName, endpointDefinition) {
|
|---|
| 609 | const queryAction = (arg, {
|
|---|
| 610 | subscribe = true,
|
|---|
| 611 | forceRefetch,
|
|---|
| 612 | subscriptionOptions,
|
|---|
| 613 | [forceQueryFnSymbol]: forceQueryFn,
|
|---|
| 614 | ...rest
|
|---|
| 615 | } = {}) => (dispatch, getState) => {
|
|---|
| 616 | const queryCacheKey = serializeQueryArgs({
|
|---|
| 617 | queryArgs: arg,
|
|---|
| 618 | endpointDefinition,
|
|---|
| 619 | endpointName
|
|---|
| 620 | });
|
|---|
| 621 | let thunk;
|
|---|
| 622 | const commonThunkArgs = {
|
|---|
| 623 | ...rest,
|
|---|
| 624 | type: ENDPOINT_QUERY,
|
|---|
| 625 | subscribe,
|
|---|
| 626 | forceRefetch,
|
|---|
| 627 | subscriptionOptions,
|
|---|
| 628 | endpointName,
|
|---|
| 629 | originalArgs: arg,
|
|---|
| 630 | queryCacheKey,
|
|---|
| 631 | [forceQueryFnSymbol]: forceQueryFn
|
|---|
| 632 | };
|
|---|
| 633 | if (isQueryDefinition(endpointDefinition)) {
|
|---|
| 634 | thunk = queryThunk(commonThunkArgs);
|
|---|
| 635 | } else {
|
|---|
| 636 | const {
|
|---|
| 637 | direction,
|
|---|
| 638 | initialPageParam,
|
|---|
| 639 | refetchCachedPages
|
|---|
| 640 | } = rest;
|
|---|
| 641 | thunk = infiniteQueryThunk({
|
|---|
| 642 | ...commonThunkArgs,
|
|---|
| 643 | // Supply these even if undefined. This helps with a field existence
|
|---|
| 644 | // check over in `buildSlice.ts`
|
|---|
| 645 | direction,
|
|---|
| 646 | initialPageParam,
|
|---|
| 647 | refetchCachedPages
|
|---|
| 648 | });
|
|---|
| 649 | }
|
|---|
| 650 | const selector = api.endpoints[endpointName].select(arg);
|
|---|
| 651 | const thunkResult = dispatch(thunk);
|
|---|
| 652 | const stateAfter = selector(getState());
|
|---|
| 653 | middlewareWarning(dispatch);
|
|---|
| 654 | const {
|
|---|
| 655 | requestId,
|
|---|
| 656 | abort
|
|---|
| 657 | } = thunkResult;
|
|---|
| 658 | const skippedSynchronously = stateAfter.requestId !== requestId;
|
|---|
| 659 | const runningQuery = getRunningQueries(dispatch)?.get(queryCacheKey);
|
|---|
| 660 | const selectFromState = () => selector(getState());
|
|---|
| 661 | const statePromise = Object.assign(forceQueryFn ? (
|
|---|
| 662 | // a query has been forced (upsertQueryData)
|
|---|
| 663 | // -> we want to resolve it once data has been written with the data that will be written
|
|---|
| 664 | thunkResult.then(selectFromState)
|
|---|
| 665 | ) : skippedSynchronously && !runningQuery ? (
|
|---|
| 666 | // a query has been skipped due to a condition and we do not have any currently running query
|
|---|
| 667 | // -> we want to resolve it immediately with the current data
|
|---|
| 668 | Promise.resolve(stateAfter)
|
|---|
| 669 | ) : (
|
|---|
| 670 | // query just started or one is already in flight
|
|---|
| 671 | // -> wait for the running query, then resolve with data from after that
|
|---|
| 672 | Promise.all([runningQuery, thunkResult]).then(selectFromState)
|
|---|
| 673 | ), {
|
|---|
| 674 | arg,
|
|---|
| 675 | requestId,
|
|---|
| 676 | subscriptionOptions,
|
|---|
| 677 | queryCacheKey,
|
|---|
| 678 | abort,
|
|---|
| 679 | async unwrap() {
|
|---|
| 680 | const result = await statePromise;
|
|---|
| 681 | if (result.isError) {
|
|---|
| 682 | throw result.error;
|
|---|
| 683 | }
|
|---|
| 684 | return result.data;
|
|---|
| 685 | },
|
|---|
| 686 | refetch: (options) => dispatch(queryAction(arg, {
|
|---|
| 687 | subscribe: false,
|
|---|
| 688 | forceRefetch: true,
|
|---|
| 689 | ...options
|
|---|
| 690 | })),
|
|---|
| 691 | unsubscribe() {
|
|---|
| 692 | if (subscribe) dispatch(unsubscribeQueryResult({
|
|---|
| 693 | queryCacheKey,
|
|---|
| 694 | requestId
|
|---|
| 695 | }));
|
|---|
| 696 | },
|
|---|
| 697 | updateSubscriptionOptions(options) {
|
|---|
| 698 | statePromise.subscriptionOptions = options;
|
|---|
| 699 | dispatch(updateSubscriptionOptions({
|
|---|
| 700 | endpointName,
|
|---|
| 701 | requestId,
|
|---|
| 702 | queryCacheKey,
|
|---|
| 703 | options
|
|---|
| 704 | }));
|
|---|
| 705 | }
|
|---|
| 706 | });
|
|---|
| 707 | if (!runningQuery && !skippedSynchronously && !forceQueryFn) {
|
|---|
| 708 | const runningQueries = getRunningQueries(dispatch);
|
|---|
| 709 | runningQueries.set(queryCacheKey, statePromise);
|
|---|
| 710 | statePromise.then(() => {
|
|---|
| 711 | runningQueries.delete(queryCacheKey);
|
|---|
| 712 | });
|
|---|
| 713 | }
|
|---|
| 714 | return statePromise;
|
|---|
| 715 | };
|
|---|
| 716 | return queryAction;
|
|---|
| 717 | }
|
|---|
| 718 | function buildInitiateQuery(endpointName, endpointDefinition) {
|
|---|
| 719 | const queryAction = buildInitiateAnyQuery(endpointName, endpointDefinition);
|
|---|
| 720 | return queryAction;
|
|---|
| 721 | }
|
|---|
| 722 | function buildInitiateInfiniteQuery(endpointName, endpointDefinition) {
|
|---|
| 723 | const infiniteQueryAction = buildInitiateAnyQuery(endpointName, endpointDefinition);
|
|---|
| 724 | return infiniteQueryAction;
|
|---|
| 725 | }
|
|---|
| 726 | function buildInitiateMutation(endpointName) {
|
|---|
| 727 | return (arg, {
|
|---|
| 728 | track = true,
|
|---|
| 729 | fixedCacheKey
|
|---|
| 730 | } = {}) => (dispatch, getState) => {
|
|---|
| 731 | const thunk = mutationThunk({
|
|---|
| 732 | type: "mutation",
|
|---|
| 733 | endpointName,
|
|---|
| 734 | originalArgs: arg,
|
|---|
| 735 | track,
|
|---|
| 736 | fixedCacheKey
|
|---|
| 737 | });
|
|---|
| 738 | const thunkResult = dispatch(thunk);
|
|---|
| 739 | middlewareWarning(dispatch);
|
|---|
| 740 | const {
|
|---|
| 741 | requestId,
|
|---|
| 742 | abort,
|
|---|
| 743 | unwrap
|
|---|
| 744 | } = thunkResult;
|
|---|
| 745 | const returnValuePromise = asSafePromise(thunkResult.unwrap().then((data) => ({
|
|---|
| 746 | data
|
|---|
| 747 | })), (error) => ({
|
|---|
| 748 | error
|
|---|
| 749 | }));
|
|---|
| 750 | const reset = () => {
|
|---|
| 751 | dispatch(removeMutationResult({
|
|---|
| 752 | requestId,
|
|---|
| 753 | fixedCacheKey
|
|---|
| 754 | }));
|
|---|
| 755 | };
|
|---|
| 756 | const ret = Object.assign(returnValuePromise, {
|
|---|
| 757 | arg: thunkResult.arg,
|
|---|
| 758 | requestId,
|
|---|
| 759 | abort,
|
|---|
| 760 | unwrap,
|
|---|
| 761 | reset
|
|---|
| 762 | });
|
|---|
| 763 | const runningMutations = getRunningMutations(dispatch);
|
|---|
| 764 | runningMutations.set(requestId, ret);
|
|---|
| 765 | ret.then(() => {
|
|---|
| 766 | runningMutations.delete(requestId);
|
|---|
| 767 | });
|
|---|
| 768 | if (fixedCacheKey) {
|
|---|
| 769 | runningMutations.set(fixedCacheKey, ret);
|
|---|
| 770 | ret.then(() => {
|
|---|
| 771 | if (runningMutations.get(fixedCacheKey) === ret) {
|
|---|
| 772 | runningMutations.delete(fixedCacheKey);
|
|---|
| 773 | }
|
|---|
| 774 | });
|
|---|
| 775 | }
|
|---|
| 776 | return ret;
|
|---|
| 777 | };
|
|---|
| 778 | }
|
|---|
| 779 | }
|
|---|
| 780 |
|
|---|
| 781 | // src/query/standardSchema.ts
|
|---|
| 782 | var import_utils4 = require("@standard-schema/utils");
|
|---|
| 783 | var NamedSchemaError = class extends import_utils4.SchemaError {
|
|---|
| 784 | constructor(issues, value, schemaName, _bqMeta) {
|
|---|
| 785 | super(issues);
|
|---|
| 786 | this.value = value;
|
|---|
| 787 | this.schemaName = schemaName;
|
|---|
| 788 | this._bqMeta = _bqMeta;
|
|---|
| 789 | }
|
|---|
| 790 | };
|
|---|
| 791 | var shouldSkip = (skipSchemaValidation, schemaName) => Array.isArray(skipSchemaValidation) ? skipSchemaValidation.includes(schemaName) : !!skipSchemaValidation;
|
|---|
| 792 | async function parseWithSchema(schema, data, schemaName, bqMeta) {
|
|---|
| 793 | const result = await schema["~standard"].validate(data);
|
|---|
| 794 | if (result.issues) {
|
|---|
| 795 | throw new NamedSchemaError(result.issues, data, schemaName, bqMeta);
|
|---|
| 796 | }
|
|---|
| 797 | return result.value;
|
|---|
| 798 | }
|
|---|
| 799 |
|
|---|
| 800 | // src/query/core/buildThunks.ts
|
|---|
| 801 | function defaultTransformResponse(baseQueryReturnValue) {
|
|---|
| 802 | return baseQueryReturnValue;
|
|---|
| 803 | }
|
|---|
| 804 | var addShouldAutoBatch = (arg = {}) => {
|
|---|
| 805 | return {
|
|---|
| 806 | ...arg,
|
|---|
| 807 | [import_toolkit.SHOULD_AUTOBATCH]: true
|
|---|
| 808 | };
|
|---|
| 809 | };
|
|---|
| 810 | function buildThunks({
|
|---|
| 811 | reducerPath,
|
|---|
| 812 | baseQuery,
|
|---|
| 813 | context: {
|
|---|
| 814 | endpointDefinitions
|
|---|
| 815 | },
|
|---|
| 816 | serializeQueryArgs,
|
|---|
| 817 | api,
|
|---|
| 818 | assertTagType,
|
|---|
| 819 | selectors,
|
|---|
| 820 | onSchemaFailure,
|
|---|
| 821 | catchSchemaFailure: globalCatchSchemaFailure,
|
|---|
| 822 | skipSchemaValidation: globalSkipSchemaValidation
|
|---|
| 823 | }) {
|
|---|
| 824 | const patchQueryData = (endpointName, arg, patches, updateProvided) => (dispatch, getState) => {
|
|---|
| 825 | const endpointDefinition = endpointDefinitions[endpointName];
|
|---|
| 826 | const queryCacheKey = serializeQueryArgs({
|
|---|
| 827 | queryArgs: arg,
|
|---|
| 828 | endpointDefinition,
|
|---|
| 829 | endpointName
|
|---|
| 830 | });
|
|---|
| 831 | dispatch(api.internalActions.queryResultPatched({
|
|---|
| 832 | queryCacheKey,
|
|---|
| 833 | patches
|
|---|
| 834 | }));
|
|---|
| 835 | if (!updateProvided) {
|
|---|
| 836 | return;
|
|---|
| 837 | }
|
|---|
| 838 | const newValue = api.endpoints[endpointName].select(arg)(
|
|---|
| 839 | // Work around TS 4.1 mismatch
|
|---|
| 840 | getState()
|
|---|
| 841 | );
|
|---|
| 842 | const providedTags = calculateProvidedBy(endpointDefinition.providesTags, newValue.data, void 0, arg, {}, assertTagType);
|
|---|
| 843 | dispatch(api.internalActions.updateProvidedBy([{
|
|---|
| 844 | queryCacheKey,
|
|---|
| 845 | providedTags
|
|---|
| 846 | }]));
|
|---|
| 847 | };
|
|---|
| 848 | function addToStart(items, item, max = 0) {
|
|---|
| 849 | const newItems = [item, ...items];
|
|---|
| 850 | return max && newItems.length > max ? newItems.slice(0, -1) : newItems;
|
|---|
| 851 | }
|
|---|
| 852 | function addToEnd(items, item, max = 0) {
|
|---|
| 853 | const newItems = [...items, item];
|
|---|
| 854 | return max && newItems.length > max ? newItems.slice(1) : newItems;
|
|---|
| 855 | }
|
|---|
| 856 | const updateQueryData = (endpointName, arg, updateRecipe, updateProvided = true) => (dispatch, getState) => {
|
|---|
| 857 | const endpointDefinition = api.endpoints[endpointName];
|
|---|
| 858 | const currentState = endpointDefinition.select(arg)(
|
|---|
| 859 | // Work around TS 4.1 mismatch
|
|---|
| 860 | getState()
|
|---|
| 861 | );
|
|---|
| 862 | const ret = {
|
|---|
| 863 | patches: [],
|
|---|
| 864 | inversePatches: [],
|
|---|
| 865 | undo: () => dispatch(api.util.patchQueryData(endpointName, arg, ret.inversePatches, updateProvided))
|
|---|
| 866 | };
|
|---|
| 867 | if (currentState.status === STATUS_UNINITIALIZED) {
|
|---|
| 868 | return ret;
|
|---|
| 869 | }
|
|---|
| 870 | let newValue;
|
|---|
| 871 | if ("data" in currentState) {
|
|---|
| 872 | if ((0, import_immer.isDraftable)(currentState.data)) {
|
|---|
| 873 | const [value, patches, inversePatches] = (0, import_immer.produceWithPatches)(currentState.data, updateRecipe);
|
|---|
| 874 | ret.patches.push(...patches);
|
|---|
| 875 | ret.inversePatches.push(...inversePatches);
|
|---|
| 876 | newValue = value;
|
|---|
| 877 | } else {
|
|---|
| 878 | newValue = updateRecipe(currentState.data);
|
|---|
| 879 | ret.patches.push({
|
|---|
| 880 | op: "replace",
|
|---|
| 881 | path: [],
|
|---|
| 882 | value: newValue
|
|---|
| 883 | });
|
|---|
| 884 | ret.inversePatches.push({
|
|---|
| 885 | op: "replace",
|
|---|
| 886 | path: [],
|
|---|
| 887 | value: currentState.data
|
|---|
| 888 | });
|
|---|
| 889 | }
|
|---|
| 890 | }
|
|---|
| 891 | if (ret.patches.length === 0) {
|
|---|
| 892 | return ret;
|
|---|
| 893 | }
|
|---|
| 894 | dispatch(api.util.patchQueryData(endpointName, arg, ret.patches, updateProvided));
|
|---|
| 895 | return ret;
|
|---|
| 896 | };
|
|---|
| 897 | const upsertQueryData = (endpointName, arg, value) => (dispatch) => {
|
|---|
| 898 | const res = dispatch(api.endpoints[endpointName].initiate(arg, {
|
|---|
| 899 | subscribe: false,
|
|---|
| 900 | forceRefetch: true,
|
|---|
| 901 | [forceQueryFnSymbol]: () => ({
|
|---|
| 902 | data: value
|
|---|
| 903 | })
|
|---|
| 904 | }));
|
|---|
| 905 | return res;
|
|---|
| 906 | };
|
|---|
| 907 | const getTransformCallbackForEndpoint = (endpointDefinition, transformFieldName) => {
|
|---|
| 908 | return endpointDefinition.query && endpointDefinition[transformFieldName] ? endpointDefinition[transformFieldName] : defaultTransformResponse;
|
|---|
| 909 | };
|
|---|
| 910 | const executeEndpoint = async (arg, {
|
|---|
| 911 | signal,
|
|---|
| 912 | abort,
|
|---|
| 913 | rejectWithValue,
|
|---|
| 914 | fulfillWithValue,
|
|---|
| 915 | dispatch,
|
|---|
| 916 | getState,
|
|---|
| 917 | extra
|
|---|
| 918 | }) => {
|
|---|
| 919 | const endpointDefinition = endpointDefinitions[arg.endpointName];
|
|---|
| 920 | const {
|
|---|
| 921 | metaSchema,
|
|---|
| 922 | skipSchemaValidation = globalSkipSchemaValidation
|
|---|
| 923 | } = endpointDefinition;
|
|---|
| 924 | const isQuery = arg.type === ENDPOINT_QUERY;
|
|---|
| 925 | try {
|
|---|
| 926 | let transformResponse = defaultTransformResponse;
|
|---|
| 927 | const baseQueryApi = {
|
|---|
| 928 | signal,
|
|---|
| 929 | abort,
|
|---|
| 930 | dispatch,
|
|---|
| 931 | getState,
|
|---|
| 932 | extra,
|
|---|
| 933 | endpoint: arg.endpointName,
|
|---|
| 934 | type: arg.type,
|
|---|
| 935 | forced: isQuery ? isForcedQuery(arg, getState()) : void 0,
|
|---|
| 936 | queryCacheKey: isQuery ? arg.queryCacheKey : void 0
|
|---|
| 937 | };
|
|---|
| 938 | const forceQueryFn = isQuery ? arg[forceQueryFnSymbol] : void 0;
|
|---|
| 939 | let finalQueryReturnValue;
|
|---|
| 940 | const fetchPage = async (data, param, maxPages, previous) => {
|
|---|
| 941 | if (param == null && data.pages.length) {
|
|---|
| 942 | return Promise.resolve({
|
|---|
| 943 | data
|
|---|
| 944 | });
|
|---|
| 945 | }
|
|---|
| 946 | const finalQueryArg = {
|
|---|
| 947 | queryArg: arg.originalArgs,
|
|---|
| 948 | pageParam: param
|
|---|
| 949 | };
|
|---|
| 950 | const pageResponse = await executeRequest(finalQueryArg);
|
|---|
| 951 | const addTo = previous ? addToStart : addToEnd;
|
|---|
| 952 | return {
|
|---|
| 953 | data: {
|
|---|
| 954 | pages: addTo(data.pages, pageResponse.data, maxPages),
|
|---|
| 955 | pageParams: addTo(data.pageParams, param, maxPages)
|
|---|
| 956 | },
|
|---|
| 957 | meta: pageResponse.meta
|
|---|
| 958 | };
|
|---|
| 959 | };
|
|---|
| 960 | async function executeRequest(finalQueryArg) {
|
|---|
| 961 | let result;
|
|---|
| 962 | const {
|
|---|
| 963 | extraOptions,
|
|---|
| 964 | argSchema,
|
|---|
| 965 | rawResponseSchema,
|
|---|
| 966 | responseSchema
|
|---|
| 967 | } = endpointDefinition;
|
|---|
| 968 | if (argSchema && !shouldSkip(skipSchemaValidation, "arg")) {
|
|---|
| 969 | finalQueryArg = await parseWithSchema(
|
|---|
| 970 | argSchema,
|
|---|
| 971 | finalQueryArg,
|
|---|
| 972 | "argSchema",
|
|---|
| 973 | {}
|
|---|
| 974 | // we don't have a meta yet, so we can't pass it
|
|---|
| 975 | );
|
|---|
| 976 | }
|
|---|
| 977 | if (forceQueryFn) {
|
|---|
| 978 | result = forceQueryFn();
|
|---|
| 979 | } else if (endpointDefinition.query) {
|
|---|
| 980 | transformResponse = getTransformCallbackForEndpoint(endpointDefinition, "transformResponse");
|
|---|
| 981 | result = await baseQuery(endpointDefinition.query(finalQueryArg), baseQueryApi, extraOptions);
|
|---|
| 982 | } else {
|
|---|
| 983 | result = await endpointDefinition.queryFn(finalQueryArg, baseQueryApi, extraOptions, (arg2) => baseQuery(arg2, baseQueryApi, extraOptions));
|
|---|
| 984 | }
|
|---|
| 985 | if (typeof process !== "undefined" && true) {
|
|---|
| 986 | const what = endpointDefinition.query ? "`baseQuery`" : "`queryFn`";
|
|---|
| 987 | let err;
|
|---|
| 988 | if (!result) {
|
|---|
| 989 | err = `${what} did not return anything.`;
|
|---|
| 990 | } else if (typeof result !== "object") {
|
|---|
| 991 | err = `${what} did not return an object.`;
|
|---|
| 992 | } else if (result.error && result.data) {
|
|---|
| 993 | err = `${what} returned an object containing both \`error\` and \`result\`.`;
|
|---|
| 994 | } else if (result.error === void 0 && result.data === void 0) {
|
|---|
| 995 | err = `${what} returned an object containing neither a valid \`error\` and \`result\`. At least one of them should not be \`undefined\``;
|
|---|
| 996 | } else {
|
|---|
| 997 | for (const key of Object.keys(result)) {
|
|---|
| 998 | if (key !== "error" && key !== "data" && key !== "meta") {
|
|---|
| 999 | err = `The object returned by ${what} has the unknown property ${key}.`;
|
|---|
| 1000 | break;
|
|---|
| 1001 | }
|
|---|
| 1002 | }
|
|---|
| 1003 | }
|
|---|
| 1004 | if (err) {
|
|---|
| 1005 | console.error(`Error encountered handling the endpoint ${arg.endpointName}.
|
|---|
| 1006 | ${err}
|
|---|
| 1007 | It needs to return an object with either the shape \`{ data: <value> }\` or \`{ error: <value> }\` that may contain an optional \`meta\` property.
|
|---|
| 1008 | Object returned was:`, result);
|
|---|
| 1009 | }
|
|---|
| 1010 | }
|
|---|
| 1011 | if (result.error) throw new HandledError(result.error, result.meta);
|
|---|
| 1012 | let {
|
|---|
| 1013 | data
|
|---|
| 1014 | } = result;
|
|---|
| 1015 | if (rawResponseSchema && !shouldSkip(skipSchemaValidation, "rawResponse")) {
|
|---|
| 1016 | data = await parseWithSchema(rawResponseSchema, result.data, "rawResponseSchema", result.meta);
|
|---|
| 1017 | }
|
|---|
| 1018 | let transformedResponse = await transformResponse(data, result.meta, finalQueryArg);
|
|---|
| 1019 | if (responseSchema && !shouldSkip(skipSchemaValidation, "response")) {
|
|---|
| 1020 | transformedResponse = await parseWithSchema(responseSchema, transformedResponse, "responseSchema", result.meta);
|
|---|
| 1021 | }
|
|---|
| 1022 | return {
|
|---|
| 1023 | ...result,
|
|---|
| 1024 | data: transformedResponse
|
|---|
| 1025 | };
|
|---|
| 1026 | }
|
|---|
| 1027 | if (isQuery && "infiniteQueryOptions" in endpointDefinition) {
|
|---|
| 1028 | const {
|
|---|
| 1029 | infiniteQueryOptions
|
|---|
| 1030 | } = endpointDefinition;
|
|---|
| 1031 | const {
|
|---|
| 1032 | maxPages = Infinity
|
|---|
| 1033 | } = infiniteQueryOptions;
|
|---|
| 1034 | const refetchCachedPages = arg.refetchCachedPages ?? infiniteQueryOptions.refetchCachedPages ?? true;
|
|---|
| 1035 | let result;
|
|---|
| 1036 | const blankData = {
|
|---|
| 1037 | pages: [],
|
|---|
| 1038 | pageParams: []
|
|---|
| 1039 | };
|
|---|
| 1040 | const cachedData = selectors.selectQueryEntry(getState(), arg.queryCacheKey)?.data;
|
|---|
| 1041 | const isForcedQueryNeedingRefetch = (
|
|---|
| 1042 | // arg.forceRefetch
|
|---|
| 1043 | isForcedQuery(arg, getState()) && !arg.direction
|
|---|
| 1044 | );
|
|---|
| 1045 | const existingData = isForcedQueryNeedingRefetch || !cachedData ? blankData : cachedData;
|
|---|
| 1046 | if ("direction" in arg && arg.direction && existingData.pages.length) {
|
|---|
| 1047 | const previous = arg.direction === "backward";
|
|---|
| 1048 | const pageParamFn = previous ? getPreviousPageParam : getNextPageParam;
|
|---|
| 1049 | const param = pageParamFn(infiniteQueryOptions, existingData, arg.originalArgs);
|
|---|
| 1050 | result = await fetchPage(existingData, param, maxPages, previous);
|
|---|
| 1051 | } else {
|
|---|
| 1052 | const {
|
|---|
| 1053 | initialPageParam = infiniteQueryOptions.initialPageParam
|
|---|
| 1054 | } = arg;
|
|---|
| 1055 | const cachedPageParams = cachedData?.pageParams ?? [];
|
|---|
| 1056 | const firstPageParam = cachedPageParams[0] ?? initialPageParam;
|
|---|
| 1057 | const totalPages = cachedPageParams.length;
|
|---|
| 1058 | result = await fetchPage(existingData, firstPageParam, maxPages);
|
|---|
| 1059 | if (forceQueryFn) {
|
|---|
| 1060 | result = {
|
|---|
| 1061 | data: result.data.pages[0]
|
|---|
| 1062 | };
|
|---|
| 1063 | }
|
|---|
| 1064 | if (refetchCachedPages) {
|
|---|
| 1065 | for (let i = 1; i < totalPages; i++) {
|
|---|
| 1066 | const param = getNextPageParam(infiniteQueryOptions, result.data, arg.originalArgs);
|
|---|
| 1067 | result = await fetchPage(result.data, param, maxPages);
|
|---|
| 1068 | }
|
|---|
| 1069 | }
|
|---|
| 1070 | }
|
|---|
| 1071 | finalQueryReturnValue = result;
|
|---|
| 1072 | } else {
|
|---|
| 1073 | finalQueryReturnValue = await executeRequest(arg.originalArgs);
|
|---|
| 1074 | }
|
|---|
| 1075 | if (metaSchema && !shouldSkip(skipSchemaValidation, "meta") && finalQueryReturnValue.meta) {
|
|---|
| 1076 | finalQueryReturnValue.meta = await parseWithSchema(metaSchema, finalQueryReturnValue.meta, "metaSchema", finalQueryReturnValue.meta);
|
|---|
| 1077 | }
|
|---|
| 1078 | return fulfillWithValue(finalQueryReturnValue.data, addShouldAutoBatch({
|
|---|
| 1079 | fulfilledTimeStamp: Date.now(),
|
|---|
| 1080 | baseQueryMeta: finalQueryReturnValue.meta
|
|---|
| 1081 | }));
|
|---|
| 1082 | } catch (error) {
|
|---|
| 1083 | let caughtError = error;
|
|---|
| 1084 | if (caughtError instanceof HandledError) {
|
|---|
| 1085 | let transformErrorResponse = getTransformCallbackForEndpoint(endpointDefinition, "transformErrorResponse");
|
|---|
| 1086 | const {
|
|---|
| 1087 | rawErrorResponseSchema,
|
|---|
| 1088 | errorResponseSchema
|
|---|
| 1089 | } = endpointDefinition;
|
|---|
| 1090 | let {
|
|---|
| 1091 | value,
|
|---|
| 1092 | meta
|
|---|
| 1093 | } = caughtError;
|
|---|
| 1094 | try {
|
|---|
| 1095 | if (rawErrorResponseSchema && !shouldSkip(skipSchemaValidation, "rawErrorResponse")) {
|
|---|
| 1096 | value = await parseWithSchema(rawErrorResponseSchema, value, "rawErrorResponseSchema", meta);
|
|---|
| 1097 | }
|
|---|
| 1098 | if (metaSchema && !shouldSkip(skipSchemaValidation, "meta")) {
|
|---|
| 1099 | meta = await parseWithSchema(metaSchema, meta, "metaSchema", meta);
|
|---|
| 1100 | }
|
|---|
| 1101 | let transformedErrorResponse = await transformErrorResponse(value, meta, arg.originalArgs);
|
|---|
| 1102 | if (errorResponseSchema && !shouldSkip(skipSchemaValidation, "errorResponse")) {
|
|---|
| 1103 | transformedErrorResponse = await parseWithSchema(errorResponseSchema, transformedErrorResponse, "errorResponseSchema", meta);
|
|---|
| 1104 | }
|
|---|
| 1105 | return rejectWithValue(transformedErrorResponse, addShouldAutoBatch({
|
|---|
| 1106 | baseQueryMeta: meta
|
|---|
| 1107 | }));
|
|---|
| 1108 | } catch (e) {
|
|---|
| 1109 | caughtError = e;
|
|---|
| 1110 | }
|
|---|
| 1111 | }
|
|---|
| 1112 | try {
|
|---|
| 1113 | if (caughtError instanceof NamedSchemaError) {
|
|---|
| 1114 | const info = {
|
|---|
| 1115 | endpoint: arg.endpointName,
|
|---|
| 1116 | arg: arg.originalArgs,
|
|---|
| 1117 | type: arg.type,
|
|---|
| 1118 | queryCacheKey: isQuery ? arg.queryCacheKey : void 0
|
|---|
| 1119 | };
|
|---|
| 1120 | endpointDefinition.onSchemaFailure?.(caughtError, info);
|
|---|
| 1121 | onSchemaFailure?.(caughtError, info);
|
|---|
| 1122 | const {
|
|---|
| 1123 | catchSchemaFailure = globalCatchSchemaFailure
|
|---|
| 1124 | } = endpointDefinition;
|
|---|
| 1125 | if (catchSchemaFailure) {
|
|---|
| 1126 | return rejectWithValue(catchSchemaFailure(caughtError, info), addShouldAutoBatch({
|
|---|
| 1127 | baseQueryMeta: caughtError._bqMeta
|
|---|
| 1128 | }));
|
|---|
| 1129 | }
|
|---|
| 1130 | }
|
|---|
| 1131 | } catch (e) {
|
|---|
| 1132 | caughtError = e;
|
|---|
| 1133 | }
|
|---|
| 1134 | if (typeof process !== "undefined" && true) {
|
|---|
| 1135 | console.error(`An unhandled error occurred processing a request for the endpoint "${arg.endpointName}".
|
|---|
| 1136 | In the case of an unhandled error, no tags will be "provided" or "invalidated".`, caughtError);
|
|---|
| 1137 | } else {
|
|---|
| 1138 | console.error(caughtError);
|
|---|
| 1139 | }
|
|---|
| 1140 | throw caughtError;
|
|---|
| 1141 | }
|
|---|
| 1142 | };
|
|---|
| 1143 | function isForcedQuery(arg, state) {
|
|---|
| 1144 | const requestState = selectors.selectQueryEntry(state, arg.queryCacheKey);
|
|---|
| 1145 | const baseFetchOnMountOrArgChange = selectors.selectConfig(state).refetchOnMountOrArgChange;
|
|---|
| 1146 | const fulfilledVal = requestState?.fulfilledTimeStamp;
|
|---|
| 1147 | const refetchVal = arg.forceRefetch ?? (arg.subscribe && baseFetchOnMountOrArgChange);
|
|---|
| 1148 | if (refetchVal) {
|
|---|
| 1149 | return refetchVal === true || (Number(/* @__PURE__ */ new Date()) - Number(fulfilledVal)) / 1e3 >= refetchVal;
|
|---|
| 1150 | }
|
|---|
| 1151 | return false;
|
|---|
| 1152 | }
|
|---|
| 1153 | const createQueryThunk = () => {
|
|---|
| 1154 | const generatedQueryThunk = (0, import_toolkit.createAsyncThunk)(`${reducerPath}/executeQuery`, executeEndpoint, {
|
|---|
| 1155 | getPendingMeta({
|
|---|
| 1156 | arg
|
|---|
| 1157 | }) {
|
|---|
| 1158 | const endpointDefinition = endpointDefinitions[arg.endpointName];
|
|---|
| 1159 | return addShouldAutoBatch({
|
|---|
| 1160 | startedTimeStamp: Date.now(),
|
|---|
| 1161 | ...isInfiniteQueryDefinition(endpointDefinition) ? {
|
|---|
| 1162 | direction: arg.direction
|
|---|
| 1163 | } : {}
|
|---|
| 1164 | });
|
|---|
| 1165 | },
|
|---|
| 1166 | condition(queryThunkArg, {
|
|---|
| 1167 | getState
|
|---|
| 1168 | }) {
|
|---|
| 1169 | const state = getState();
|
|---|
| 1170 | const requestState = selectors.selectQueryEntry(state, queryThunkArg.queryCacheKey);
|
|---|
| 1171 | const fulfilledVal = requestState?.fulfilledTimeStamp;
|
|---|
| 1172 | const currentArg = queryThunkArg.originalArgs;
|
|---|
| 1173 | const previousArg = requestState?.originalArgs;
|
|---|
| 1174 | const endpointDefinition = endpointDefinitions[queryThunkArg.endpointName];
|
|---|
| 1175 | const direction = queryThunkArg.direction;
|
|---|
| 1176 | if (isUpsertQuery(queryThunkArg)) {
|
|---|
| 1177 | return true;
|
|---|
| 1178 | }
|
|---|
| 1179 | if (requestState?.status === "pending") {
|
|---|
| 1180 | return false;
|
|---|
| 1181 | }
|
|---|
| 1182 | if (isForcedQuery(queryThunkArg, state)) {
|
|---|
| 1183 | return true;
|
|---|
| 1184 | }
|
|---|
| 1185 | if (isQueryDefinition(endpointDefinition) && endpointDefinition?.forceRefetch?.({
|
|---|
| 1186 | currentArg,
|
|---|
| 1187 | previousArg,
|
|---|
| 1188 | endpointState: requestState,
|
|---|
| 1189 | state
|
|---|
| 1190 | })) {
|
|---|
| 1191 | return true;
|
|---|
| 1192 | }
|
|---|
| 1193 | if (fulfilledVal && !direction) {
|
|---|
| 1194 | return false;
|
|---|
| 1195 | }
|
|---|
| 1196 | return true;
|
|---|
| 1197 | },
|
|---|
| 1198 | dispatchConditionRejection: true
|
|---|
| 1199 | });
|
|---|
| 1200 | return generatedQueryThunk;
|
|---|
| 1201 | };
|
|---|
| 1202 | const queryThunk = createQueryThunk();
|
|---|
| 1203 | const infiniteQueryThunk = createQueryThunk();
|
|---|
| 1204 | const mutationThunk = (0, import_toolkit.createAsyncThunk)(`${reducerPath}/executeMutation`, executeEndpoint, {
|
|---|
| 1205 | getPendingMeta() {
|
|---|
| 1206 | return addShouldAutoBatch({
|
|---|
| 1207 | startedTimeStamp: Date.now()
|
|---|
| 1208 | });
|
|---|
| 1209 | }
|
|---|
| 1210 | });
|
|---|
| 1211 | const hasTheForce = (options) => "force" in options;
|
|---|
| 1212 | const hasMaxAge = (options) => "ifOlderThan" in options;
|
|---|
| 1213 | const prefetch = (endpointName, arg, options = {}) => (dispatch, getState) => {
|
|---|
| 1214 | const force = hasTheForce(options) && options.force;
|
|---|
| 1215 | const maxAge = hasMaxAge(options) && options.ifOlderThan;
|
|---|
| 1216 | const queryAction = (force2 = true) => {
|
|---|
| 1217 | const options2 = {
|
|---|
| 1218 | forceRefetch: force2,
|
|---|
| 1219 | subscribe: false
|
|---|
| 1220 | };
|
|---|
| 1221 | return api.endpoints[endpointName].initiate(arg, options2);
|
|---|
| 1222 | };
|
|---|
| 1223 | const latestStateValue = api.endpoints[endpointName].select(arg)(getState());
|
|---|
| 1224 | if (force) {
|
|---|
| 1225 | dispatch(queryAction());
|
|---|
| 1226 | } else if (maxAge) {
|
|---|
| 1227 | const lastFulfilledTs = latestStateValue?.fulfilledTimeStamp;
|
|---|
| 1228 | if (!lastFulfilledTs) {
|
|---|
| 1229 | dispatch(queryAction());
|
|---|
| 1230 | return;
|
|---|
| 1231 | }
|
|---|
| 1232 | const shouldRetrigger = (Number(/* @__PURE__ */ new Date()) - Number(new Date(lastFulfilledTs))) / 1e3 >= maxAge;
|
|---|
| 1233 | if (shouldRetrigger) {
|
|---|
| 1234 | dispatch(queryAction());
|
|---|
| 1235 | }
|
|---|
| 1236 | } else {
|
|---|
| 1237 | dispatch(queryAction(false));
|
|---|
| 1238 | }
|
|---|
| 1239 | };
|
|---|
| 1240 | function matchesEndpoint(endpointName) {
|
|---|
| 1241 | return (action) => action?.meta?.arg?.endpointName === endpointName;
|
|---|
| 1242 | }
|
|---|
| 1243 | function buildMatchThunkActions(thunk, endpointName) {
|
|---|
| 1244 | return {
|
|---|
| 1245 | matchPending: (0, import_toolkit.isAllOf)((0, import_toolkit.isPending)(thunk), matchesEndpoint(endpointName)),
|
|---|
| 1246 | matchFulfilled: (0, import_toolkit.isAllOf)((0, import_toolkit.isFulfilled)(thunk), matchesEndpoint(endpointName)),
|
|---|
| 1247 | matchRejected: (0, import_toolkit.isAllOf)((0, import_toolkit.isRejected)(thunk), matchesEndpoint(endpointName))
|
|---|
| 1248 | };
|
|---|
| 1249 | }
|
|---|
| 1250 | return {
|
|---|
| 1251 | queryThunk,
|
|---|
| 1252 | mutationThunk,
|
|---|
| 1253 | infiniteQueryThunk,
|
|---|
| 1254 | prefetch,
|
|---|
| 1255 | updateQueryData,
|
|---|
| 1256 | upsertQueryData,
|
|---|
| 1257 | patchQueryData,
|
|---|
| 1258 | buildMatchThunkActions
|
|---|
| 1259 | };
|
|---|
| 1260 | }
|
|---|
| 1261 | function getNextPageParam(options, {
|
|---|
| 1262 | pages,
|
|---|
| 1263 | pageParams
|
|---|
| 1264 | }, queryArg) {
|
|---|
| 1265 | const lastIndex = pages.length - 1;
|
|---|
| 1266 | return options.getNextPageParam(pages[lastIndex], pages, pageParams[lastIndex], pageParams, queryArg);
|
|---|
| 1267 | }
|
|---|
| 1268 | function getPreviousPageParam(options, {
|
|---|
| 1269 | pages,
|
|---|
| 1270 | pageParams
|
|---|
| 1271 | }, queryArg) {
|
|---|
| 1272 | return options.getPreviousPageParam?.(pages[0], pages, pageParams[0], pageParams, queryArg);
|
|---|
| 1273 | }
|
|---|
| 1274 | function calculateProvidedByThunk(action, type, endpointDefinitions, assertTagType) {
|
|---|
| 1275 | return calculateProvidedBy(endpointDefinitions[action.meta.arg.endpointName][type], (0, import_toolkit.isFulfilled)(action) ? action.payload : void 0, (0, import_toolkit.isRejectedWithValue)(action) ? action.payload : void 0, action.meta.arg.originalArgs, "baseQueryMeta" in action.meta ? action.meta.baseQueryMeta : void 0, assertTagType);
|
|---|
| 1276 | }
|
|---|
| 1277 |
|
|---|
| 1278 | // src/query/utils/getCurrent.ts
|
|---|
| 1279 | function getCurrent(value) {
|
|---|
| 1280 | return (0, import_immer.isDraft)(value) ? (0, import_immer.current)(value) : value;
|
|---|
| 1281 | }
|
|---|
| 1282 |
|
|---|
| 1283 | // src/query/core/buildSlice.ts
|
|---|
| 1284 | function updateQuerySubstateIfExists(state, queryCacheKey, update) {
|
|---|
| 1285 | const substate = state[queryCacheKey];
|
|---|
| 1286 | if (substate) {
|
|---|
| 1287 | update(substate);
|
|---|
| 1288 | }
|
|---|
| 1289 | }
|
|---|
| 1290 | function getMutationCacheKey(id) {
|
|---|
| 1291 | return ("arg" in id ? id.arg.fixedCacheKey : id.fixedCacheKey) ?? id.requestId;
|
|---|
| 1292 | }
|
|---|
| 1293 | function updateMutationSubstateIfExists(state, id, update) {
|
|---|
| 1294 | const substate = state[getMutationCacheKey(id)];
|
|---|
| 1295 | if (substate) {
|
|---|
| 1296 | update(substate);
|
|---|
| 1297 | }
|
|---|
| 1298 | }
|
|---|
| 1299 | var initialState = {};
|
|---|
| 1300 | function buildSlice({
|
|---|
| 1301 | reducerPath,
|
|---|
| 1302 | queryThunk,
|
|---|
| 1303 | mutationThunk,
|
|---|
| 1304 | serializeQueryArgs,
|
|---|
| 1305 | context: {
|
|---|
| 1306 | endpointDefinitions: definitions,
|
|---|
| 1307 | apiUid,
|
|---|
| 1308 | extractRehydrationInfo,
|
|---|
| 1309 | hasRehydrationInfo
|
|---|
| 1310 | },
|
|---|
| 1311 | assertTagType,
|
|---|
| 1312 | config
|
|---|
| 1313 | }) {
|
|---|
| 1314 | const resetApiState = (0, import_toolkit.createAction)(`${reducerPath}/resetApiState`);
|
|---|
| 1315 | function writePendingCacheEntry(draft, arg, upserting, meta) {
|
|---|
| 1316 | draft[arg.queryCacheKey] ??= {
|
|---|
| 1317 | status: STATUS_UNINITIALIZED,
|
|---|
| 1318 | endpointName: arg.endpointName
|
|---|
| 1319 | };
|
|---|
| 1320 | updateQuerySubstateIfExists(draft, arg.queryCacheKey, (substate) => {
|
|---|
| 1321 | substate.status = STATUS_PENDING;
|
|---|
| 1322 | substate.requestId = upserting && substate.requestId ? (
|
|---|
| 1323 | // for `upsertQuery` **updates**, keep the current `requestId`
|
|---|
| 1324 | substate.requestId
|
|---|
| 1325 | ) : (
|
|---|
| 1326 | // for normal queries or `upsertQuery` **inserts** always update the `requestId`
|
|---|
| 1327 | meta.requestId
|
|---|
| 1328 | );
|
|---|
| 1329 | if (arg.originalArgs !== void 0) {
|
|---|
| 1330 | substate.originalArgs = arg.originalArgs;
|
|---|
| 1331 | }
|
|---|
| 1332 | substate.startedTimeStamp = meta.startedTimeStamp;
|
|---|
| 1333 | const endpointDefinition = definitions[meta.arg.endpointName];
|
|---|
| 1334 | if (isInfiniteQueryDefinition(endpointDefinition) && "direction" in arg) {
|
|---|
| 1335 | ;
|
|---|
| 1336 | substate.direction = arg.direction;
|
|---|
| 1337 | }
|
|---|
| 1338 | });
|
|---|
| 1339 | }
|
|---|
| 1340 | function writeFulfilledCacheEntry(draft, meta, payload, upserting) {
|
|---|
| 1341 | updateQuerySubstateIfExists(draft, meta.arg.queryCacheKey, (substate) => {
|
|---|
| 1342 | if (substate.requestId !== meta.requestId && !upserting) return;
|
|---|
| 1343 | const {
|
|---|
| 1344 | merge
|
|---|
| 1345 | } = definitions[meta.arg.endpointName];
|
|---|
| 1346 | substate.status = STATUS_FULFILLED;
|
|---|
| 1347 | if (merge) {
|
|---|
| 1348 | if (substate.data !== void 0) {
|
|---|
| 1349 | const {
|
|---|
| 1350 | fulfilledTimeStamp,
|
|---|
| 1351 | arg,
|
|---|
| 1352 | baseQueryMeta,
|
|---|
| 1353 | requestId
|
|---|
| 1354 | } = meta;
|
|---|
| 1355 | let newData = (0, import_toolkit.createNextState)(substate.data, (draftSubstateData) => {
|
|---|
| 1356 | return merge(draftSubstateData, payload, {
|
|---|
| 1357 | arg: arg.originalArgs,
|
|---|
| 1358 | baseQueryMeta,
|
|---|
| 1359 | fulfilledTimeStamp,
|
|---|
| 1360 | requestId
|
|---|
| 1361 | });
|
|---|
| 1362 | });
|
|---|
| 1363 | substate.data = newData;
|
|---|
| 1364 | } else {
|
|---|
| 1365 | substate.data = payload;
|
|---|
| 1366 | }
|
|---|
| 1367 | } else {
|
|---|
| 1368 | substate.data = definitions[meta.arg.endpointName].structuralSharing ?? true ? copyWithStructuralSharing((0, import_immer.isDraft)(substate.data) ? (0, import_immer.original)(substate.data) : substate.data, payload) : payload;
|
|---|
| 1369 | }
|
|---|
| 1370 | delete substate.error;
|
|---|
| 1371 | substate.fulfilledTimeStamp = meta.fulfilledTimeStamp;
|
|---|
| 1372 | });
|
|---|
| 1373 | }
|
|---|
| 1374 | const querySlice = (0, import_toolkit.createSlice)({
|
|---|
| 1375 | name: `${reducerPath}/queries`,
|
|---|
| 1376 | initialState,
|
|---|
| 1377 | reducers: {
|
|---|
| 1378 | removeQueryResult: {
|
|---|
| 1379 | reducer(draft, {
|
|---|
| 1380 | payload: {
|
|---|
| 1381 | queryCacheKey
|
|---|
| 1382 | }
|
|---|
| 1383 | }) {
|
|---|
| 1384 | delete draft[queryCacheKey];
|
|---|
| 1385 | },
|
|---|
| 1386 | prepare: (0, import_toolkit.prepareAutoBatched)()
|
|---|
| 1387 | },
|
|---|
| 1388 | cacheEntriesUpserted: {
|
|---|
| 1389 | reducer(draft, action) {
|
|---|
| 1390 | for (const entry of action.payload) {
|
|---|
| 1391 | const {
|
|---|
| 1392 | queryDescription: arg,
|
|---|
| 1393 | value
|
|---|
| 1394 | } = entry;
|
|---|
| 1395 | writePendingCacheEntry(draft, arg, true, {
|
|---|
| 1396 | arg,
|
|---|
| 1397 | requestId: action.meta.requestId,
|
|---|
| 1398 | startedTimeStamp: action.meta.timestamp
|
|---|
| 1399 | });
|
|---|
| 1400 | writeFulfilledCacheEntry(
|
|---|
| 1401 | draft,
|
|---|
| 1402 | {
|
|---|
| 1403 | arg,
|
|---|
| 1404 | requestId: action.meta.requestId,
|
|---|
| 1405 | fulfilledTimeStamp: action.meta.timestamp,
|
|---|
| 1406 | baseQueryMeta: {}
|
|---|
| 1407 | },
|
|---|
| 1408 | value,
|
|---|
| 1409 | // We know we're upserting here
|
|---|
| 1410 | true
|
|---|
| 1411 | );
|
|---|
| 1412 | }
|
|---|
| 1413 | },
|
|---|
| 1414 | prepare: (payload) => {
|
|---|
| 1415 | const queryDescriptions = payload.map((entry) => {
|
|---|
| 1416 | const {
|
|---|
| 1417 | endpointName,
|
|---|
| 1418 | arg,
|
|---|
| 1419 | value
|
|---|
| 1420 | } = entry;
|
|---|
| 1421 | const endpointDefinition = definitions[endpointName];
|
|---|
| 1422 | const queryDescription = {
|
|---|
| 1423 | type: ENDPOINT_QUERY,
|
|---|
| 1424 | endpointName,
|
|---|
| 1425 | originalArgs: entry.arg,
|
|---|
| 1426 | queryCacheKey: serializeQueryArgs({
|
|---|
| 1427 | queryArgs: arg,
|
|---|
| 1428 | endpointDefinition,
|
|---|
| 1429 | endpointName
|
|---|
| 1430 | })
|
|---|
| 1431 | };
|
|---|
| 1432 | return {
|
|---|
| 1433 | queryDescription,
|
|---|
| 1434 | value
|
|---|
| 1435 | };
|
|---|
| 1436 | });
|
|---|
| 1437 | const result = {
|
|---|
| 1438 | payload: queryDescriptions,
|
|---|
| 1439 | meta: {
|
|---|
| 1440 | [import_toolkit.SHOULD_AUTOBATCH]: true,
|
|---|
| 1441 | requestId: (0, import_toolkit.nanoid)(),
|
|---|
| 1442 | timestamp: Date.now()
|
|---|
| 1443 | }
|
|---|
| 1444 | };
|
|---|
| 1445 | return result;
|
|---|
| 1446 | }
|
|---|
| 1447 | },
|
|---|
| 1448 | queryResultPatched: {
|
|---|
| 1449 | reducer(draft, {
|
|---|
| 1450 | payload: {
|
|---|
| 1451 | queryCacheKey,
|
|---|
| 1452 | patches
|
|---|
| 1453 | }
|
|---|
| 1454 | }) {
|
|---|
| 1455 | updateQuerySubstateIfExists(draft, queryCacheKey, (substate) => {
|
|---|
| 1456 | substate.data = (0, import_immer.applyPatches)(substate.data, patches.concat());
|
|---|
| 1457 | });
|
|---|
| 1458 | },
|
|---|
| 1459 | prepare: (0, import_toolkit.prepareAutoBatched)()
|
|---|
| 1460 | }
|
|---|
| 1461 | },
|
|---|
| 1462 | extraReducers(builder) {
|
|---|
| 1463 | builder.addCase(queryThunk.pending, (draft, {
|
|---|
| 1464 | meta,
|
|---|
| 1465 | meta: {
|
|---|
| 1466 | arg
|
|---|
| 1467 | }
|
|---|
| 1468 | }) => {
|
|---|
| 1469 | const upserting = isUpsertQuery(arg);
|
|---|
| 1470 | writePendingCacheEntry(draft, arg, upserting, meta);
|
|---|
| 1471 | }).addCase(queryThunk.fulfilled, (draft, {
|
|---|
| 1472 | meta,
|
|---|
| 1473 | payload
|
|---|
| 1474 | }) => {
|
|---|
| 1475 | const upserting = isUpsertQuery(meta.arg);
|
|---|
| 1476 | writeFulfilledCacheEntry(draft, meta, payload, upserting);
|
|---|
| 1477 | }).addCase(queryThunk.rejected, (draft, {
|
|---|
| 1478 | meta: {
|
|---|
| 1479 | condition,
|
|---|
| 1480 | arg,
|
|---|
| 1481 | requestId
|
|---|
| 1482 | },
|
|---|
| 1483 | error,
|
|---|
| 1484 | payload
|
|---|
| 1485 | }) => {
|
|---|
| 1486 | updateQuerySubstateIfExists(draft, arg.queryCacheKey, (substate) => {
|
|---|
| 1487 | if (condition) {
|
|---|
| 1488 | } else {
|
|---|
| 1489 | if (substate.requestId !== requestId) return;
|
|---|
| 1490 | substate.status = STATUS_REJECTED;
|
|---|
| 1491 | substate.error = payload ?? error;
|
|---|
| 1492 | }
|
|---|
| 1493 | });
|
|---|
| 1494 | }).addMatcher(hasRehydrationInfo, (draft, action) => {
|
|---|
| 1495 | const {
|
|---|
| 1496 | queries
|
|---|
| 1497 | } = extractRehydrationInfo(action);
|
|---|
| 1498 | for (const [key, entry] of Object.entries(queries)) {
|
|---|
| 1499 | if (
|
|---|
| 1500 | // do not rehydrate entries that were currently in flight.
|
|---|
| 1501 | entry?.status === STATUS_FULFILLED || entry?.status === STATUS_REJECTED
|
|---|
| 1502 | ) {
|
|---|
| 1503 | draft[key] = entry;
|
|---|
| 1504 | }
|
|---|
| 1505 | }
|
|---|
| 1506 | });
|
|---|
| 1507 | }
|
|---|
| 1508 | });
|
|---|
| 1509 | const mutationSlice = (0, import_toolkit.createSlice)({
|
|---|
| 1510 | name: `${reducerPath}/mutations`,
|
|---|
| 1511 | initialState,
|
|---|
| 1512 | reducers: {
|
|---|
| 1513 | removeMutationResult: {
|
|---|
| 1514 | reducer(draft, {
|
|---|
| 1515 | payload
|
|---|
| 1516 | }) {
|
|---|
| 1517 | const cacheKey = getMutationCacheKey(payload);
|
|---|
| 1518 | if (cacheKey in draft) {
|
|---|
| 1519 | delete draft[cacheKey];
|
|---|
| 1520 | }
|
|---|
| 1521 | },
|
|---|
| 1522 | prepare: (0, import_toolkit.prepareAutoBatched)()
|
|---|
| 1523 | }
|
|---|
| 1524 | },
|
|---|
| 1525 | extraReducers(builder) {
|
|---|
| 1526 | builder.addCase(mutationThunk.pending, (draft, {
|
|---|
| 1527 | meta,
|
|---|
| 1528 | meta: {
|
|---|
| 1529 | requestId,
|
|---|
| 1530 | arg,
|
|---|
| 1531 | startedTimeStamp
|
|---|
| 1532 | }
|
|---|
| 1533 | }) => {
|
|---|
| 1534 | if (!arg.track) return;
|
|---|
| 1535 | draft[getMutationCacheKey(meta)] = {
|
|---|
| 1536 | requestId,
|
|---|
| 1537 | status: STATUS_PENDING,
|
|---|
| 1538 | endpointName: arg.endpointName,
|
|---|
| 1539 | startedTimeStamp
|
|---|
| 1540 | };
|
|---|
| 1541 | }).addCase(mutationThunk.fulfilled, (draft, {
|
|---|
| 1542 | payload,
|
|---|
| 1543 | meta
|
|---|
| 1544 | }) => {
|
|---|
| 1545 | if (!meta.arg.track) return;
|
|---|
| 1546 | updateMutationSubstateIfExists(draft, meta, (substate) => {
|
|---|
| 1547 | if (substate.requestId !== meta.requestId) return;
|
|---|
| 1548 | substate.status = STATUS_FULFILLED;
|
|---|
| 1549 | substate.data = payload;
|
|---|
| 1550 | substate.fulfilledTimeStamp = meta.fulfilledTimeStamp;
|
|---|
| 1551 | });
|
|---|
| 1552 | }).addCase(mutationThunk.rejected, (draft, {
|
|---|
| 1553 | payload,
|
|---|
| 1554 | error,
|
|---|
| 1555 | meta
|
|---|
| 1556 | }) => {
|
|---|
| 1557 | if (!meta.arg.track) return;
|
|---|
| 1558 | updateMutationSubstateIfExists(draft, meta, (substate) => {
|
|---|
| 1559 | if (substate.requestId !== meta.requestId) return;
|
|---|
| 1560 | substate.status = STATUS_REJECTED;
|
|---|
| 1561 | substate.error = payload ?? error;
|
|---|
| 1562 | });
|
|---|
| 1563 | }).addMatcher(hasRehydrationInfo, (draft, action) => {
|
|---|
| 1564 | const {
|
|---|
| 1565 | mutations
|
|---|
| 1566 | } = extractRehydrationInfo(action);
|
|---|
| 1567 | for (const [key, entry] of Object.entries(mutations)) {
|
|---|
| 1568 | if (
|
|---|
| 1569 | // do not rehydrate entries that were currently in flight.
|
|---|
| 1570 | (entry?.status === STATUS_FULFILLED || entry?.status === STATUS_REJECTED) && // only rehydrate endpoints that were persisted using a `fixedCacheKey`
|
|---|
| 1571 | key !== entry?.requestId
|
|---|
| 1572 | ) {
|
|---|
| 1573 | draft[key] = entry;
|
|---|
| 1574 | }
|
|---|
| 1575 | }
|
|---|
| 1576 | });
|
|---|
| 1577 | }
|
|---|
| 1578 | });
|
|---|
| 1579 | const initialInvalidationState = {
|
|---|
| 1580 | tags: {},
|
|---|
| 1581 | keys: {}
|
|---|
| 1582 | };
|
|---|
| 1583 | const invalidationSlice = (0, import_toolkit.createSlice)({
|
|---|
| 1584 | name: `${reducerPath}/invalidation`,
|
|---|
| 1585 | initialState: initialInvalidationState,
|
|---|
| 1586 | reducers: {
|
|---|
| 1587 | updateProvidedBy: {
|
|---|
| 1588 | reducer(draft, action) {
|
|---|
| 1589 | for (const {
|
|---|
| 1590 | queryCacheKey,
|
|---|
| 1591 | providedTags
|
|---|
| 1592 | } of action.payload) {
|
|---|
| 1593 | removeCacheKeyFromTags(draft, queryCacheKey);
|
|---|
| 1594 | for (const {
|
|---|
| 1595 | type,
|
|---|
| 1596 | id
|
|---|
| 1597 | } of providedTags) {
|
|---|
| 1598 | const subscribedQueries = (draft.tags[type] ??= {})[id || "__internal_without_id"] ??= [];
|
|---|
| 1599 | const alreadySubscribed = subscribedQueries.includes(queryCacheKey);
|
|---|
| 1600 | if (!alreadySubscribed) {
|
|---|
| 1601 | subscribedQueries.push(queryCacheKey);
|
|---|
| 1602 | }
|
|---|
| 1603 | }
|
|---|
| 1604 | draft.keys[queryCacheKey] = providedTags;
|
|---|
| 1605 | }
|
|---|
| 1606 | },
|
|---|
| 1607 | prepare: (0, import_toolkit.prepareAutoBatched)()
|
|---|
| 1608 | }
|
|---|
| 1609 | },
|
|---|
| 1610 | extraReducers(builder) {
|
|---|
| 1611 | builder.addCase(querySlice.actions.removeQueryResult, (draft, {
|
|---|
| 1612 | payload: {
|
|---|
| 1613 | queryCacheKey
|
|---|
| 1614 | }
|
|---|
| 1615 | }) => {
|
|---|
| 1616 | removeCacheKeyFromTags(draft, queryCacheKey);
|
|---|
| 1617 | }).addMatcher(hasRehydrationInfo, (draft, action) => {
|
|---|
| 1618 | const {
|
|---|
| 1619 | provided
|
|---|
| 1620 | } = extractRehydrationInfo(action);
|
|---|
| 1621 | for (const [type, incomingTags] of Object.entries(provided.tags ?? {})) {
|
|---|
| 1622 | for (const [id, cacheKeys] of Object.entries(incomingTags)) {
|
|---|
| 1623 | const subscribedQueries = (draft.tags[type] ??= {})[id || "__internal_without_id"] ??= [];
|
|---|
| 1624 | for (const queryCacheKey of cacheKeys) {
|
|---|
| 1625 | const alreadySubscribed = subscribedQueries.includes(queryCacheKey);
|
|---|
| 1626 | if (!alreadySubscribed) {
|
|---|
| 1627 | subscribedQueries.push(queryCacheKey);
|
|---|
| 1628 | }
|
|---|
| 1629 | draft.keys[queryCacheKey] = provided.keys[queryCacheKey];
|
|---|
| 1630 | }
|
|---|
| 1631 | }
|
|---|
| 1632 | }
|
|---|
| 1633 | }).addMatcher((0, import_toolkit.isAnyOf)((0, import_toolkit.isFulfilled)(queryThunk), (0, import_toolkit.isRejectedWithValue)(queryThunk)), (draft, action) => {
|
|---|
| 1634 | writeProvidedTagsForQueries(draft, [action]);
|
|---|
| 1635 | }).addMatcher(querySlice.actions.cacheEntriesUpserted.match, (draft, action) => {
|
|---|
| 1636 | const mockActions = action.payload.map(({
|
|---|
| 1637 | queryDescription,
|
|---|
| 1638 | value
|
|---|
| 1639 | }) => {
|
|---|
| 1640 | return {
|
|---|
| 1641 | type: "UNKNOWN",
|
|---|
| 1642 | payload: value,
|
|---|
| 1643 | meta: {
|
|---|
| 1644 | requestStatus: "fulfilled",
|
|---|
| 1645 | requestId: "UNKNOWN",
|
|---|
| 1646 | arg: queryDescription
|
|---|
| 1647 | }
|
|---|
| 1648 | };
|
|---|
| 1649 | });
|
|---|
| 1650 | writeProvidedTagsForQueries(draft, mockActions);
|
|---|
| 1651 | });
|
|---|
| 1652 | }
|
|---|
| 1653 | });
|
|---|
| 1654 | function removeCacheKeyFromTags(draft, queryCacheKey) {
|
|---|
| 1655 | const existingTags = getCurrent(draft.keys[queryCacheKey] ?? []);
|
|---|
| 1656 | for (const tag of existingTags) {
|
|---|
| 1657 | const tagType = tag.type;
|
|---|
| 1658 | const tagId = tag.id ?? "__internal_without_id";
|
|---|
| 1659 | const tagSubscriptions = draft.tags[tagType]?.[tagId];
|
|---|
| 1660 | if (tagSubscriptions) {
|
|---|
| 1661 | draft.tags[tagType][tagId] = getCurrent(tagSubscriptions).filter((qc) => qc !== queryCacheKey);
|
|---|
| 1662 | }
|
|---|
| 1663 | }
|
|---|
| 1664 | delete draft.keys[queryCacheKey];
|
|---|
| 1665 | }
|
|---|
| 1666 | function writeProvidedTagsForQueries(draft, actions3) {
|
|---|
| 1667 | const providedByEntries = actions3.map((action) => {
|
|---|
| 1668 | const providedTags = calculateProvidedByThunk(action, "providesTags", definitions, assertTagType);
|
|---|
| 1669 | const {
|
|---|
| 1670 | queryCacheKey
|
|---|
| 1671 | } = action.meta.arg;
|
|---|
| 1672 | return {
|
|---|
| 1673 | queryCacheKey,
|
|---|
| 1674 | providedTags
|
|---|
| 1675 | };
|
|---|
| 1676 | });
|
|---|
| 1677 | invalidationSlice.caseReducers.updateProvidedBy(draft, invalidationSlice.actions.updateProvidedBy(providedByEntries));
|
|---|
| 1678 | }
|
|---|
| 1679 | const subscriptionSlice = (0, import_toolkit.createSlice)({
|
|---|
| 1680 | name: `${reducerPath}/subscriptions`,
|
|---|
| 1681 | initialState,
|
|---|
| 1682 | reducers: {
|
|---|
| 1683 | updateSubscriptionOptions(d, a) {
|
|---|
| 1684 | },
|
|---|
| 1685 | unsubscribeQueryResult(d, a) {
|
|---|
| 1686 | },
|
|---|
| 1687 | internal_getRTKQSubscriptions() {
|
|---|
| 1688 | }
|
|---|
| 1689 | }
|
|---|
| 1690 | });
|
|---|
| 1691 | const internalSubscriptionsSlice = (0, import_toolkit.createSlice)({
|
|---|
| 1692 | name: `${reducerPath}/internalSubscriptions`,
|
|---|
| 1693 | initialState,
|
|---|
| 1694 | reducers: {
|
|---|
| 1695 | subscriptionsUpdated: {
|
|---|
| 1696 | reducer(state, action) {
|
|---|
| 1697 | return (0, import_immer.applyPatches)(state, action.payload);
|
|---|
| 1698 | },
|
|---|
| 1699 | prepare: (0, import_toolkit.prepareAutoBatched)()
|
|---|
| 1700 | }
|
|---|
| 1701 | }
|
|---|
| 1702 | });
|
|---|
| 1703 | const configSlice = (0, import_toolkit.createSlice)({
|
|---|
| 1704 | name: `${reducerPath}/config`,
|
|---|
| 1705 | initialState: {
|
|---|
| 1706 | online: isOnline(),
|
|---|
| 1707 | focused: isDocumentVisible(),
|
|---|
| 1708 | middlewareRegistered: false,
|
|---|
| 1709 | ...config
|
|---|
| 1710 | },
|
|---|
| 1711 | reducers: {
|
|---|
| 1712 | middlewareRegistered(state, {
|
|---|
| 1713 | payload
|
|---|
| 1714 | }) {
|
|---|
| 1715 | state.middlewareRegistered = state.middlewareRegistered === "conflict" || apiUid !== payload ? "conflict" : true;
|
|---|
| 1716 | }
|
|---|
| 1717 | },
|
|---|
| 1718 | extraReducers: (builder) => {
|
|---|
| 1719 | builder.addCase(onOnline, (state) => {
|
|---|
| 1720 | state.online = true;
|
|---|
| 1721 | }).addCase(onOffline, (state) => {
|
|---|
| 1722 | state.online = false;
|
|---|
| 1723 | }).addCase(onFocus, (state) => {
|
|---|
| 1724 | state.focused = true;
|
|---|
| 1725 | }).addCase(onFocusLost, (state) => {
|
|---|
| 1726 | state.focused = false;
|
|---|
| 1727 | }).addMatcher(hasRehydrationInfo, (draft) => ({
|
|---|
| 1728 | ...draft
|
|---|
| 1729 | }));
|
|---|
| 1730 | }
|
|---|
| 1731 | });
|
|---|
| 1732 | const combinedReducer = (0, import_toolkit.combineReducers)({
|
|---|
| 1733 | queries: querySlice.reducer,
|
|---|
| 1734 | mutations: mutationSlice.reducer,
|
|---|
| 1735 | provided: invalidationSlice.reducer,
|
|---|
| 1736 | subscriptions: internalSubscriptionsSlice.reducer,
|
|---|
| 1737 | config: configSlice.reducer
|
|---|
| 1738 | });
|
|---|
| 1739 | const reducer = (state, action) => combinedReducer(resetApiState.match(action) ? void 0 : state, action);
|
|---|
| 1740 | const actions2 = {
|
|---|
| 1741 | ...configSlice.actions,
|
|---|
| 1742 | ...querySlice.actions,
|
|---|
| 1743 | ...subscriptionSlice.actions,
|
|---|
| 1744 | ...internalSubscriptionsSlice.actions,
|
|---|
| 1745 | ...mutationSlice.actions,
|
|---|
| 1746 | ...invalidationSlice.actions,
|
|---|
| 1747 | resetApiState
|
|---|
| 1748 | };
|
|---|
| 1749 | return {
|
|---|
| 1750 | reducer,
|
|---|
| 1751 | actions: actions2
|
|---|
| 1752 | };
|
|---|
| 1753 | }
|
|---|
| 1754 |
|
|---|
| 1755 | // src/query/core/buildSelectors.ts
|
|---|
| 1756 | var skipToken = /* @__PURE__ */ Symbol.for("RTKQ/skipToken");
|
|---|
| 1757 | var initialSubState = {
|
|---|
| 1758 | status: STATUS_UNINITIALIZED
|
|---|
| 1759 | };
|
|---|
| 1760 | var defaultQuerySubState = /* @__PURE__ */ (0, import_toolkit.createNextState)(initialSubState, () => {
|
|---|
| 1761 | });
|
|---|
| 1762 | var defaultMutationSubState = /* @__PURE__ */ (0, import_toolkit.createNextState)(initialSubState, () => {
|
|---|
| 1763 | });
|
|---|
| 1764 | function buildSelectors({
|
|---|
| 1765 | serializeQueryArgs,
|
|---|
| 1766 | reducerPath,
|
|---|
| 1767 | createSelector: createSelector2
|
|---|
| 1768 | }) {
|
|---|
| 1769 | const selectSkippedQuery = (state) => defaultQuerySubState;
|
|---|
| 1770 | const selectSkippedMutation = (state) => defaultMutationSubState;
|
|---|
| 1771 | return {
|
|---|
| 1772 | buildQuerySelector,
|
|---|
| 1773 | buildInfiniteQuerySelector,
|
|---|
| 1774 | buildMutationSelector,
|
|---|
| 1775 | selectInvalidatedBy,
|
|---|
| 1776 | selectCachedArgsForQuery,
|
|---|
| 1777 | selectApiState,
|
|---|
| 1778 | selectQueries,
|
|---|
| 1779 | selectMutations,
|
|---|
| 1780 | selectQueryEntry,
|
|---|
| 1781 | selectConfig
|
|---|
| 1782 | };
|
|---|
| 1783 | function withRequestFlags(substate) {
|
|---|
| 1784 | return {
|
|---|
| 1785 | ...substate,
|
|---|
| 1786 | ...getRequestStatusFlags(substate.status)
|
|---|
| 1787 | };
|
|---|
| 1788 | }
|
|---|
| 1789 | function selectApiState(rootState) {
|
|---|
| 1790 | const state = rootState[reducerPath];
|
|---|
| 1791 | if (true) {
|
|---|
| 1792 | if (!state) {
|
|---|
| 1793 | if (selectApiState.triggered) return state;
|
|---|
| 1794 | selectApiState.triggered = true;
|
|---|
| 1795 | console.error(`Error: No data found at \`state.${reducerPath}\`. Did you forget to add the reducer to the store?`);
|
|---|
| 1796 | }
|
|---|
| 1797 | }
|
|---|
| 1798 | return state;
|
|---|
| 1799 | }
|
|---|
| 1800 | function selectQueries(rootState) {
|
|---|
| 1801 | return selectApiState(rootState)?.queries;
|
|---|
| 1802 | }
|
|---|
| 1803 | function selectQueryEntry(rootState, cacheKey) {
|
|---|
| 1804 | return selectQueries(rootState)?.[cacheKey];
|
|---|
| 1805 | }
|
|---|
| 1806 | function selectMutations(rootState) {
|
|---|
| 1807 | return selectApiState(rootState)?.mutations;
|
|---|
| 1808 | }
|
|---|
| 1809 | function selectConfig(rootState) {
|
|---|
| 1810 | return selectApiState(rootState)?.config;
|
|---|
| 1811 | }
|
|---|
| 1812 | function buildAnyQuerySelector(endpointName, endpointDefinition, combiner) {
|
|---|
| 1813 | return (queryArgs) => {
|
|---|
| 1814 | if (queryArgs === skipToken) {
|
|---|
| 1815 | return createSelector2(selectSkippedQuery, combiner);
|
|---|
| 1816 | }
|
|---|
| 1817 | const serializedArgs = serializeQueryArgs({
|
|---|
| 1818 | queryArgs,
|
|---|
| 1819 | endpointDefinition,
|
|---|
| 1820 | endpointName
|
|---|
| 1821 | });
|
|---|
| 1822 | const selectQuerySubstate = (state) => selectQueryEntry(state, serializedArgs) ?? defaultQuerySubState;
|
|---|
| 1823 | return createSelector2(selectQuerySubstate, combiner);
|
|---|
| 1824 | };
|
|---|
| 1825 | }
|
|---|
| 1826 | function buildQuerySelector(endpointName, endpointDefinition) {
|
|---|
| 1827 | return buildAnyQuerySelector(endpointName, endpointDefinition, withRequestFlags);
|
|---|
| 1828 | }
|
|---|
| 1829 | function buildInfiniteQuerySelector(endpointName, endpointDefinition) {
|
|---|
| 1830 | const {
|
|---|
| 1831 | infiniteQueryOptions
|
|---|
| 1832 | } = endpointDefinition;
|
|---|
| 1833 | function withInfiniteQueryResultFlags(substate) {
|
|---|
| 1834 | const stateWithRequestFlags = {
|
|---|
| 1835 | ...substate,
|
|---|
| 1836 | ...getRequestStatusFlags(substate.status)
|
|---|
| 1837 | };
|
|---|
| 1838 | const {
|
|---|
| 1839 | isLoading,
|
|---|
| 1840 | isError,
|
|---|
| 1841 | direction
|
|---|
| 1842 | } = stateWithRequestFlags;
|
|---|
| 1843 | const isForward = direction === "forward";
|
|---|
| 1844 | const isBackward = direction === "backward";
|
|---|
| 1845 | return {
|
|---|
| 1846 | ...stateWithRequestFlags,
|
|---|
| 1847 | hasNextPage: getHasNextPage(infiniteQueryOptions, stateWithRequestFlags.data, stateWithRequestFlags.originalArgs),
|
|---|
| 1848 | hasPreviousPage: getHasPreviousPage(infiniteQueryOptions, stateWithRequestFlags.data, stateWithRequestFlags.originalArgs),
|
|---|
| 1849 | isFetchingNextPage: isLoading && isForward,
|
|---|
| 1850 | isFetchingPreviousPage: isLoading && isBackward,
|
|---|
| 1851 | isFetchNextPageError: isError && isForward,
|
|---|
| 1852 | isFetchPreviousPageError: isError && isBackward
|
|---|
| 1853 | };
|
|---|
| 1854 | }
|
|---|
| 1855 | return buildAnyQuerySelector(endpointName, endpointDefinition, withInfiniteQueryResultFlags);
|
|---|
| 1856 | }
|
|---|
| 1857 | function buildMutationSelector() {
|
|---|
| 1858 | return (id) => {
|
|---|
| 1859 | let mutationId;
|
|---|
| 1860 | if (typeof id === "object") {
|
|---|
| 1861 | mutationId = getMutationCacheKey(id) ?? skipToken;
|
|---|
| 1862 | } else {
|
|---|
| 1863 | mutationId = id;
|
|---|
| 1864 | }
|
|---|
| 1865 | const selectMutationSubstate = (state) => selectApiState(state)?.mutations?.[mutationId] ?? defaultMutationSubState;
|
|---|
| 1866 | const finalSelectMutationSubstate = mutationId === skipToken ? selectSkippedMutation : selectMutationSubstate;
|
|---|
| 1867 | return createSelector2(finalSelectMutationSubstate, withRequestFlags);
|
|---|
| 1868 | };
|
|---|
| 1869 | }
|
|---|
| 1870 | function selectInvalidatedBy(state, tags) {
|
|---|
| 1871 | const apiState = state[reducerPath];
|
|---|
| 1872 | const toInvalidate = /* @__PURE__ */ new Set();
|
|---|
| 1873 | const finalTags = filterMap(tags, isNotNullish, expandTagDescription);
|
|---|
| 1874 | for (const tag of finalTags) {
|
|---|
| 1875 | const provided = apiState.provided.tags[tag.type];
|
|---|
| 1876 | if (!provided) {
|
|---|
| 1877 | continue;
|
|---|
| 1878 | }
|
|---|
| 1879 | let invalidateSubscriptions = (tag.id !== void 0 ? (
|
|---|
| 1880 | // id given: invalidate all queries that provide this type & id
|
|---|
| 1881 | provided[tag.id]
|
|---|
| 1882 | ) : (
|
|---|
| 1883 | // no id: invalidate all queries that provide this type
|
|---|
| 1884 | Object.values(provided).flat()
|
|---|
| 1885 | )) ?? [];
|
|---|
| 1886 | for (const invalidate of invalidateSubscriptions) {
|
|---|
| 1887 | toInvalidate.add(invalidate);
|
|---|
| 1888 | }
|
|---|
| 1889 | }
|
|---|
| 1890 | return Array.from(toInvalidate.values()).flatMap((queryCacheKey) => {
|
|---|
| 1891 | const querySubState = apiState.queries[queryCacheKey];
|
|---|
| 1892 | return querySubState ? {
|
|---|
| 1893 | queryCacheKey,
|
|---|
| 1894 | endpointName: querySubState.endpointName,
|
|---|
| 1895 | originalArgs: querySubState.originalArgs
|
|---|
| 1896 | } : [];
|
|---|
| 1897 | });
|
|---|
| 1898 | }
|
|---|
| 1899 | function selectCachedArgsForQuery(state, queryName) {
|
|---|
| 1900 | return filterMap(Object.values(selectQueries(state)), (entry) => entry?.endpointName === queryName && entry.status !== STATUS_UNINITIALIZED, (entry) => entry.originalArgs);
|
|---|
| 1901 | }
|
|---|
| 1902 | function getHasNextPage(options, data, queryArg) {
|
|---|
| 1903 | if (!data) return false;
|
|---|
| 1904 | return getNextPageParam(options, data, queryArg) != null;
|
|---|
| 1905 | }
|
|---|
| 1906 | function getHasPreviousPage(options, data, queryArg) {
|
|---|
| 1907 | if (!data || !options.getPreviousPageParam) return false;
|
|---|
| 1908 | return getPreviousPageParam(options, data, queryArg) != null;
|
|---|
| 1909 | }
|
|---|
| 1910 | }
|
|---|
| 1911 |
|
|---|
| 1912 | // src/query/createApi.ts
|
|---|
| 1913 | var import_toolkit3 = require("@reduxjs/toolkit");
|
|---|
| 1914 |
|
|---|
| 1915 | // src/query/defaultSerializeQueryArgs.ts
|
|---|
| 1916 | var cache = WeakMap ? /* @__PURE__ */ new WeakMap() : void 0;
|
|---|
| 1917 | var defaultSerializeQueryArgs = ({
|
|---|
| 1918 | endpointName,
|
|---|
| 1919 | queryArgs
|
|---|
| 1920 | }) => {
|
|---|
| 1921 | let serialized = "";
|
|---|
| 1922 | const cached = cache?.get(queryArgs);
|
|---|
| 1923 | if (typeof cached === "string") {
|
|---|
| 1924 | serialized = cached;
|
|---|
| 1925 | } else {
|
|---|
| 1926 | const stringified = JSON.stringify(queryArgs, (key, value) => {
|
|---|
| 1927 | value = typeof value === "bigint" ? {
|
|---|
| 1928 | $bigint: value.toString()
|
|---|
| 1929 | } : value;
|
|---|
| 1930 | value = (0, import_toolkit.isPlainObject)(value) ? Object.keys(value).sort().reduce((acc, key2) => {
|
|---|
| 1931 | acc[key2] = value[key2];
|
|---|
| 1932 | return acc;
|
|---|
| 1933 | }, {}) : value;
|
|---|
| 1934 | return value;
|
|---|
| 1935 | });
|
|---|
| 1936 | if ((0, import_toolkit.isPlainObject)(queryArgs)) {
|
|---|
| 1937 | cache?.set(queryArgs, stringified);
|
|---|
| 1938 | }
|
|---|
| 1939 | serialized = stringified;
|
|---|
| 1940 | }
|
|---|
| 1941 | return `${endpointName}(${serialized})`;
|
|---|
| 1942 | };
|
|---|
| 1943 |
|
|---|
| 1944 | // src/query/createApi.ts
|
|---|
| 1945 | var import_reselect = require("reselect");
|
|---|
| 1946 | function buildCreateApi(...modules) {
|
|---|
| 1947 | return function baseCreateApi(options) {
|
|---|
| 1948 | const extractRehydrationInfo = (0, import_reselect.weakMapMemoize)((action) => options.extractRehydrationInfo?.(action, {
|
|---|
| 1949 | reducerPath: options.reducerPath ?? "api"
|
|---|
| 1950 | }));
|
|---|
| 1951 | const optionsWithDefaults = {
|
|---|
| 1952 | reducerPath: "api",
|
|---|
| 1953 | keepUnusedDataFor: 60,
|
|---|
| 1954 | refetchOnMountOrArgChange: false,
|
|---|
| 1955 | refetchOnFocus: false,
|
|---|
| 1956 | refetchOnReconnect: false,
|
|---|
| 1957 | invalidationBehavior: "delayed",
|
|---|
| 1958 | ...options,
|
|---|
| 1959 | extractRehydrationInfo,
|
|---|
| 1960 | serializeQueryArgs(queryArgsApi) {
|
|---|
| 1961 | let finalSerializeQueryArgs = defaultSerializeQueryArgs;
|
|---|
| 1962 | if ("serializeQueryArgs" in queryArgsApi.endpointDefinition) {
|
|---|
| 1963 | const endpointSQA = queryArgsApi.endpointDefinition.serializeQueryArgs;
|
|---|
| 1964 | finalSerializeQueryArgs = (queryArgsApi2) => {
|
|---|
| 1965 | const initialResult = endpointSQA(queryArgsApi2);
|
|---|
| 1966 | if (typeof initialResult === "string") {
|
|---|
| 1967 | return initialResult;
|
|---|
| 1968 | } else {
|
|---|
| 1969 | return defaultSerializeQueryArgs({
|
|---|
| 1970 | ...queryArgsApi2,
|
|---|
| 1971 | queryArgs: initialResult
|
|---|
| 1972 | });
|
|---|
| 1973 | }
|
|---|
| 1974 | };
|
|---|
| 1975 | } else if (options.serializeQueryArgs) {
|
|---|
| 1976 | finalSerializeQueryArgs = options.serializeQueryArgs;
|
|---|
| 1977 | }
|
|---|
| 1978 | return finalSerializeQueryArgs(queryArgsApi);
|
|---|
| 1979 | },
|
|---|
| 1980 | tagTypes: [...options.tagTypes || []]
|
|---|
| 1981 | };
|
|---|
| 1982 | const context = {
|
|---|
| 1983 | endpointDefinitions: {},
|
|---|
| 1984 | batch(fn) {
|
|---|
| 1985 | fn();
|
|---|
| 1986 | },
|
|---|
| 1987 | apiUid: (0, import_toolkit.nanoid)(),
|
|---|
| 1988 | extractRehydrationInfo,
|
|---|
| 1989 | hasRehydrationInfo: (0, import_reselect.weakMapMemoize)((action) => extractRehydrationInfo(action) != null)
|
|---|
| 1990 | };
|
|---|
| 1991 | const api = {
|
|---|
| 1992 | injectEndpoints,
|
|---|
| 1993 | enhanceEndpoints({
|
|---|
| 1994 | addTagTypes,
|
|---|
| 1995 | endpoints
|
|---|
| 1996 | }) {
|
|---|
| 1997 | if (addTagTypes) {
|
|---|
| 1998 | for (const eT of addTagTypes) {
|
|---|
| 1999 | if (!optionsWithDefaults.tagTypes.includes(eT)) {
|
|---|
| 2000 | ;
|
|---|
| 2001 | optionsWithDefaults.tagTypes.push(eT);
|
|---|
| 2002 | }
|
|---|
| 2003 | }
|
|---|
| 2004 | }
|
|---|
| 2005 | if (endpoints) {
|
|---|
| 2006 | for (const [endpointName, partialDefinition] of Object.entries(endpoints)) {
|
|---|
| 2007 | if (typeof partialDefinition === "function") {
|
|---|
| 2008 | partialDefinition(getEndpointDefinition(context, endpointName));
|
|---|
| 2009 | } else {
|
|---|
| 2010 | Object.assign(getEndpointDefinition(context, endpointName) || {}, partialDefinition);
|
|---|
| 2011 | }
|
|---|
| 2012 | }
|
|---|
| 2013 | }
|
|---|
| 2014 | return api;
|
|---|
| 2015 | }
|
|---|
| 2016 | };
|
|---|
| 2017 | const initializedModules = modules.map((m) => m.init(api, optionsWithDefaults, context));
|
|---|
| 2018 | function injectEndpoints(inject) {
|
|---|
| 2019 | const evaluatedEndpoints = inject.endpoints({
|
|---|
| 2020 | query: (x) => ({
|
|---|
| 2021 | ...x,
|
|---|
| 2022 | type: ENDPOINT_QUERY
|
|---|
| 2023 | }),
|
|---|
| 2024 | mutation: (x) => ({
|
|---|
| 2025 | ...x,
|
|---|
| 2026 | type: ENDPOINT_MUTATION
|
|---|
| 2027 | }),
|
|---|
| 2028 | infiniteQuery: (x) => ({
|
|---|
| 2029 | ...x,
|
|---|
| 2030 | type: ENDPOINT_INFINITEQUERY
|
|---|
| 2031 | })
|
|---|
| 2032 | });
|
|---|
| 2033 | for (const [endpointName, definition] of Object.entries(evaluatedEndpoints)) {
|
|---|
| 2034 | if (inject.overrideExisting !== true && endpointName in context.endpointDefinitions) {
|
|---|
| 2035 | if (inject.overrideExisting === "throw") {
|
|---|
| 2036 | throw new Error(false ? _formatProdErrorMessage2(39) : `called \`injectEndpoints\` to override already-existing endpointName ${endpointName} without specifying \`overrideExisting: true\``);
|
|---|
| 2037 | } else if (typeof process !== "undefined" && true) {
|
|---|
| 2038 | console.error(`called \`injectEndpoints\` to override already-existing endpointName ${endpointName} without specifying \`overrideExisting: true\``);
|
|---|
| 2039 | }
|
|---|
| 2040 | continue;
|
|---|
| 2041 | }
|
|---|
| 2042 | if (typeof process !== "undefined" && true) {
|
|---|
| 2043 | if (isInfiniteQueryDefinition(definition)) {
|
|---|
| 2044 | const {
|
|---|
| 2045 | infiniteQueryOptions
|
|---|
| 2046 | } = definition;
|
|---|
| 2047 | const {
|
|---|
| 2048 | maxPages,
|
|---|
| 2049 | getPreviousPageParam: getPreviousPageParam2
|
|---|
| 2050 | } = infiniteQueryOptions;
|
|---|
| 2051 | if (typeof maxPages === "number") {
|
|---|
| 2052 | if (maxPages < 1) {
|
|---|
| 2053 | throw new Error(false ? _formatProdErrorMessage22(40) : `maxPages for endpoint '${endpointName}' must be a number greater than 0`);
|
|---|
| 2054 | }
|
|---|
| 2055 | if (typeof getPreviousPageParam2 !== "function") {
|
|---|
| 2056 | throw new Error(false ? _formatProdErrorMessage3(41) : `getPreviousPageParam for endpoint '${endpointName}' must be a function if maxPages is used`);
|
|---|
| 2057 | }
|
|---|
| 2058 | }
|
|---|
| 2059 | }
|
|---|
| 2060 | }
|
|---|
| 2061 | context.endpointDefinitions[endpointName] = definition;
|
|---|
| 2062 | for (const m of initializedModules) {
|
|---|
| 2063 | m.injectEndpoint(endpointName, definition);
|
|---|
| 2064 | }
|
|---|
| 2065 | }
|
|---|
| 2066 | return api;
|
|---|
| 2067 | }
|
|---|
| 2068 | return api.injectEndpoints({
|
|---|
| 2069 | endpoints: options.endpoints
|
|---|
| 2070 | });
|
|---|
| 2071 | };
|
|---|
| 2072 | }
|
|---|
| 2073 |
|
|---|
| 2074 | // src/query/fakeBaseQuery.ts
|
|---|
| 2075 | var import_toolkit4 = require("@reduxjs/toolkit");
|
|---|
| 2076 | var _NEVER = /* @__PURE__ */ Symbol();
|
|---|
| 2077 | function fakeBaseQuery() {
|
|---|
| 2078 | return function() {
|
|---|
| 2079 | throw new Error(false ? _formatProdErrorMessage4(33) : "When using `fakeBaseQuery`, all queries & mutations must use the `queryFn` definition syntax.");
|
|---|
| 2080 | };
|
|---|
| 2081 | }
|
|---|
| 2082 |
|
|---|
| 2083 | // src/query/tsHelpers.ts
|
|---|
| 2084 | function assertCast(v) {
|
|---|
| 2085 | }
|
|---|
| 2086 | function safeAssign(target, ...args) {
|
|---|
| 2087 | return Object.assign(target, ...args);
|
|---|
| 2088 | }
|
|---|
| 2089 |
|
|---|
| 2090 | // src/query/core/buildMiddleware/batchActions.ts
|
|---|
| 2091 | var buildBatchedActionsHandler = ({
|
|---|
| 2092 | api,
|
|---|
| 2093 | queryThunk,
|
|---|
| 2094 | internalState,
|
|---|
| 2095 | mwApi
|
|---|
| 2096 | }) => {
|
|---|
| 2097 | const subscriptionsPrefix = `${api.reducerPath}/subscriptions`;
|
|---|
| 2098 | let previousSubscriptions = null;
|
|---|
| 2099 | let updateSyncTimer = null;
|
|---|
| 2100 | const {
|
|---|
| 2101 | updateSubscriptionOptions,
|
|---|
| 2102 | unsubscribeQueryResult
|
|---|
| 2103 | } = api.internalActions;
|
|---|
| 2104 | const actuallyMutateSubscriptions = (currentSubscriptions, action) => {
|
|---|
| 2105 | if (updateSubscriptionOptions.match(action)) {
|
|---|
| 2106 | const {
|
|---|
| 2107 | queryCacheKey,
|
|---|
| 2108 | requestId,
|
|---|
| 2109 | options
|
|---|
| 2110 | } = action.payload;
|
|---|
| 2111 | const sub = currentSubscriptions.get(queryCacheKey);
|
|---|
| 2112 | if (sub?.has(requestId)) {
|
|---|
| 2113 | sub.set(requestId, options);
|
|---|
| 2114 | }
|
|---|
| 2115 | return true;
|
|---|
| 2116 | }
|
|---|
| 2117 | if (unsubscribeQueryResult.match(action)) {
|
|---|
| 2118 | const {
|
|---|
| 2119 | queryCacheKey,
|
|---|
| 2120 | requestId
|
|---|
| 2121 | } = action.payload;
|
|---|
| 2122 | const sub = currentSubscriptions.get(queryCacheKey);
|
|---|
| 2123 | if (sub) {
|
|---|
| 2124 | sub.delete(requestId);
|
|---|
| 2125 | }
|
|---|
| 2126 | return true;
|
|---|
| 2127 | }
|
|---|
| 2128 | if (api.internalActions.removeQueryResult.match(action)) {
|
|---|
| 2129 | currentSubscriptions.delete(action.payload.queryCacheKey);
|
|---|
| 2130 | return true;
|
|---|
| 2131 | }
|
|---|
| 2132 | if (queryThunk.pending.match(action)) {
|
|---|
| 2133 | const {
|
|---|
| 2134 | meta: {
|
|---|
| 2135 | arg,
|
|---|
| 2136 | requestId
|
|---|
| 2137 | }
|
|---|
| 2138 | } = action;
|
|---|
| 2139 | const substate = getOrInsertComputed(currentSubscriptions, arg.queryCacheKey, createNewMap);
|
|---|
| 2140 | if (arg.subscribe) {
|
|---|
| 2141 | substate.set(requestId, arg.subscriptionOptions ?? substate.get(requestId) ?? {});
|
|---|
| 2142 | }
|
|---|
| 2143 | return true;
|
|---|
| 2144 | }
|
|---|
| 2145 | let mutated = false;
|
|---|
| 2146 | if (queryThunk.rejected.match(action)) {
|
|---|
| 2147 | const {
|
|---|
| 2148 | meta: {
|
|---|
| 2149 | condition,
|
|---|
| 2150 | arg,
|
|---|
| 2151 | requestId
|
|---|
| 2152 | }
|
|---|
| 2153 | } = action;
|
|---|
| 2154 | if (condition && arg.subscribe) {
|
|---|
| 2155 | const substate = getOrInsertComputed(currentSubscriptions, arg.queryCacheKey, createNewMap);
|
|---|
| 2156 | substate.set(requestId, arg.subscriptionOptions ?? substate.get(requestId) ?? {});
|
|---|
| 2157 | mutated = true;
|
|---|
| 2158 | }
|
|---|
| 2159 | }
|
|---|
| 2160 | return mutated;
|
|---|
| 2161 | };
|
|---|
| 2162 | const getSubscriptions = () => internalState.currentSubscriptions;
|
|---|
| 2163 | const getSubscriptionCount = (queryCacheKey) => {
|
|---|
| 2164 | const subscriptions = getSubscriptions();
|
|---|
| 2165 | const subscriptionsForQueryArg = subscriptions.get(queryCacheKey);
|
|---|
| 2166 | return subscriptionsForQueryArg?.size ?? 0;
|
|---|
| 2167 | };
|
|---|
| 2168 | const isRequestSubscribed = (queryCacheKey, requestId) => {
|
|---|
| 2169 | const subscriptions = getSubscriptions();
|
|---|
| 2170 | return !!subscriptions?.get(queryCacheKey)?.get(requestId);
|
|---|
| 2171 | };
|
|---|
| 2172 | const subscriptionSelectors = {
|
|---|
| 2173 | getSubscriptions,
|
|---|
| 2174 | getSubscriptionCount,
|
|---|
| 2175 | isRequestSubscribed
|
|---|
| 2176 | };
|
|---|
| 2177 | function serializeSubscriptions(currentSubscriptions) {
|
|---|
| 2178 | return JSON.parse(JSON.stringify(Object.fromEntries([...currentSubscriptions].map(([k, v]) => [k, Object.fromEntries(v)]))));
|
|---|
| 2179 | }
|
|---|
| 2180 | return (action, mwApi2) => {
|
|---|
| 2181 | if (!previousSubscriptions) {
|
|---|
| 2182 | previousSubscriptions = serializeSubscriptions(internalState.currentSubscriptions);
|
|---|
| 2183 | }
|
|---|
| 2184 | if (api.util.resetApiState.match(action)) {
|
|---|
| 2185 | previousSubscriptions = {};
|
|---|
| 2186 | internalState.currentSubscriptions.clear();
|
|---|
| 2187 | updateSyncTimer = null;
|
|---|
| 2188 | return [true, false];
|
|---|
| 2189 | }
|
|---|
| 2190 | if (api.internalActions.internal_getRTKQSubscriptions.match(action)) {
|
|---|
| 2191 | return [false, subscriptionSelectors];
|
|---|
| 2192 | }
|
|---|
| 2193 | const didMutate = actuallyMutateSubscriptions(internalState.currentSubscriptions, action);
|
|---|
| 2194 | let actionShouldContinue = true;
|
|---|
| 2195 | if (false) {
|
|---|
| 2196 | return [false, internalState.currentPolls];
|
|---|
| 2197 | }
|
|---|
| 2198 | if (didMutate) {
|
|---|
| 2199 | if (!updateSyncTimer) {
|
|---|
| 2200 | updateSyncTimer = setTimeout(() => {
|
|---|
| 2201 | const newSubscriptions = serializeSubscriptions(internalState.currentSubscriptions);
|
|---|
| 2202 | const [, patches] = (0, import_immer.produceWithPatches)(previousSubscriptions, () => newSubscriptions);
|
|---|
| 2203 | mwApi2.next(api.internalActions.subscriptionsUpdated(patches));
|
|---|
| 2204 | previousSubscriptions = newSubscriptions;
|
|---|
| 2205 | updateSyncTimer = null;
|
|---|
| 2206 | }, 500);
|
|---|
| 2207 | }
|
|---|
| 2208 | const isSubscriptionSliceAction = typeof action.type == "string" && !!action.type.startsWith(subscriptionsPrefix);
|
|---|
| 2209 | const isAdditionalSubscriptionAction = queryThunk.rejected.match(action) && action.meta.condition && !!action.meta.arg.subscribe;
|
|---|
| 2210 | actionShouldContinue = !isSubscriptionSliceAction && !isAdditionalSubscriptionAction;
|
|---|
| 2211 | }
|
|---|
| 2212 | return [actionShouldContinue, false];
|
|---|
| 2213 | };
|
|---|
| 2214 | };
|
|---|
| 2215 |
|
|---|
| 2216 | // src/query/core/buildMiddleware/cacheCollection.ts
|
|---|
| 2217 | var THIRTY_TWO_BIT_MAX_TIMER_SECONDS = 2147483647 / 1e3 - 1;
|
|---|
| 2218 | var buildCacheCollectionHandler = ({
|
|---|
| 2219 | reducerPath,
|
|---|
| 2220 | api,
|
|---|
| 2221 | queryThunk,
|
|---|
| 2222 | context,
|
|---|
| 2223 | internalState,
|
|---|
| 2224 | selectors: {
|
|---|
| 2225 | selectQueryEntry,
|
|---|
| 2226 | selectConfig
|
|---|
| 2227 | },
|
|---|
| 2228 | getRunningQueryThunk,
|
|---|
| 2229 | mwApi
|
|---|
| 2230 | }) => {
|
|---|
| 2231 | const {
|
|---|
| 2232 | removeQueryResult,
|
|---|
| 2233 | unsubscribeQueryResult,
|
|---|
| 2234 | cacheEntriesUpserted
|
|---|
| 2235 | } = api.internalActions;
|
|---|
| 2236 | const canTriggerUnsubscribe = (0, import_toolkit.isAnyOf)(unsubscribeQueryResult.match, queryThunk.fulfilled, queryThunk.rejected, cacheEntriesUpserted.match);
|
|---|
| 2237 | function anySubscriptionsRemainingForKey(queryCacheKey) {
|
|---|
| 2238 | const subscriptions = internalState.currentSubscriptions.get(queryCacheKey);
|
|---|
| 2239 | if (!subscriptions) {
|
|---|
| 2240 | return false;
|
|---|
| 2241 | }
|
|---|
| 2242 | const hasSubscriptions = subscriptions.size > 0;
|
|---|
| 2243 | return hasSubscriptions;
|
|---|
| 2244 | }
|
|---|
| 2245 | const currentRemovalTimeouts = {};
|
|---|
| 2246 | function abortAllPromises(promiseMap) {
|
|---|
| 2247 | for (const promise of promiseMap.values()) {
|
|---|
| 2248 | promise?.abort?.();
|
|---|
| 2249 | }
|
|---|
| 2250 | }
|
|---|
| 2251 | const handler = (action, mwApi2) => {
|
|---|
| 2252 | const state = mwApi2.getState();
|
|---|
| 2253 | const config = selectConfig(state);
|
|---|
| 2254 | if (canTriggerUnsubscribe(action)) {
|
|---|
| 2255 | let queryCacheKeys;
|
|---|
| 2256 | if (cacheEntriesUpserted.match(action)) {
|
|---|
| 2257 | queryCacheKeys = action.payload.map((entry) => entry.queryDescription.queryCacheKey);
|
|---|
| 2258 | } else {
|
|---|
| 2259 | const {
|
|---|
| 2260 | queryCacheKey
|
|---|
| 2261 | } = unsubscribeQueryResult.match(action) ? action.payload : action.meta.arg;
|
|---|
| 2262 | queryCacheKeys = [queryCacheKey];
|
|---|
| 2263 | }
|
|---|
| 2264 | handleUnsubscribeMany(queryCacheKeys, mwApi2, config);
|
|---|
| 2265 | }
|
|---|
| 2266 | if (api.util.resetApiState.match(action)) {
|
|---|
| 2267 | for (const [key, timeout] of Object.entries(currentRemovalTimeouts)) {
|
|---|
| 2268 | if (timeout) clearTimeout(timeout);
|
|---|
| 2269 | delete currentRemovalTimeouts[key];
|
|---|
| 2270 | }
|
|---|
| 2271 | abortAllPromises(internalState.runningQueries);
|
|---|
| 2272 | abortAllPromises(internalState.runningMutations);
|
|---|
| 2273 | }
|
|---|
| 2274 | if (context.hasRehydrationInfo(action)) {
|
|---|
| 2275 | const {
|
|---|
| 2276 | queries
|
|---|
| 2277 | } = context.extractRehydrationInfo(action);
|
|---|
| 2278 | handleUnsubscribeMany(Object.keys(queries), mwApi2, config);
|
|---|
| 2279 | }
|
|---|
| 2280 | };
|
|---|
| 2281 | function handleUnsubscribeMany(cacheKeys, api2, config) {
|
|---|
| 2282 | const state = api2.getState();
|
|---|
| 2283 | for (const queryCacheKey of cacheKeys) {
|
|---|
| 2284 | const entry = selectQueryEntry(state, queryCacheKey);
|
|---|
| 2285 | if (entry?.endpointName) {
|
|---|
| 2286 | handleUnsubscribe(queryCacheKey, entry.endpointName, api2, config);
|
|---|
| 2287 | }
|
|---|
| 2288 | }
|
|---|
| 2289 | }
|
|---|
| 2290 | function handleUnsubscribe(queryCacheKey, endpointName, api2, config) {
|
|---|
| 2291 | const endpointDefinition = getEndpointDefinition(context, endpointName);
|
|---|
| 2292 | const keepUnusedDataFor = endpointDefinition?.keepUnusedDataFor ?? config.keepUnusedDataFor;
|
|---|
| 2293 | if (keepUnusedDataFor === Infinity) {
|
|---|
| 2294 | return;
|
|---|
| 2295 | }
|
|---|
| 2296 | const finalKeepUnusedDataFor = Math.max(0, Math.min(keepUnusedDataFor, THIRTY_TWO_BIT_MAX_TIMER_SECONDS));
|
|---|
| 2297 | if (!anySubscriptionsRemainingForKey(queryCacheKey)) {
|
|---|
| 2298 | const currentTimeout = currentRemovalTimeouts[queryCacheKey];
|
|---|
| 2299 | if (currentTimeout) {
|
|---|
| 2300 | clearTimeout(currentTimeout);
|
|---|
| 2301 | }
|
|---|
| 2302 | currentRemovalTimeouts[queryCacheKey] = setTimeout(() => {
|
|---|
| 2303 | if (!anySubscriptionsRemainingForKey(queryCacheKey)) {
|
|---|
| 2304 | const entry = selectQueryEntry(api2.getState(), queryCacheKey);
|
|---|
| 2305 | if (entry?.endpointName) {
|
|---|
| 2306 | const runningQuery = api2.dispatch(getRunningQueryThunk(entry.endpointName, entry.originalArgs));
|
|---|
| 2307 | runningQuery?.abort();
|
|---|
| 2308 | }
|
|---|
| 2309 | api2.dispatch(removeQueryResult({
|
|---|
| 2310 | queryCacheKey
|
|---|
| 2311 | }));
|
|---|
| 2312 | }
|
|---|
| 2313 | delete currentRemovalTimeouts[queryCacheKey];
|
|---|
| 2314 | }, finalKeepUnusedDataFor * 1e3);
|
|---|
| 2315 | }
|
|---|
| 2316 | }
|
|---|
| 2317 | return handler;
|
|---|
| 2318 | };
|
|---|
| 2319 |
|
|---|
| 2320 | // src/query/core/buildMiddleware/cacheLifecycle.ts
|
|---|
| 2321 | var neverResolvedError = new Error("Promise never resolved before cacheEntryRemoved.");
|
|---|
| 2322 | var buildCacheLifecycleHandler = ({
|
|---|
| 2323 | api,
|
|---|
| 2324 | reducerPath,
|
|---|
| 2325 | context,
|
|---|
| 2326 | queryThunk,
|
|---|
| 2327 | mutationThunk,
|
|---|
| 2328 | internalState,
|
|---|
| 2329 | selectors: {
|
|---|
| 2330 | selectQueryEntry,
|
|---|
| 2331 | selectApiState
|
|---|
| 2332 | }
|
|---|
| 2333 | }) => {
|
|---|
| 2334 | const isQueryThunk = (0, import_toolkit.isAsyncThunkAction)(queryThunk);
|
|---|
| 2335 | const isMutationThunk = (0, import_toolkit.isAsyncThunkAction)(mutationThunk);
|
|---|
| 2336 | const isFulfilledThunk = (0, import_toolkit.isFulfilled)(queryThunk, mutationThunk);
|
|---|
| 2337 | const lifecycleMap = {};
|
|---|
| 2338 | const {
|
|---|
| 2339 | removeQueryResult,
|
|---|
| 2340 | removeMutationResult,
|
|---|
| 2341 | cacheEntriesUpserted
|
|---|
| 2342 | } = api.internalActions;
|
|---|
| 2343 | function resolveLifecycleEntry(cacheKey, data, meta) {
|
|---|
| 2344 | const lifecycle = lifecycleMap[cacheKey];
|
|---|
| 2345 | if (lifecycle?.valueResolved) {
|
|---|
| 2346 | lifecycle.valueResolved({
|
|---|
| 2347 | data,
|
|---|
| 2348 | meta
|
|---|
| 2349 | });
|
|---|
| 2350 | delete lifecycle.valueResolved;
|
|---|
| 2351 | }
|
|---|
| 2352 | }
|
|---|
| 2353 | function removeLifecycleEntry(cacheKey) {
|
|---|
| 2354 | const lifecycle = lifecycleMap[cacheKey];
|
|---|
| 2355 | if (lifecycle) {
|
|---|
| 2356 | delete lifecycleMap[cacheKey];
|
|---|
| 2357 | lifecycle.cacheEntryRemoved();
|
|---|
| 2358 | }
|
|---|
| 2359 | }
|
|---|
| 2360 | function getActionMetaFields(action) {
|
|---|
| 2361 | const {
|
|---|
| 2362 | arg,
|
|---|
| 2363 | requestId
|
|---|
| 2364 | } = action.meta;
|
|---|
| 2365 | const {
|
|---|
| 2366 | endpointName,
|
|---|
| 2367 | originalArgs
|
|---|
| 2368 | } = arg;
|
|---|
| 2369 | return [endpointName, originalArgs, requestId];
|
|---|
| 2370 | }
|
|---|
| 2371 | const handler = (action, mwApi, stateBefore) => {
|
|---|
| 2372 | const cacheKey = getCacheKey(action);
|
|---|
| 2373 | function checkForNewCacheKey(endpointName, cacheKey2, requestId, originalArgs) {
|
|---|
| 2374 | const oldEntry = selectQueryEntry(stateBefore, cacheKey2);
|
|---|
| 2375 | const newEntry = selectQueryEntry(mwApi.getState(), cacheKey2);
|
|---|
| 2376 | if (!oldEntry && newEntry) {
|
|---|
| 2377 | handleNewKey(endpointName, originalArgs, cacheKey2, mwApi, requestId);
|
|---|
| 2378 | }
|
|---|
| 2379 | }
|
|---|
| 2380 | if (queryThunk.pending.match(action)) {
|
|---|
| 2381 | const [endpointName, originalArgs, requestId] = getActionMetaFields(action);
|
|---|
| 2382 | checkForNewCacheKey(endpointName, cacheKey, requestId, originalArgs);
|
|---|
| 2383 | } else if (cacheEntriesUpserted.match(action)) {
|
|---|
| 2384 | for (const {
|
|---|
| 2385 | queryDescription,
|
|---|
| 2386 | value
|
|---|
| 2387 | } of action.payload) {
|
|---|
| 2388 | const {
|
|---|
| 2389 | endpointName,
|
|---|
| 2390 | originalArgs,
|
|---|
| 2391 | queryCacheKey
|
|---|
| 2392 | } = queryDescription;
|
|---|
| 2393 | checkForNewCacheKey(endpointName, queryCacheKey, action.meta.requestId, originalArgs);
|
|---|
| 2394 | resolveLifecycleEntry(queryCacheKey, value, {});
|
|---|
| 2395 | }
|
|---|
| 2396 | } else if (mutationThunk.pending.match(action)) {
|
|---|
| 2397 | const state = mwApi.getState()[reducerPath].mutations[cacheKey];
|
|---|
| 2398 | if (state) {
|
|---|
| 2399 | const [endpointName, originalArgs, requestId] = getActionMetaFields(action);
|
|---|
| 2400 | handleNewKey(endpointName, originalArgs, cacheKey, mwApi, requestId);
|
|---|
| 2401 | }
|
|---|
| 2402 | } else if (isFulfilledThunk(action)) {
|
|---|
| 2403 | resolveLifecycleEntry(cacheKey, action.payload, action.meta.baseQueryMeta);
|
|---|
| 2404 | } else if (removeQueryResult.match(action) || removeMutationResult.match(action)) {
|
|---|
| 2405 | removeLifecycleEntry(cacheKey);
|
|---|
| 2406 | } else if (api.util.resetApiState.match(action)) {
|
|---|
| 2407 | for (const cacheKey2 of Object.keys(lifecycleMap)) {
|
|---|
| 2408 | removeLifecycleEntry(cacheKey2);
|
|---|
| 2409 | }
|
|---|
| 2410 | }
|
|---|
| 2411 | };
|
|---|
| 2412 | function getCacheKey(action) {
|
|---|
| 2413 | if (isQueryThunk(action)) return action.meta.arg.queryCacheKey;
|
|---|
| 2414 | if (isMutationThunk(action)) {
|
|---|
| 2415 | return action.meta.arg.fixedCacheKey ?? action.meta.requestId;
|
|---|
| 2416 | }
|
|---|
| 2417 | if (removeQueryResult.match(action)) return action.payload.queryCacheKey;
|
|---|
| 2418 | if (removeMutationResult.match(action)) return getMutationCacheKey(action.payload);
|
|---|
| 2419 | return "";
|
|---|
| 2420 | }
|
|---|
| 2421 | function handleNewKey(endpointName, originalArgs, queryCacheKey, mwApi, requestId) {
|
|---|
| 2422 | const endpointDefinition = getEndpointDefinition(context, endpointName);
|
|---|
| 2423 | const onCacheEntryAdded = endpointDefinition?.onCacheEntryAdded;
|
|---|
| 2424 | if (!onCacheEntryAdded) return;
|
|---|
| 2425 | const lifecycle = {};
|
|---|
| 2426 | const cacheEntryRemoved = new Promise((resolve) => {
|
|---|
| 2427 | lifecycle.cacheEntryRemoved = resolve;
|
|---|
| 2428 | });
|
|---|
| 2429 | const cacheDataLoaded = Promise.race([new Promise((resolve) => {
|
|---|
| 2430 | lifecycle.valueResolved = resolve;
|
|---|
| 2431 | }), cacheEntryRemoved.then(() => {
|
|---|
| 2432 | throw neverResolvedError;
|
|---|
| 2433 | })]);
|
|---|
| 2434 | cacheDataLoaded.catch(() => {
|
|---|
| 2435 | });
|
|---|
| 2436 | lifecycleMap[queryCacheKey] = lifecycle;
|
|---|
| 2437 | const selector = api.endpoints[endpointName].select(isAnyQueryDefinition(endpointDefinition) ? originalArgs : queryCacheKey);
|
|---|
| 2438 | const extra = mwApi.dispatch((_, __, extra2) => extra2);
|
|---|
| 2439 | const lifecycleApi = {
|
|---|
| 2440 | ...mwApi,
|
|---|
| 2441 | getCacheEntry: () => selector(mwApi.getState()),
|
|---|
| 2442 | requestId,
|
|---|
| 2443 | extra,
|
|---|
| 2444 | updateCachedData: isAnyQueryDefinition(endpointDefinition) ? (updateRecipe) => mwApi.dispatch(api.util.updateQueryData(endpointName, originalArgs, updateRecipe)) : void 0,
|
|---|
| 2445 | cacheDataLoaded,
|
|---|
| 2446 | cacheEntryRemoved
|
|---|
| 2447 | };
|
|---|
| 2448 | const runningHandler = onCacheEntryAdded(originalArgs, lifecycleApi);
|
|---|
| 2449 | Promise.resolve(runningHandler).catch((e) => {
|
|---|
| 2450 | if (e === neverResolvedError) return;
|
|---|
| 2451 | throw e;
|
|---|
| 2452 | });
|
|---|
| 2453 | }
|
|---|
| 2454 | return handler;
|
|---|
| 2455 | };
|
|---|
| 2456 |
|
|---|
| 2457 | // src/query/core/buildMiddleware/devMiddleware.ts
|
|---|
| 2458 | var buildDevCheckHandler = ({
|
|---|
| 2459 | api,
|
|---|
| 2460 | context: {
|
|---|
| 2461 | apiUid
|
|---|
| 2462 | },
|
|---|
| 2463 | reducerPath
|
|---|
| 2464 | }) => {
|
|---|
| 2465 | return (action, mwApi) => {
|
|---|
| 2466 | if (api.util.resetApiState.match(action)) {
|
|---|
| 2467 | mwApi.dispatch(api.internalActions.middlewareRegistered(apiUid));
|
|---|
| 2468 | }
|
|---|
| 2469 | if (typeof process !== "undefined" && true) {
|
|---|
| 2470 | if (api.internalActions.middlewareRegistered.match(action) && action.payload === apiUid && mwApi.getState()[reducerPath]?.config?.middlewareRegistered === "conflict") {
|
|---|
| 2471 | console.warn(`There is a mismatch between slice and middleware for the reducerPath "${reducerPath}".
|
|---|
| 2472 | You can only have one api per reducer path, this will lead to crashes in various situations!${reducerPath === "api" ? `
|
|---|
| 2473 | If you have multiple apis, you *have* to specify the reducerPath option when using createApi!` : ""}`);
|
|---|
| 2474 | }
|
|---|
| 2475 | }
|
|---|
| 2476 | };
|
|---|
| 2477 | };
|
|---|
| 2478 |
|
|---|
| 2479 | // src/query/core/buildMiddleware/invalidationByTags.ts
|
|---|
| 2480 | var buildInvalidationByTagsHandler = ({
|
|---|
| 2481 | reducerPath,
|
|---|
| 2482 | context,
|
|---|
| 2483 | context: {
|
|---|
| 2484 | endpointDefinitions
|
|---|
| 2485 | },
|
|---|
| 2486 | mutationThunk,
|
|---|
| 2487 | queryThunk,
|
|---|
| 2488 | api,
|
|---|
| 2489 | assertTagType,
|
|---|
| 2490 | refetchQuery,
|
|---|
| 2491 | internalState
|
|---|
| 2492 | }) => {
|
|---|
| 2493 | const {
|
|---|
| 2494 | removeQueryResult
|
|---|
| 2495 | } = api.internalActions;
|
|---|
| 2496 | const isThunkActionWithTags = (0, import_toolkit.isAnyOf)((0, import_toolkit.isFulfilled)(mutationThunk), (0, import_toolkit.isRejectedWithValue)(mutationThunk));
|
|---|
| 2497 | const isQueryEnd = (0, import_toolkit.isAnyOf)((0, import_toolkit.isFulfilled)(queryThunk, mutationThunk), (0, import_toolkit.isRejected)(queryThunk, mutationThunk));
|
|---|
| 2498 | let pendingTagInvalidations = [];
|
|---|
| 2499 | let pendingRequestCount = 0;
|
|---|
| 2500 | const handler = (action, mwApi) => {
|
|---|
| 2501 | if (queryThunk.pending.match(action) || mutationThunk.pending.match(action)) {
|
|---|
| 2502 | pendingRequestCount++;
|
|---|
| 2503 | }
|
|---|
| 2504 | if (isQueryEnd(action)) {
|
|---|
| 2505 | pendingRequestCount = Math.max(0, pendingRequestCount - 1);
|
|---|
| 2506 | }
|
|---|
| 2507 | if (isThunkActionWithTags(action)) {
|
|---|
| 2508 | invalidateTags(calculateProvidedByThunk(action, "invalidatesTags", endpointDefinitions, assertTagType), mwApi);
|
|---|
| 2509 | } else if (isQueryEnd(action)) {
|
|---|
| 2510 | invalidateTags([], mwApi);
|
|---|
| 2511 | } else if (api.util.invalidateTags.match(action)) {
|
|---|
| 2512 | invalidateTags(calculateProvidedBy(action.payload, void 0, void 0, void 0, void 0, assertTagType), mwApi);
|
|---|
| 2513 | }
|
|---|
| 2514 | };
|
|---|
| 2515 | function hasPendingRequests() {
|
|---|
| 2516 | return pendingRequestCount > 0;
|
|---|
| 2517 | }
|
|---|
| 2518 | function invalidateTags(newTags, mwApi) {
|
|---|
| 2519 | const rootState = mwApi.getState();
|
|---|
| 2520 | const state = rootState[reducerPath];
|
|---|
| 2521 | pendingTagInvalidations.push(...newTags);
|
|---|
| 2522 | if (state.config.invalidationBehavior === "delayed" && hasPendingRequests()) {
|
|---|
| 2523 | return;
|
|---|
| 2524 | }
|
|---|
| 2525 | const tags = pendingTagInvalidations;
|
|---|
| 2526 | pendingTagInvalidations = [];
|
|---|
| 2527 | if (tags.length === 0) return;
|
|---|
| 2528 | const toInvalidate = api.util.selectInvalidatedBy(rootState, tags);
|
|---|
| 2529 | context.batch(() => {
|
|---|
| 2530 | const valuesArray = Array.from(toInvalidate.values());
|
|---|
| 2531 | for (const {
|
|---|
| 2532 | queryCacheKey
|
|---|
| 2533 | } of valuesArray) {
|
|---|
| 2534 | const querySubState = state.queries[queryCacheKey];
|
|---|
| 2535 | const subscriptionSubState = getOrInsertComputed(internalState.currentSubscriptions, queryCacheKey, createNewMap);
|
|---|
| 2536 | if (querySubState) {
|
|---|
| 2537 | if (subscriptionSubState.size === 0) {
|
|---|
| 2538 | mwApi.dispatch(removeQueryResult({
|
|---|
| 2539 | queryCacheKey
|
|---|
| 2540 | }));
|
|---|
| 2541 | } else if (querySubState.status !== STATUS_UNINITIALIZED) {
|
|---|
| 2542 | mwApi.dispatch(refetchQuery(querySubState));
|
|---|
| 2543 | }
|
|---|
| 2544 | }
|
|---|
| 2545 | }
|
|---|
| 2546 | });
|
|---|
| 2547 | }
|
|---|
| 2548 | return handler;
|
|---|
| 2549 | };
|
|---|
| 2550 |
|
|---|
| 2551 | // src/query/core/buildMiddleware/polling.ts
|
|---|
| 2552 | var buildPollingHandler = ({
|
|---|
| 2553 | reducerPath,
|
|---|
| 2554 | queryThunk,
|
|---|
| 2555 | api,
|
|---|
| 2556 | refetchQuery,
|
|---|
| 2557 | internalState
|
|---|
| 2558 | }) => {
|
|---|
| 2559 | const {
|
|---|
| 2560 | currentPolls,
|
|---|
| 2561 | currentSubscriptions
|
|---|
| 2562 | } = internalState;
|
|---|
| 2563 | const pendingPollingUpdates = /* @__PURE__ */ new Set();
|
|---|
| 2564 | let pollingUpdateTimer = null;
|
|---|
| 2565 | const handler = (action, mwApi) => {
|
|---|
| 2566 | if (api.internalActions.updateSubscriptionOptions.match(action) || api.internalActions.unsubscribeQueryResult.match(action)) {
|
|---|
| 2567 | schedulePollingUpdate(action.payload.queryCacheKey, mwApi);
|
|---|
| 2568 | }
|
|---|
| 2569 | if (queryThunk.pending.match(action) || queryThunk.rejected.match(action) && action.meta.condition) {
|
|---|
| 2570 | schedulePollingUpdate(action.meta.arg.queryCacheKey, mwApi);
|
|---|
| 2571 | }
|
|---|
| 2572 | if (queryThunk.fulfilled.match(action) || queryThunk.rejected.match(action) && !action.meta.condition) {
|
|---|
| 2573 | startNextPoll(action.meta.arg, mwApi);
|
|---|
| 2574 | }
|
|---|
| 2575 | if (api.util.resetApiState.match(action)) {
|
|---|
| 2576 | clearPolls();
|
|---|
| 2577 | if (pollingUpdateTimer) {
|
|---|
| 2578 | clearTimeout(pollingUpdateTimer);
|
|---|
| 2579 | pollingUpdateTimer = null;
|
|---|
| 2580 | }
|
|---|
| 2581 | pendingPollingUpdates.clear();
|
|---|
| 2582 | }
|
|---|
| 2583 | };
|
|---|
| 2584 | function schedulePollingUpdate(queryCacheKey, api2) {
|
|---|
| 2585 | pendingPollingUpdates.add(queryCacheKey);
|
|---|
| 2586 | if (!pollingUpdateTimer) {
|
|---|
| 2587 | pollingUpdateTimer = setTimeout(() => {
|
|---|
| 2588 | for (const key of pendingPollingUpdates) {
|
|---|
| 2589 | updatePollingInterval({
|
|---|
| 2590 | queryCacheKey: key
|
|---|
| 2591 | }, api2);
|
|---|
| 2592 | }
|
|---|
| 2593 | pendingPollingUpdates.clear();
|
|---|
| 2594 | pollingUpdateTimer = null;
|
|---|
| 2595 | }, 0);
|
|---|
| 2596 | }
|
|---|
| 2597 | }
|
|---|
| 2598 | function startNextPoll({
|
|---|
| 2599 | queryCacheKey
|
|---|
| 2600 | }, api2) {
|
|---|
| 2601 | const state = api2.getState()[reducerPath];
|
|---|
| 2602 | const querySubState = state.queries[queryCacheKey];
|
|---|
| 2603 | const subscriptions = currentSubscriptions.get(queryCacheKey);
|
|---|
| 2604 | if (!querySubState || querySubState.status === STATUS_UNINITIALIZED) return;
|
|---|
| 2605 | const {
|
|---|
| 2606 | lowestPollingInterval,
|
|---|
| 2607 | skipPollingIfUnfocused
|
|---|
| 2608 | } = findLowestPollingInterval(subscriptions);
|
|---|
| 2609 | if (!Number.isFinite(lowestPollingInterval)) return;
|
|---|
| 2610 | const currentPoll = currentPolls.get(queryCacheKey);
|
|---|
| 2611 | if (currentPoll?.timeout) {
|
|---|
| 2612 | clearTimeout(currentPoll.timeout);
|
|---|
| 2613 | currentPoll.timeout = void 0;
|
|---|
| 2614 | }
|
|---|
| 2615 | const nextPollTimestamp = Date.now() + lowestPollingInterval;
|
|---|
| 2616 | currentPolls.set(queryCacheKey, {
|
|---|
| 2617 | nextPollTimestamp,
|
|---|
| 2618 | pollingInterval: lowestPollingInterval,
|
|---|
| 2619 | timeout: setTimeout(() => {
|
|---|
| 2620 | if (state.config.focused || !skipPollingIfUnfocused) {
|
|---|
| 2621 | api2.dispatch(refetchQuery(querySubState));
|
|---|
| 2622 | }
|
|---|
| 2623 | startNextPoll({
|
|---|
| 2624 | queryCacheKey
|
|---|
| 2625 | }, api2);
|
|---|
| 2626 | }, lowestPollingInterval)
|
|---|
| 2627 | });
|
|---|
| 2628 | }
|
|---|
| 2629 | function updatePollingInterval({
|
|---|
| 2630 | queryCacheKey
|
|---|
| 2631 | }, api2) {
|
|---|
| 2632 | const state = api2.getState()[reducerPath];
|
|---|
| 2633 | const querySubState = state.queries[queryCacheKey];
|
|---|
| 2634 | const subscriptions = currentSubscriptions.get(queryCacheKey);
|
|---|
| 2635 | if (!querySubState || querySubState.status === STATUS_UNINITIALIZED) {
|
|---|
| 2636 | return;
|
|---|
| 2637 | }
|
|---|
| 2638 | const {
|
|---|
| 2639 | lowestPollingInterval
|
|---|
| 2640 | } = findLowestPollingInterval(subscriptions);
|
|---|
| 2641 | if (false) {
|
|---|
| 2642 | const updateCounters = currentPolls.pollUpdateCounters ??= {};
|
|---|
| 2643 | updateCounters[queryCacheKey] ??= 0;
|
|---|
| 2644 | updateCounters[queryCacheKey]++;
|
|---|
| 2645 | }
|
|---|
| 2646 | if (!Number.isFinite(lowestPollingInterval)) {
|
|---|
| 2647 | cleanupPollForKey(queryCacheKey);
|
|---|
| 2648 | return;
|
|---|
| 2649 | }
|
|---|
| 2650 | const currentPoll = currentPolls.get(queryCacheKey);
|
|---|
| 2651 | const nextPollTimestamp = Date.now() + lowestPollingInterval;
|
|---|
| 2652 | if (!currentPoll || nextPollTimestamp < currentPoll.nextPollTimestamp) {
|
|---|
| 2653 | startNextPoll({
|
|---|
| 2654 | queryCacheKey
|
|---|
| 2655 | }, api2);
|
|---|
| 2656 | }
|
|---|
| 2657 | }
|
|---|
| 2658 | function cleanupPollForKey(key) {
|
|---|
| 2659 | const existingPoll = currentPolls.get(key);
|
|---|
| 2660 | if (existingPoll?.timeout) {
|
|---|
| 2661 | clearTimeout(existingPoll.timeout);
|
|---|
| 2662 | }
|
|---|
| 2663 | currentPolls.delete(key);
|
|---|
| 2664 | }
|
|---|
| 2665 | function clearPolls() {
|
|---|
| 2666 | for (const key of currentPolls.keys()) {
|
|---|
| 2667 | cleanupPollForKey(key);
|
|---|
| 2668 | }
|
|---|
| 2669 | }
|
|---|
| 2670 | function findLowestPollingInterval(subscribers = /* @__PURE__ */ new Map()) {
|
|---|
| 2671 | let skipPollingIfUnfocused = false;
|
|---|
| 2672 | let lowestPollingInterval = Number.POSITIVE_INFINITY;
|
|---|
| 2673 | for (const entry of subscribers.values()) {
|
|---|
| 2674 | if (!!entry.pollingInterval) {
|
|---|
| 2675 | lowestPollingInterval = Math.min(entry.pollingInterval, lowestPollingInterval);
|
|---|
| 2676 | skipPollingIfUnfocused = entry.skipPollingIfUnfocused || skipPollingIfUnfocused;
|
|---|
| 2677 | }
|
|---|
| 2678 | }
|
|---|
| 2679 | return {
|
|---|
| 2680 | lowestPollingInterval,
|
|---|
| 2681 | skipPollingIfUnfocused
|
|---|
| 2682 | };
|
|---|
| 2683 | }
|
|---|
| 2684 | return handler;
|
|---|
| 2685 | };
|
|---|
| 2686 |
|
|---|
| 2687 | // src/query/core/buildMiddleware/queryLifecycle.ts
|
|---|
| 2688 | var buildQueryLifecycleHandler = ({
|
|---|
| 2689 | api,
|
|---|
| 2690 | context,
|
|---|
| 2691 | queryThunk,
|
|---|
| 2692 | mutationThunk
|
|---|
| 2693 | }) => {
|
|---|
| 2694 | const isPendingThunk = (0, import_toolkit.isPending)(queryThunk, mutationThunk);
|
|---|
| 2695 | const isRejectedThunk = (0, import_toolkit.isRejected)(queryThunk, mutationThunk);
|
|---|
| 2696 | const isFullfilledThunk = (0, import_toolkit.isFulfilled)(queryThunk, mutationThunk);
|
|---|
| 2697 | const lifecycleMap = {};
|
|---|
| 2698 | const handler = (action, mwApi) => {
|
|---|
| 2699 | if (isPendingThunk(action)) {
|
|---|
| 2700 | const {
|
|---|
| 2701 | requestId,
|
|---|
| 2702 | arg: {
|
|---|
| 2703 | endpointName,
|
|---|
| 2704 | originalArgs
|
|---|
| 2705 | }
|
|---|
| 2706 | } = action.meta;
|
|---|
| 2707 | const endpointDefinition = getEndpointDefinition(context, endpointName);
|
|---|
| 2708 | const onQueryStarted = endpointDefinition?.onQueryStarted;
|
|---|
| 2709 | if (onQueryStarted) {
|
|---|
| 2710 | const lifecycle = {};
|
|---|
| 2711 | const queryFulfilled = new Promise((resolve, reject) => {
|
|---|
| 2712 | lifecycle.resolve = resolve;
|
|---|
| 2713 | lifecycle.reject = reject;
|
|---|
| 2714 | });
|
|---|
| 2715 | queryFulfilled.catch(() => {
|
|---|
| 2716 | });
|
|---|
| 2717 | lifecycleMap[requestId] = lifecycle;
|
|---|
| 2718 | const selector = api.endpoints[endpointName].select(isAnyQueryDefinition(endpointDefinition) ? originalArgs : requestId);
|
|---|
| 2719 | const extra = mwApi.dispatch((_, __, extra2) => extra2);
|
|---|
| 2720 | const lifecycleApi = {
|
|---|
| 2721 | ...mwApi,
|
|---|
| 2722 | getCacheEntry: () => selector(mwApi.getState()),
|
|---|
| 2723 | requestId,
|
|---|
| 2724 | extra,
|
|---|
| 2725 | updateCachedData: isAnyQueryDefinition(endpointDefinition) ? (updateRecipe) => mwApi.dispatch(api.util.updateQueryData(endpointName, originalArgs, updateRecipe)) : void 0,
|
|---|
| 2726 | queryFulfilled
|
|---|
| 2727 | };
|
|---|
| 2728 | onQueryStarted(originalArgs, lifecycleApi);
|
|---|
| 2729 | }
|
|---|
| 2730 | } else if (isFullfilledThunk(action)) {
|
|---|
| 2731 | const {
|
|---|
| 2732 | requestId,
|
|---|
| 2733 | baseQueryMeta
|
|---|
| 2734 | } = action.meta;
|
|---|
| 2735 | lifecycleMap[requestId]?.resolve({
|
|---|
| 2736 | data: action.payload,
|
|---|
| 2737 | meta: baseQueryMeta
|
|---|
| 2738 | });
|
|---|
| 2739 | delete lifecycleMap[requestId];
|
|---|
| 2740 | } else if (isRejectedThunk(action)) {
|
|---|
| 2741 | const {
|
|---|
| 2742 | requestId,
|
|---|
| 2743 | rejectedWithValue,
|
|---|
| 2744 | baseQueryMeta
|
|---|
| 2745 | } = action.meta;
|
|---|
| 2746 | lifecycleMap[requestId]?.reject({
|
|---|
| 2747 | error: action.payload ?? action.error,
|
|---|
| 2748 | isUnhandledError: !rejectedWithValue,
|
|---|
| 2749 | meta: baseQueryMeta
|
|---|
| 2750 | });
|
|---|
| 2751 | delete lifecycleMap[requestId];
|
|---|
| 2752 | }
|
|---|
| 2753 | };
|
|---|
| 2754 | return handler;
|
|---|
| 2755 | };
|
|---|
| 2756 |
|
|---|
| 2757 | // src/query/core/buildMiddleware/windowEventHandling.ts
|
|---|
| 2758 | var buildWindowEventHandler = ({
|
|---|
| 2759 | reducerPath,
|
|---|
| 2760 | context,
|
|---|
| 2761 | api,
|
|---|
| 2762 | refetchQuery,
|
|---|
| 2763 | internalState
|
|---|
| 2764 | }) => {
|
|---|
| 2765 | const {
|
|---|
| 2766 | removeQueryResult
|
|---|
| 2767 | } = api.internalActions;
|
|---|
| 2768 | const handler = (action, mwApi) => {
|
|---|
| 2769 | if (onFocus.match(action)) {
|
|---|
| 2770 | refetchValidQueries(mwApi, "refetchOnFocus");
|
|---|
| 2771 | }
|
|---|
| 2772 | if (onOnline.match(action)) {
|
|---|
| 2773 | refetchValidQueries(mwApi, "refetchOnReconnect");
|
|---|
| 2774 | }
|
|---|
| 2775 | };
|
|---|
| 2776 | function refetchValidQueries(api2, type) {
|
|---|
| 2777 | const state = api2.getState()[reducerPath];
|
|---|
| 2778 | const queries = state.queries;
|
|---|
| 2779 | const subscriptions = internalState.currentSubscriptions;
|
|---|
| 2780 | context.batch(() => {
|
|---|
| 2781 | for (const queryCacheKey of subscriptions.keys()) {
|
|---|
| 2782 | const querySubState = queries[queryCacheKey];
|
|---|
| 2783 | const subscriptionSubState = subscriptions.get(queryCacheKey);
|
|---|
| 2784 | if (!subscriptionSubState || !querySubState) continue;
|
|---|
| 2785 | const values = [...subscriptionSubState.values()];
|
|---|
| 2786 | const shouldRefetch = values.some((sub) => sub[type] === true) || values.every((sub) => sub[type] === void 0) && state.config[type];
|
|---|
| 2787 | if (shouldRefetch) {
|
|---|
| 2788 | if (subscriptionSubState.size === 0) {
|
|---|
| 2789 | api2.dispatch(removeQueryResult({
|
|---|
| 2790 | queryCacheKey
|
|---|
| 2791 | }));
|
|---|
| 2792 | } else if (querySubState.status !== STATUS_UNINITIALIZED) {
|
|---|
| 2793 | api2.dispatch(refetchQuery(querySubState));
|
|---|
| 2794 | }
|
|---|
| 2795 | }
|
|---|
| 2796 | }
|
|---|
| 2797 | });
|
|---|
| 2798 | }
|
|---|
| 2799 | return handler;
|
|---|
| 2800 | };
|
|---|
| 2801 |
|
|---|
| 2802 | // src/query/core/buildMiddleware/index.ts
|
|---|
| 2803 | function buildMiddleware(input) {
|
|---|
| 2804 | const {
|
|---|
| 2805 | reducerPath,
|
|---|
| 2806 | queryThunk,
|
|---|
| 2807 | api,
|
|---|
| 2808 | context,
|
|---|
| 2809 | getInternalState
|
|---|
| 2810 | } = input;
|
|---|
| 2811 | const {
|
|---|
| 2812 | apiUid
|
|---|
| 2813 | } = context;
|
|---|
| 2814 | const actions2 = {
|
|---|
| 2815 | invalidateTags: (0, import_toolkit.createAction)(`${reducerPath}/invalidateTags`)
|
|---|
| 2816 | };
|
|---|
| 2817 | const isThisApiSliceAction = (action) => action.type.startsWith(`${reducerPath}/`);
|
|---|
| 2818 | const handlerBuilders = [buildDevCheckHandler, buildCacheCollectionHandler, buildInvalidationByTagsHandler, buildPollingHandler, buildCacheLifecycleHandler, buildQueryLifecycleHandler];
|
|---|
| 2819 | const middleware = (mwApi) => {
|
|---|
| 2820 | let initialized2 = false;
|
|---|
| 2821 | const internalState = getInternalState(mwApi.dispatch);
|
|---|
| 2822 | const builderArgs = {
|
|---|
| 2823 | ...input,
|
|---|
| 2824 | internalState,
|
|---|
| 2825 | refetchQuery,
|
|---|
| 2826 | isThisApiSliceAction,
|
|---|
| 2827 | mwApi
|
|---|
| 2828 | };
|
|---|
| 2829 | const handlers = handlerBuilders.map((build) => build(builderArgs));
|
|---|
| 2830 | const batchedActionsHandler = buildBatchedActionsHandler(builderArgs);
|
|---|
| 2831 | const windowEventsHandler = buildWindowEventHandler(builderArgs);
|
|---|
| 2832 | return (next) => {
|
|---|
| 2833 | return (action) => {
|
|---|
| 2834 | if (!(0, import_toolkit.isAction)(action)) {
|
|---|
| 2835 | return next(action);
|
|---|
| 2836 | }
|
|---|
| 2837 | if (!initialized2) {
|
|---|
| 2838 | initialized2 = true;
|
|---|
| 2839 | mwApi.dispatch(api.internalActions.middlewareRegistered(apiUid));
|
|---|
| 2840 | }
|
|---|
| 2841 | const mwApiWithNext = {
|
|---|
| 2842 | ...mwApi,
|
|---|
| 2843 | next
|
|---|
| 2844 | };
|
|---|
| 2845 | const stateBefore = mwApi.getState();
|
|---|
| 2846 | const [actionShouldContinue, internalProbeResult] = batchedActionsHandler(action, mwApiWithNext, stateBefore);
|
|---|
| 2847 | let res;
|
|---|
| 2848 | if (actionShouldContinue) {
|
|---|
| 2849 | res = next(action);
|
|---|
| 2850 | } else {
|
|---|
| 2851 | res = internalProbeResult;
|
|---|
| 2852 | }
|
|---|
| 2853 | if (!!mwApi.getState()[reducerPath]) {
|
|---|
| 2854 | windowEventsHandler(action, mwApiWithNext, stateBefore);
|
|---|
| 2855 | if (isThisApiSliceAction(action) || context.hasRehydrationInfo(action)) {
|
|---|
| 2856 | for (const handler of handlers) {
|
|---|
| 2857 | handler(action, mwApiWithNext, stateBefore);
|
|---|
| 2858 | }
|
|---|
| 2859 | }
|
|---|
| 2860 | }
|
|---|
| 2861 | return res;
|
|---|
| 2862 | };
|
|---|
| 2863 | };
|
|---|
| 2864 | };
|
|---|
| 2865 | return {
|
|---|
| 2866 | middleware,
|
|---|
| 2867 | actions: actions2
|
|---|
| 2868 | };
|
|---|
| 2869 | function refetchQuery(querySubState) {
|
|---|
| 2870 | return input.api.endpoints[querySubState.endpointName].initiate(querySubState.originalArgs, {
|
|---|
| 2871 | subscribe: false,
|
|---|
| 2872 | forceRefetch: true
|
|---|
| 2873 | });
|
|---|
| 2874 | }
|
|---|
| 2875 | }
|
|---|
| 2876 |
|
|---|
| 2877 | // src/query/core/module.ts
|
|---|
| 2878 | var coreModuleName = /* @__PURE__ */ Symbol();
|
|---|
| 2879 | var coreModule = ({
|
|---|
| 2880 | createSelector: createSelector2 = import_toolkit.createSelector
|
|---|
| 2881 | } = {}) => ({
|
|---|
| 2882 | name: coreModuleName,
|
|---|
| 2883 | init(api, {
|
|---|
| 2884 | baseQuery,
|
|---|
| 2885 | tagTypes,
|
|---|
| 2886 | reducerPath,
|
|---|
| 2887 | serializeQueryArgs,
|
|---|
| 2888 | keepUnusedDataFor,
|
|---|
| 2889 | refetchOnMountOrArgChange,
|
|---|
| 2890 | refetchOnFocus,
|
|---|
| 2891 | refetchOnReconnect,
|
|---|
| 2892 | invalidationBehavior,
|
|---|
| 2893 | onSchemaFailure,
|
|---|
| 2894 | catchSchemaFailure,
|
|---|
| 2895 | skipSchemaValidation
|
|---|
| 2896 | }, context) {
|
|---|
| 2897 | (0, import_immer.enablePatches)();
|
|---|
| 2898 | assertCast(serializeQueryArgs);
|
|---|
| 2899 | const assertTagType = (tag) => {
|
|---|
| 2900 | if (typeof process !== "undefined" && true) {
|
|---|
| 2901 | if (!tagTypes.includes(tag.type)) {
|
|---|
| 2902 | console.error(`Tag type '${tag.type}' was used, but not specified in \`tagTypes\`!`);
|
|---|
| 2903 | }
|
|---|
| 2904 | }
|
|---|
| 2905 | return tag;
|
|---|
| 2906 | };
|
|---|
| 2907 | Object.assign(api, {
|
|---|
| 2908 | reducerPath,
|
|---|
| 2909 | endpoints: {},
|
|---|
| 2910 | internalActions: {
|
|---|
| 2911 | onOnline,
|
|---|
| 2912 | onOffline,
|
|---|
| 2913 | onFocus,
|
|---|
| 2914 | onFocusLost
|
|---|
| 2915 | },
|
|---|
| 2916 | util: {}
|
|---|
| 2917 | });
|
|---|
| 2918 | const selectors = buildSelectors({
|
|---|
| 2919 | serializeQueryArgs,
|
|---|
| 2920 | reducerPath,
|
|---|
| 2921 | createSelector: createSelector2
|
|---|
| 2922 | });
|
|---|
| 2923 | const {
|
|---|
| 2924 | selectInvalidatedBy,
|
|---|
| 2925 | selectCachedArgsForQuery,
|
|---|
| 2926 | buildQuerySelector,
|
|---|
| 2927 | buildInfiniteQuerySelector,
|
|---|
| 2928 | buildMutationSelector
|
|---|
| 2929 | } = selectors;
|
|---|
| 2930 | safeAssign(api.util, {
|
|---|
| 2931 | selectInvalidatedBy,
|
|---|
| 2932 | selectCachedArgsForQuery
|
|---|
| 2933 | });
|
|---|
| 2934 | const {
|
|---|
| 2935 | queryThunk,
|
|---|
| 2936 | infiniteQueryThunk,
|
|---|
| 2937 | mutationThunk,
|
|---|
| 2938 | patchQueryData,
|
|---|
| 2939 | updateQueryData,
|
|---|
| 2940 | upsertQueryData,
|
|---|
| 2941 | prefetch,
|
|---|
| 2942 | buildMatchThunkActions
|
|---|
| 2943 | } = buildThunks({
|
|---|
| 2944 | baseQuery,
|
|---|
| 2945 | reducerPath,
|
|---|
| 2946 | context,
|
|---|
| 2947 | api,
|
|---|
| 2948 | serializeQueryArgs,
|
|---|
| 2949 | assertTagType,
|
|---|
| 2950 | selectors,
|
|---|
| 2951 | onSchemaFailure,
|
|---|
| 2952 | catchSchemaFailure,
|
|---|
| 2953 | skipSchemaValidation
|
|---|
| 2954 | });
|
|---|
| 2955 | const {
|
|---|
| 2956 | reducer,
|
|---|
| 2957 | actions: sliceActions
|
|---|
| 2958 | } = buildSlice({
|
|---|
| 2959 | context,
|
|---|
| 2960 | queryThunk,
|
|---|
| 2961 | infiniteQueryThunk,
|
|---|
| 2962 | mutationThunk,
|
|---|
| 2963 | serializeQueryArgs,
|
|---|
| 2964 | reducerPath,
|
|---|
| 2965 | assertTagType,
|
|---|
| 2966 | config: {
|
|---|
| 2967 | refetchOnFocus,
|
|---|
| 2968 | refetchOnReconnect,
|
|---|
| 2969 | refetchOnMountOrArgChange,
|
|---|
| 2970 | keepUnusedDataFor,
|
|---|
| 2971 | reducerPath,
|
|---|
| 2972 | invalidationBehavior
|
|---|
| 2973 | }
|
|---|
| 2974 | });
|
|---|
| 2975 | safeAssign(api.util, {
|
|---|
| 2976 | patchQueryData,
|
|---|
| 2977 | updateQueryData,
|
|---|
| 2978 | upsertQueryData,
|
|---|
| 2979 | prefetch,
|
|---|
| 2980 | resetApiState: sliceActions.resetApiState,
|
|---|
| 2981 | upsertQueryEntries: sliceActions.cacheEntriesUpserted
|
|---|
| 2982 | });
|
|---|
| 2983 | safeAssign(api.internalActions, sliceActions);
|
|---|
| 2984 | const internalStateMap = /* @__PURE__ */ new WeakMap();
|
|---|
| 2985 | const getInternalState = (dispatch) => {
|
|---|
| 2986 | const state = getOrInsertComputed(internalStateMap, dispatch, () => ({
|
|---|
| 2987 | currentSubscriptions: /* @__PURE__ */ new Map(),
|
|---|
| 2988 | currentPolls: /* @__PURE__ */ new Map(),
|
|---|
| 2989 | runningQueries: /* @__PURE__ */ new Map(),
|
|---|
| 2990 | runningMutations: /* @__PURE__ */ new Map()
|
|---|
| 2991 | }));
|
|---|
| 2992 | return state;
|
|---|
| 2993 | };
|
|---|
| 2994 | const {
|
|---|
| 2995 | buildInitiateQuery,
|
|---|
| 2996 | buildInitiateInfiniteQuery,
|
|---|
| 2997 | buildInitiateMutation,
|
|---|
| 2998 | getRunningMutationThunk,
|
|---|
| 2999 | getRunningMutationsThunk,
|
|---|
| 3000 | getRunningQueriesThunk,
|
|---|
| 3001 | getRunningQueryThunk
|
|---|
| 3002 | } = buildInitiate({
|
|---|
| 3003 | queryThunk,
|
|---|
| 3004 | mutationThunk,
|
|---|
| 3005 | infiniteQueryThunk,
|
|---|
| 3006 | api,
|
|---|
| 3007 | serializeQueryArgs,
|
|---|
| 3008 | context,
|
|---|
| 3009 | getInternalState
|
|---|
| 3010 | });
|
|---|
| 3011 | safeAssign(api.util, {
|
|---|
| 3012 | getRunningMutationThunk,
|
|---|
| 3013 | getRunningMutationsThunk,
|
|---|
| 3014 | getRunningQueryThunk,
|
|---|
| 3015 | getRunningQueriesThunk
|
|---|
| 3016 | });
|
|---|
| 3017 | const {
|
|---|
| 3018 | middleware,
|
|---|
| 3019 | actions: middlewareActions
|
|---|
| 3020 | } = buildMiddleware({
|
|---|
| 3021 | reducerPath,
|
|---|
| 3022 | context,
|
|---|
| 3023 | queryThunk,
|
|---|
| 3024 | mutationThunk,
|
|---|
| 3025 | infiniteQueryThunk,
|
|---|
| 3026 | api,
|
|---|
| 3027 | assertTagType,
|
|---|
| 3028 | selectors,
|
|---|
| 3029 | getRunningQueryThunk,
|
|---|
| 3030 | getInternalState
|
|---|
| 3031 | });
|
|---|
| 3032 | safeAssign(api.util, middlewareActions);
|
|---|
| 3033 | safeAssign(api, {
|
|---|
| 3034 | reducer,
|
|---|
| 3035 | middleware
|
|---|
| 3036 | });
|
|---|
| 3037 | return {
|
|---|
| 3038 | name: coreModuleName,
|
|---|
| 3039 | injectEndpoint(endpointName, definition) {
|
|---|
| 3040 | const anyApi = api;
|
|---|
| 3041 | const endpoint = anyApi.endpoints[endpointName] ??= {};
|
|---|
| 3042 | if (isQueryDefinition(definition)) {
|
|---|
| 3043 | safeAssign(endpoint, {
|
|---|
| 3044 | name: endpointName,
|
|---|
| 3045 | select: buildQuerySelector(endpointName, definition),
|
|---|
| 3046 | initiate: buildInitiateQuery(endpointName, definition)
|
|---|
| 3047 | }, buildMatchThunkActions(queryThunk, endpointName));
|
|---|
| 3048 | }
|
|---|
| 3049 | if (isMutationDefinition(definition)) {
|
|---|
| 3050 | safeAssign(endpoint, {
|
|---|
| 3051 | name: endpointName,
|
|---|
| 3052 | select: buildMutationSelector(),
|
|---|
| 3053 | initiate: buildInitiateMutation(endpointName)
|
|---|
| 3054 | }, buildMatchThunkActions(mutationThunk, endpointName));
|
|---|
| 3055 | }
|
|---|
| 3056 | if (isInfiniteQueryDefinition(definition)) {
|
|---|
| 3057 | safeAssign(endpoint, {
|
|---|
| 3058 | name: endpointName,
|
|---|
| 3059 | select: buildInfiniteQuerySelector(endpointName, definition),
|
|---|
| 3060 | initiate: buildInitiateInfiniteQuery(endpointName, definition)
|
|---|
| 3061 | }, buildMatchThunkActions(queryThunk, endpointName));
|
|---|
| 3062 | }
|
|---|
| 3063 | }
|
|---|
| 3064 | };
|
|---|
| 3065 | }
|
|---|
| 3066 | });
|
|---|
| 3067 |
|
|---|
| 3068 | // src/query/core/index.ts
|
|---|
| 3069 | var createApi = /* @__PURE__ */ buildCreateApi(coreModule());
|
|---|
| 3070 | // Annotate the CommonJS export names for ESM import in node:
|
|---|
| 3071 | 0 && (module.exports = {
|
|---|
| 3072 | NamedSchemaError,
|
|---|
| 3073 | QueryStatus,
|
|---|
| 3074 | _NEVER,
|
|---|
| 3075 | buildCreateApi,
|
|---|
| 3076 | copyWithStructuralSharing,
|
|---|
| 3077 | coreModule,
|
|---|
| 3078 | coreModuleName,
|
|---|
| 3079 | createApi,
|
|---|
| 3080 | defaultSerializeQueryArgs,
|
|---|
| 3081 | fakeBaseQuery,
|
|---|
| 3082 | fetchBaseQuery,
|
|---|
| 3083 | retry,
|
|---|
| 3084 | setupListeners,
|
|---|
| 3085 | skipToken
|
|---|
| 3086 | });
|
|---|
| 3087 | //# sourceMappingURL=rtk-query.development.cjs.map |
|---|