utils.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. // TYPES
  4. // UTILS
  5. const isServer = typeof window === 'undefined' || 'Deno' in window;
  6. function noop() {
  7. return undefined;
  8. }
  9. function functionalUpdate(updater, input) {
  10. return typeof updater === 'function' ? updater(input) : updater;
  11. }
  12. function isValidTimeout(value) {
  13. return typeof value === 'number' && value >= 0 && value !== Infinity;
  14. }
  15. function difference(array1, array2) {
  16. return array1.filter(x => !array2.includes(x));
  17. }
  18. function replaceAt(array, index, value) {
  19. const copy = array.slice(0);
  20. copy[index] = value;
  21. return copy;
  22. }
  23. function timeUntilStale(updatedAt, staleTime) {
  24. return Math.max(updatedAt + (staleTime || 0) - Date.now(), 0);
  25. }
  26. function parseQueryArgs(arg1, arg2, arg3) {
  27. if (!isQueryKey(arg1)) {
  28. return arg1;
  29. }
  30. if (typeof arg2 === 'function') {
  31. return { ...arg3,
  32. queryKey: arg1,
  33. queryFn: arg2
  34. };
  35. }
  36. return { ...arg2,
  37. queryKey: arg1
  38. };
  39. }
  40. function parseMutationArgs(arg1, arg2, arg3) {
  41. if (isQueryKey(arg1)) {
  42. if (typeof arg2 === 'function') {
  43. return { ...arg3,
  44. mutationKey: arg1,
  45. mutationFn: arg2
  46. };
  47. }
  48. return { ...arg2,
  49. mutationKey: arg1
  50. };
  51. }
  52. if (typeof arg1 === 'function') {
  53. return { ...arg2,
  54. mutationFn: arg1
  55. };
  56. }
  57. return { ...arg1
  58. };
  59. }
  60. function parseFilterArgs(arg1, arg2, arg3) {
  61. return isQueryKey(arg1) ? [{ ...arg2,
  62. queryKey: arg1
  63. }, arg3] : [arg1 || {}, arg2];
  64. }
  65. function parseMutationFilterArgs(arg1, arg2, arg3) {
  66. return isQueryKey(arg1) ? [{ ...arg2,
  67. mutationKey: arg1
  68. }, arg3] : [arg1 || {}, arg2];
  69. }
  70. function matchQuery(filters, query) {
  71. const {
  72. type = 'all',
  73. exact,
  74. fetchStatus,
  75. predicate,
  76. queryKey,
  77. stale
  78. } = filters;
  79. if (isQueryKey(queryKey)) {
  80. if (exact) {
  81. if (query.queryHash !== hashQueryKeyByOptions(queryKey, query.options)) {
  82. return false;
  83. }
  84. } else if (!partialMatchKey(query.queryKey, queryKey)) {
  85. return false;
  86. }
  87. }
  88. if (type !== 'all') {
  89. const isActive = query.isActive();
  90. if (type === 'active' && !isActive) {
  91. return false;
  92. }
  93. if (type === 'inactive' && isActive) {
  94. return false;
  95. }
  96. }
  97. if (typeof stale === 'boolean' && query.isStale() !== stale) {
  98. return false;
  99. }
  100. if (typeof fetchStatus !== 'undefined' && fetchStatus !== query.state.fetchStatus) {
  101. return false;
  102. }
  103. if (predicate && !predicate(query)) {
  104. return false;
  105. }
  106. return true;
  107. }
  108. function matchMutation(filters, mutation) {
  109. const {
  110. exact,
  111. fetching,
  112. predicate,
  113. mutationKey
  114. } = filters;
  115. if (isQueryKey(mutationKey)) {
  116. if (!mutation.options.mutationKey) {
  117. return false;
  118. }
  119. if (exact) {
  120. if (hashQueryKey(mutation.options.mutationKey) !== hashQueryKey(mutationKey)) {
  121. return false;
  122. }
  123. } else if (!partialMatchKey(mutation.options.mutationKey, mutationKey)) {
  124. return false;
  125. }
  126. }
  127. if (typeof fetching === 'boolean' && mutation.state.status === 'loading' !== fetching) {
  128. return false;
  129. }
  130. if (predicate && !predicate(mutation)) {
  131. return false;
  132. }
  133. return true;
  134. }
  135. function hashQueryKeyByOptions(queryKey, options) {
  136. const hashFn = (options == null ? void 0 : options.queryKeyHashFn) || hashQueryKey;
  137. return hashFn(queryKey);
  138. }
  139. /**
  140. * Default query keys hash function.
  141. * Hashes the value into a stable hash.
  142. */
  143. function hashQueryKey(queryKey) {
  144. return JSON.stringify(queryKey, (_, val) => isPlainObject(val) ? Object.keys(val).sort().reduce((result, key) => {
  145. result[key] = val[key];
  146. return result;
  147. }, {}) : val);
  148. }
  149. /**
  150. * Checks if key `b` partially matches with key `a`.
  151. */
  152. function partialMatchKey(a, b) {
  153. return partialDeepEqual(a, b);
  154. }
  155. /**
  156. * Checks if `b` partially matches with `a`.
  157. */
  158. function partialDeepEqual(a, b) {
  159. if (a === b) {
  160. return true;
  161. }
  162. if (typeof a !== typeof b) {
  163. return false;
  164. }
  165. if (a && b && typeof a === 'object' && typeof b === 'object') {
  166. return !Object.keys(b).some(key => !partialDeepEqual(a[key], b[key]));
  167. }
  168. return false;
  169. }
  170. /**
  171. * This function returns `a` if `b` is deeply equal.
  172. * If not, it will replace any deeply equal children of `b` with those of `a`.
  173. * This can be used for structural sharing between JSON values for example.
  174. */
  175. function replaceEqualDeep(a, b) {
  176. if (a === b) {
  177. return a;
  178. }
  179. const array = isPlainArray(a) && isPlainArray(b);
  180. if (array || isPlainObject(a) && isPlainObject(b)) {
  181. const aSize = array ? a.length : Object.keys(a).length;
  182. const bItems = array ? b : Object.keys(b);
  183. const bSize = bItems.length;
  184. const copy = array ? [] : {};
  185. let equalItems = 0;
  186. for (let i = 0; i < bSize; i++) {
  187. const key = array ? i : bItems[i];
  188. copy[key] = replaceEqualDeep(a[key], b[key]);
  189. if (copy[key] === a[key]) {
  190. equalItems++;
  191. }
  192. }
  193. return aSize === bSize && equalItems === aSize ? a : copy;
  194. }
  195. return b;
  196. }
  197. /**
  198. * Shallow compare objects. Only works with objects that always have the same properties.
  199. */
  200. function shallowEqualObjects(a, b) {
  201. if (a && !b || b && !a) {
  202. return false;
  203. }
  204. for (const key in a) {
  205. if (a[key] !== b[key]) {
  206. return false;
  207. }
  208. }
  209. return true;
  210. }
  211. function isPlainArray(value) {
  212. return Array.isArray(value) && value.length === Object.keys(value).length;
  213. } // Copied from: https://github.com/jonschlinkert/is-plain-object
  214. function isPlainObject(o) {
  215. if (!hasObjectPrototype(o)) {
  216. return false;
  217. } // If has modified constructor
  218. const ctor = o.constructor;
  219. if (typeof ctor === 'undefined') {
  220. return true;
  221. } // If has modified prototype
  222. const prot = ctor.prototype;
  223. if (!hasObjectPrototype(prot)) {
  224. return false;
  225. } // If constructor does not have an Object-specific method
  226. if (!prot.hasOwnProperty('isPrototypeOf')) {
  227. return false;
  228. } // Most likely a plain Object
  229. return true;
  230. }
  231. function hasObjectPrototype(o) {
  232. return Object.prototype.toString.call(o) === '[object Object]';
  233. }
  234. function isQueryKey(value) {
  235. return Array.isArray(value);
  236. }
  237. function isError(value) {
  238. return value instanceof Error;
  239. }
  240. function sleep(timeout) {
  241. return new Promise(resolve => {
  242. setTimeout(resolve, timeout);
  243. });
  244. }
  245. /**
  246. * Schedules a microtask.
  247. * This can be useful to schedule state updates after rendering.
  248. */
  249. function scheduleMicrotask(callback) {
  250. sleep(0).then(callback);
  251. }
  252. function getAbortController() {
  253. if (typeof AbortController === 'function') {
  254. return new AbortController();
  255. }
  256. return;
  257. }
  258. function replaceData(prevData, data, options) {
  259. // Use prev data if an isDataEqual function is defined and returns `true`
  260. if (options.isDataEqual != null && options.isDataEqual(prevData, data)) {
  261. return prevData;
  262. } else if (typeof options.structuralSharing === 'function') {
  263. return options.structuralSharing(prevData, data);
  264. } else if (options.structuralSharing !== false) {
  265. // Structurally share data between prev and new data if needed
  266. return replaceEqualDeep(prevData, data);
  267. }
  268. return data;
  269. }
  270. exports.difference = difference;
  271. exports.functionalUpdate = functionalUpdate;
  272. exports.getAbortController = getAbortController;
  273. exports.hashQueryKey = hashQueryKey;
  274. exports.hashQueryKeyByOptions = hashQueryKeyByOptions;
  275. exports.isError = isError;
  276. exports.isPlainArray = isPlainArray;
  277. exports.isPlainObject = isPlainObject;
  278. exports.isQueryKey = isQueryKey;
  279. exports.isServer = isServer;
  280. exports.isValidTimeout = isValidTimeout;
  281. exports.matchMutation = matchMutation;
  282. exports.matchQuery = matchQuery;
  283. exports.noop = noop;
  284. exports.parseFilterArgs = parseFilterArgs;
  285. exports.parseMutationArgs = parseMutationArgs;
  286. exports.parseMutationFilterArgs = parseMutationFilterArgs;
  287. exports.parseQueryArgs = parseQueryArgs;
  288. exports.partialDeepEqual = partialDeepEqual;
  289. exports.partialMatchKey = partialMatchKey;
  290. exports.replaceAt = replaceAt;
  291. exports.replaceData = replaceData;
  292. exports.replaceEqualDeep = replaceEqualDeep;
  293. exports.scheduleMicrotask = scheduleMicrotask;
  294. exports.shallowEqualObjects = shallowEqualObjects;
  295. exports.sleep = sleep;
  296. exports.timeUntilStale = timeUntilStale;
  297. //# sourceMappingURL=utils.js.map