vue-hooks-plus
Version:
Vue hooks library
133 lines (132 loc) • 4.16 kB
JavaScript
const vue = require("vue");
const cache = require("../utils/cache");
const cachePromise = require("../utils/cachePromise");
const cacheSubscribe = require("../utils/cacheSubscribe");
function _interopNamespace(e) {
if (e && e.__esModule)
return e;
const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
if (e) {
for (const k in e) {
if (k !== "default") {
const d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: () => e[k]
});
}
}
}
n.default = e;
return Object.freeze(n);
}
const cache__namespace = /* @__PURE__ */ _interopNamespace(cache);
const cachePromise__namespace = /* @__PURE__ */ _interopNamespace(cachePromise);
const cacheSubscribe__namespace = /* @__PURE__ */ _interopNamespace(cacheSubscribe);
const useCachePlugin = (fetchInstance, {
cacheKey,
cacheTime = 5 * 60 * 1e3,
staleTime = 0,
setCache: customSetCache,
getCache: customGetCache
}) => {
const unSubscribeRef = vue.ref();
const currentPromiseRef = vue.ref();
const _setCache = (key, cachedData) => {
if (customSetCache) {
customSetCache(cachedData);
} else {
cache__namespace.setCache(key, cacheTime, cachedData);
}
cacheSubscribe__namespace.trigger(key, cachedData.data);
};
const _getCache = (key, params = []) => {
if (customGetCache) {
return customGetCache(params);
}
return cache__namespace.getCache(key);
};
vue.watchEffect(() => {
if (!cacheKey) {
return;
}
const cacheData = _getCache(cacheKey);
if (cacheData && Object.hasOwnProperty.call(cacheData, "data")) {
fetchInstance.state.data = cacheData.data;
fetchInstance.state.params = cacheData.params;
if (staleTime === -1 || new Date().getTime() - cacheData.time <= staleTime) {
fetchInstance.state.loading = false;
}
}
unSubscribeRef.value = cacheSubscribe__namespace.subscribe(cacheKey, (data) => {
fetchInstance.setState({ data });
});
});
vue.onScopeDispose(() => {
var _a;
(_a = unSubscribeRef.value) == null ? void 0 : _a.call(unSubscribeRef);
});
if (!cacheKey) {
return {};
}
return {
name: "cachePlugin",
onBefore: (params) => {
const cacheData = _getCache(cacheKey, params);
if (!cacheData || !Object.hasOwnProperty.call(cacheData, "data")) {
return {};
}
if (staleTime === -1 || new Date().getTime() - cacheData.time <= staleTime) {
return {
loading: false,
data: cacheData == null ? void 0 : cacheData.data,
returnNow: true
};
} else {
return {
data: cacheData == null ? void 0 : cacheData.data
};
}
},
onRequest: (service, args) => {
let servicePromise = cachePromise__namespace.getCachePromise(cacheKey);
if (servicePromise && servicePromise !== currentPromiseRef.value) {
return { servicePromise };
}
servicePromise = service(...args);
currentPromiseRef.value = servicePromise;
cachePromise__namespace.setCachePromise(cacheKey, servicePromise);
return { servicePromise };
},
onSuccess: (data, params) => {
var _a;
if (cacheKey) {
(_a = unSubscribeRef.value) == null ? void 0 : _a.call(unSubscribeRef);
_setCache(cacheKey, {
data,
params,
time: new Date().getTime()
});
unSubscribeRef.value = cacheSubscribe__namespace.subscribe(cacheKey, (d) => {
fetchInstance.setState({ data: d });
});
}
},
onMutate: (data) => {
var _a;
if (cacheKey) {
(_a = unSubscribeRef.value) == null ? void 0 : _a.call(unSubscribeRef);
_setCache(cacheKey, {
data,
params: fetchInstance.state.params,
time: new Date().getTime()
});
unSubscribeRef.value = cacheSubscribe__namespace.subscribe(cacheKey, (d) => {
fetchInstance.setState({ data: d });
});
}
}
};
};
module.exports = useCachePlugin;
;