@slan-health/taro-vueuse
Version:
taro vue版本hooks
116 lines (115 loc) • 3.7 kB
JavaScript
import { ref, onUnmounted } from "vue";
import { definePlugin } from "../definePlugin.js";
import { isFunction } from "../utils/index.js";
import { getCache, setCache } from "../utils/cache.js";
import { getCacheQuery, setCacheQuery } from "../utils/cacheQuery.js";
import { trigger, subscribe } from "../utils/cacheSubscribe.js";
const useCachePlugin = definePlugin(
(queryInstance, {
cacheKey: customCacheKey,
cacheTime = 6e5,
staleTime = 0,
getCache: customGetCache,
setCache: customSetCache
}) => {
if (!customCacheKey) return {};
const cacheKey = isFunction(customCacheKey) ? customCacheKey : () => customCacheKey;
const unSubscribe = ref(() => {
});
let currentQuery;
const _getCache = (key) => {
if (customGetCache) {
return customGetCache(key);
} else {
return getCache(key);
}
};
const _setCache = (key, time, cacheData) => {
if (customSetCache) {
customSetCache(key, cacheData);
} else {
setCache(key, time, cacheData);
}
trigger(key, cacheData.data);
};
const isFresh = (time) => staleTime === -1 || time + staleTime > (/* @__PURE__ */ new Date()).getTime();
const hasProp = (object, prop) => Object.prototype.hasOwnProperty.call(object, prop);
const subscribeCache = (params) => {
const _cacheKey2 = cacheKey(params);
return subscribe(_cacheKey2, (data) => {
queryInstance.data.value = data;
});
};
const _cacheKey = cacheKey();
const cache = _getCache(_cacheKey);
if (cache && hasProp(cache, "data")) {
queryInstance.data.value = cache.data;
queryInstance.params.value = cache.params;
}
if (_cacheKey) {
unSubscribe.value = subscribeCache();
}
onUnmounted(() => {
unSubscribe.value();
});
return {
onBefore(params) {
const _cacheKey2 = cacheKey(params);
const cache2 = _getCache(_cacheKey2);
if (!cache2 || !hasProp(cache2, "data")) {
return {};
}
if (isFresh(cache2.time)) {
queryInstance.data.value = cache2.data;
queryInstance.loading.value = false;
return {
isBreak: true,
breakResult: cache2.data
};
} else {
queryInstance.data.value = cache2.data;
}
},
onQuery(service) {
const params = queryInstance.params.value;
const _cacheKey2 = cacheKey(params);
let servicePromise = getCacheQuery(_cacheKey2);
if (servicePromise && servicePromise !== currentQuery) {
return () => servicePromise;
}
servicePromise = service();
currentQuery = servicePromise;
setCacheQuery(_cacheKey2, servicePromise);
return () => servicePromise;
},
onSuccess(data, params) {
const _cacheKey2 = cacheKey(params);
if (_cacheKey2) {
unSubscribe.value();
_setCache(_cacheKey2, cacheTime, {
data,
params,
time: (/* @__PURE__ */ new Date()).getTime()
});
unSubscribe.value = subscribeCache(params);
}
},
onMutate(data) {
const _cacheKey2 = cacheKey(queryInstance.params.value);
if (_cacheKey2) {
unSubscribe.value();
_setCache(_cacheKey2, cacheTime, {
data,
params: queryInstance.params.value,
time: (/* @__PURE__ */ new Date()).getTime()
});
unSubscribe.value = subscribeCache(queryInstance.params.value);
}
}
};
}
);
export {
useCachePlugin as default
};
//# sourceMappingURL=useCachePlugin.js.map