UNPKG

react-query

Version:

Hooks for managing, caching and syncing asynchronous and remote data in React

299 lines (251 loc) 6.59 kB
// TYPES // UTILS export const isServer = typeof window === 'undefined'; export function noop() { return undefined; } export function functionalUpdate(updater, input) { return typeof updater === 'function' ? updater(input) : updater; } export function isValidTimeout(value) { return typeof value === 'number' && value >= 0 && value !== Infinity; } export function difference(array1, array2) { return array1.filter(x => array2.indexOf(x) === -1); } export function replaceAt(array, index, value) { const copy = array.slice(0); copy[index] = value; return copy; } export function timeUntilStale(updatedAt, staleTime) { return Math.max(updatedAt + (staleTime || 0) - Date.now(), 0); } export function parseQueryArgs(arg1, arg2, arg3) { if (!isQueryKey(arg1)) { return arg1; } if (typeof arg2 === 'function') { return { ...arg3, queryKey: arg1, queryFn: arg2 }; } return { ...arg2, queryKey: arg1 }; } export 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 }; } export function parseFilterArgs(arg1, arg2, arg3) { return isQueryKey(arg1) ? [{ ...arg2, queryKey: arg1 }, arg3] : [arg1 || {}, arg2]; } export function parseMutationFilterArgs(arg1, arg2, arg3) { return isQueryKey(arg1) ? [{ ...arg2, mutationKey: arg1 }, arg3] : [arg1 || {}, arg2]; } export 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; } export 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; } export 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. */ export 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`. */ export function partialMatchKey(a, b) { return partialDeepEqual(a, b); } /** * Checks if `b` partially matches with `a`. */ export 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. */ export function replaceEqualDeep(a, b) { if (a === b) { return a; } const array = Array.isArray(a) && Array.isArray(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. */ export 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; } // Copied from: https://github.com/jonschlinkert/is-plain-object export 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]'; } export function isQueryKey(value) { return Array.isArray(value); } export function isError(value) { return value instanceof Error; } export function sleep(timeout) { return new Promise(resolve => { setTimeout(resolve, timeout); }); } /** * Schedules a microtask. * This can be useful to schedule state updates after rendering. */ export function scheduleMicrotask(callback) { sleep(0).then(callback); } export function getAbortController() { if (typeof AbortController === 'function') { return new AbortController(); } }