vue-use-query
Version:
vue use query
277 lines (276 loc) • 8.89 kB
JavaScript
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
import { isRef, unref } from 'vue';
// UTILS
export var 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 ensureQueryKeyArray(value) {
return (Array.isArray(value)
? value
: [value]);
}
export function difference(array1, array2) {
return array1.filter(function (x) { return array2.indexOf(x) === -1; });
}
export function replaceAt(array, index, value) {
var 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 __assign(__assign({}, arg3), { queryKey: arg1, queryFn: arg2 });
}
return __assign(__assign({}, arg2), { queryKey: arg1 });
}
export function parseMutationArgs(arg1, arg2, arg3) {
if (isQueryKey(arg1)) {
if (typeof arg2 === 'function') {
return __assign(__assign({}, arg3), { mutationKey: arg1, mutationFn: arg2 });
}
return __assign(__assign({}, arg2), { mutationKey: arg1 });
}
if (typeof arg1 === 'function') {
return __assign(__assign({}, arg2), { mutationFn: arg1 });
}
return __assign({}, arg1);
}
export function parseFilterArgs(arg1, arg2, arg3) {
return (isQueryKey(arg1)
? [__assign(__assign({}, arg2), { queryKey: arg1 }), arg3]
: [arg1 || {}, arg2]);
}
export function mapQueryStatusFilter(active, inactive) {
if ((active === true && inactive === true) ||
(active == null && inactive == null)) {
return 'all';
}
else if (active === false && inactive === false) {
return 'none';
}
else {
// At this point, active|inactive can only be true|false or false|true
// so, when only one value is provided, the missing one has to be the negated value
var isActive = active !== null && active !== void 0 ? active : !inactive;
return isActive ? 'active' : 'inactive';
}
}
export function matchQuery(filters, query) {
var active = filters.active, exact = filters.exact, fetching = filters.fetching, inactive = filters.inactive, predicate = filters.predicate, queryKey = filters.queryKey, stale = filters.stale;
if (isQueryKey(queryKey)) {
if (exact) {
if (query.queryHash !== hashQueryKeyByOptions(queryKey, query.options)) {
return false;
}
}
else if (!partialMatchKey(query.queryKey, queryKey)) {
return false;
}
}
var queryStatusFilter = mapQueryStatusFilter(active, inactive);
if (queryStatusFilter === 'none') {
return false;
}
else if (queryStatusFilter !== 'all') {
var isActive = query.isActive();
if (queryStatusFilter === 'active' && !isActive) {
return false;
}
if (queryStatusFilter === 'inactive' && isActive) {
return false;
}
}
if (typeof stale === 'boolean' && query.isStale() !== stale) {
return false;
}
if (typeof fetching === 'boolean' && query.isFetching() !== fetching) {
return false;
}
if (predicate && !predicate(query)) {
return false;
}
return true;
}
export function matchMutation(filters, mutation) {
var exact = filters.exact, fetching = filters.fetching, predicate = filters.predicate, mutationKey = filters.mutationKey;
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) {
var hashFn = (options === null || options === void 0 ? void 0 : options.queryKeyHashFn) || hashQueryKey;
return hashFn(queryKey);
}
/**
* Default query keys hash function.
*/
export function hashQueryKey(queryKey) {
var asArray = ensureQueryKeyArray(queryKey);
return stableValueHash(asArray);
}
/**
* Hashes the value into a stable hash.
*/
export function stableValueHash(value) {
return JSON.stringify(value, function (_, val) {
return isPlainObject(val)
? Object.keys(val)
.sort()
.reduce(function (result, key) {
result[key] = val[key];
return result;
}, {})
: isRef(val)
? unref(val)
: val;
});
}
/**
* Checks if key `b` partially matches with key `a`.
*/
export function partialMatchKey(a, b) {
return partialDeepEqual(ensureQueryKeyArray(a), ensureQueryKeyArray(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(function (key) { return !partialDeepEqual(a[key], b[key]); });
}
return false;
}
export function replaceEqualDeep(a, b) {
if (a === b) {
return a;
}
var array = Array.isArray(a) && Array.isArray(b);
if (array || (isPlainObject(a) && isPlainObject(b))) {
var aSize = array ? a.length : Object.keys(a).length;
var bItems = array ? b : Object.keys(b);
var bSize = bItems.length;
var copy = array ? [] : {};
var equalItems = 0;
for (var i = 0; i < bSize; i++) {
var 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 (var 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
var ctor = o.constructor;
if (typeof ctor === 'undefined') {
return true;
}
// If has modified prototype
var 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 typeof value === 'string' || Array.isArray(value);
}
export function isError(value) {
return value instanceof Error;
}
export function sleep(timeout) {
return new Promise(function (resolve) {
setTimeout(resolve, timeout);
});
}
/**
* Schedules a microtask.
* This can be useful to schedule state updates after rendering.
*/
export function scheduleMicrotask(callback) {
Promise.resolve()
.then(callback)
.catch(function (error) {
return setTimeout(function () {
throw error;
});
});
}