123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- // TYPES
- // UTILS
- const isServer = typeof window === 'undefined' || 'Deno' in window;
- function noop() {
- return undefined;
- }
- function functionalUpdate(updater, input) {
- return typeof updater === 'function' ? updater(input) : updater;
- }
- function isValidTimeout(value) {
- return typeof value === 'number' && value >= 0 && value !== Infinity;
- }
- function difference(array1, array2) {
- return array1.filter(x => !array2.includes(x));
- }
- function replaceAt(array, index, value) {
- const copy = array.slice(0);
- copy[index] = value;
- return copy;
- }
- function timeUntilStale(updatedAt, staleTime) {
- return Math.max(updatedAt + (staleTime || 0) - Date.now(), 0);
- }
- function parseQueryArgs(arg1, arg2, arg3) {
- if (!isQueryKey(arg1)) {
- return arg1;
- }
- if (typeof arg2 === 'function') {
- return { ...arg3,
- queryKey: arg1,
- queryFn: arg2
- };
- }
- return { ...arg2,
- queryKey: arg1
- };
- }
- function parseMutationArgs(arg1, arg2, arg3) {
- if (isQueryKey(arg1)) {
- if (typeof arg2 === 'function') {
- return { ...arg3,
- mutationKey: arg1,
- mutationFn: arg2
- };
- }
- return { ...arg2,
- mutationKey: arg1
- };
- }
- if (typeof arg1 === 'function') {
- return { ...arg2,
- mutationFn: arg1
- };
- }
- return { ...arg1
- };
- }
- function parseFilterArgs(arg1, arg2, arg3) {
- return isQueryKey(arg1) ? [{ ...arg2,
- queryKey: arg1
- }, arg3] : [arg1 || {}, arg2];
- }
- function parseMutationFilterArgs(arg1, arg2, arg3) {
- return isQueryKey(arg1) ? [{ ...arg2,
- mutationKey: arg1
- }, arg3] : [arg1 || {}, arg2];
- }
- function matchQuery(filters, query) {
- const {
- type = 'all',
- exact,
- fetchStatus,
- predicate,
- queryKey,
- stale
- } = filters;
- if (isQueryKey(queryKey)) {
- if (exact) {
- if (query.queryHash !== hashQueryKeyByOptions(queryKey, query.options)) {
- return false;
- }
- } else if (!partialMatchKey(query.queryKey, queryKey)) {
- return false;
- }
- }
- if (type !== 'all') {
- const isActive = query.isActive();
- if (type === 'active' && !isActive) {
- return false;
- }
- if (type === 'inactive' && isActive) {
- return false;
- }
- }
- if (typeof stale === 'boolean' && query.isStale() !== stale) {
- return false;
- }
- if (typeof fetchStatus !== 'undefined' && fetchStatus !== query.state.fetchStatus) {
- return false;
- }
- if (predicate && !predicate(query)) {
- return false;
- }
- return true;
- }
- function matchMutation(filters, mutation) {
- const {
- exact,
- fetching,
- predicate,
- mutationKey
- } = filters;
- if (isQueryKey(mutationKey)) {
- if (!mutation.options.mutationKey) {
- return false;
- }
- if (exact) {
- if (hashQueryKey(mutation.options.mutationKey) !== hashQueryKey(mutationKey)) {
- return false;
- }
- } else if (!partialMatchKey(mutation.options.mutationKey, mutationKey)) {
- return false;
- }
- }
- if (typeof fetching === 'boolean' && mutation.state.status === 'loading' !== fetching) {
- return false;
- }
- if (predicate && !predicate(mutation)) {
- return false;
- }
- return true;
- }
- function hashQueryKeyByOptions(queryKey, options) {
- const hashFn = (options == null ? void 0 : options.queryKeyHashFn) || hashQueryKey;
- return hashFn(queryKey);
- }
- /**
- * Default query keys hash function.
- * Hashes the value into a stable hash.
- */
- function hashQueryKey(queryKey) {
- return JSON.stringify(queryKey, (_, val) => isPlainObject(val) ? Object.keys(val).sort().reduce((result, key) => {
- result[key] = val[key];
- return result;
- }, {}) : val);
- }
- /**
- * Checks if key `b` partially matches with key `a`.
- */
- function partialMatchKey(a, b) {
- return partialDeepEqual(a, b);
- }
- /**
- * Checks if `b` partially matches with `a`.
- */
- function partialDeepEqual(a, b) {
- if (a === b) {
- return true;
- }
- if (typeof a !== typeof b) {
- return false;
- }
- if (a && b && typeof a === 'object' && typeof b === 'object') {
- return !Object.keys(b).some(key => !partialDeepEqual(a[key], b[key]));
- }
- return false;
- }
- /**
- * This function returns `a` if `b` is deeply equal.
- * If not, it will replace any deeply equal children of `b` with those of `a`.
- * This can be used for structural sharing between JSON values for example.
- */
- function replaceEqualDeep(a, b) {
- if (a === b) {
- return a;
- }
- const array = isPlainArray(a) && isPlainArray(b);
- if (array || isPlainObject(a) && isPlainObject(b)) {
- const aSize = array ? a.length : Object.keys(a).length;
- const bItems = array ? b : Object.keys(b);
- const bSize = bItems.length;
- const copy = array ? [] : {};
- let equalItems = 0;
- for (let i = 0; i < bSize; i++) {
- const key = array ? i : bItems[i];
- copy[key] = replaceEqualDeep(a[key], b[key]);
- if (copy[key] === a[key]) {
- equalItems++;
- }
- }
- return aSize === bSize && equalItems === aSize ? a : copy;
- }
- return b;
- }
- /**
- * Shallow compare objects. Only works with objects that always have the same properties.
- */
- function shallowEqualObjects(a, b) {
- if (a && !b || b && !a) {
- return false;
- }
- for (const key in a) {
- if (a[key] !== b[key]) {
- return false;
- }
- }
- return true;
- }
- function isPlainArray(value) {
- return Array.isArray(value) && value.length === Object.keys(value).length;
- } // Copied from: https://github.com/jonschlinkert/is-plain-object
- function isPlainObject(o) {
- if (!hasObjectPrototype(o)) {
- return false;
- } // If has modified constructor
- const ctor = o.constructor;
- if (typeof ctor === 'undefined') {
- return true;
- } // If has modified prototype
- const prot = ctor.prototype;
- if (!hasObjectPrototype(prot)) {
- return false;
- } // If constructor does not have an Object-specific method
- if (!prot.hasOwnProperty('isPrototypeOf')) {
- return false;
- } // Most likely a plain Object
- return true;
- }
- function hasObjectPrototype(o) {
- return Object.prototype.toString.call(o) === '[object Object]';
- }
- function isQueryKey(value) {
- return Array.isArray(value);
- }
- function isError(value) {
- return value instanceof Error;
- }
- function sleep(timeout) {
- return new Promise(resolve => {
- setTimeout(resolve, timeout);
- });
- }
- /**
- * Schedules a microtask.
- * This can be useful to schedule state updates after rendering.
- */
- function scheduleMicrotask(callback) {
- sleep(0).then(callback);
- }
- function getAbortController() {
- if (typeof AbortController === 'function') {
- return new AbortController();
- }
- return;
- }
- function replaceData(prevData, data, options) {
- // Use prev data if an isDataEqual function is defined and returns `true`
- if (options.isDataEqual != null && options.isDataEqual(prevData, data)) {
- return prevData;
- } else if (typeof options.structuralSharing === 'function') {
- return options.structuralSharing(prevData, data);
- } else if (options.structuralSharing !== false) {
- // Structurally share data between prev and new data if needed
- return replaceEqualDeep(prevData, data);
- }
- return data;
- }
- export { difference, functionalUpdate, getAbortController, hashQueryKey, hashQueryKeyByOptions, isError, isPlainArray, isPlainObject, isQueryKey, isServer, isValidTimeout, matchMutation, matchQuery, noop, parseFilterArgs, parseMutationArgs, parseMutationFilterArgs, parseQueryArgs, partialDeepEqual, partialMatchKey, replaceAt, replaceData, replaceEqualDeep, scheduleMicrotask, shallowEqualObjects, sleep, timeUntilStale };
- //# sourceMappingURL=utils.mjs.map
|