UNPKG

vue-admin-core

Version:
117 lines (114 loc) 3.96 kB
import { ref, toValue } from 'vue'; import { isBoolean, isFunction } from 'lodash-es'; var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => { __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); return value; }; class Fetch { constructor(service, options, initState = {}) { this.service = service; this.options = options; this.initState = initState; __publicField(this, "pluginImpls", []); __publicField(this, "count", 0); __publicField(this, "loading", ref(false)); __publicField(this, "params", ref([])); __publicField(this, "data", ref(void 0)); __publicField(this, "error", ref(void 0)); this.loading.value = isBoolean(toValue(initState.loading)) ? toValue(initState.loading || false) : !options.manual; this.params.value = toValue(initState.params) || []; } runPluginHandler(event, ...rest) { const r = this.pluginImpls.map((i) => { var _a; return (_a = i[event]) == null ? void 0 : _a.call(i, ...rest); }).filter(Boolean); return Object.assign({}, ...r); } async runAsync(...params) { var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j; this.count += 1; const currentCount = this.count; const { stopNow = false, returnNow = false, ...state } = this.runPluginHandler("onBefore", params); if (stopNow) { return new Promise(() => { }); } this.loading.value = isBoolean(toValue(state.loading)) ? toValue(state.loading) : true; this.params.value = params; this.error.value = state.error; if (state.data) { this.data.value = state.data; } if (returnNow) { return Promise.resolve(state.data); } (_b = (_a = this.options).onBefore) == null ? void 0 : _b.call(_a, params); try { let { servicePromise } = this.runPluginHandler("onRequest", this.service, params); if (!servicePromise) { servicePromise = this.service(...params); } const res = await servicePromise; if (currentCount !== this.count) { return new Promise(() => { }); } this.data.value = res; this.loading.value = false; this.error.value = void 0; (_d = (_c = this.options).onSuccess) == null ? void 0 : _d.call(_c, res, params); this.runPluginHandler("onSuccess", res, params); (_f = (_e = this.options).onFinally) == null ? void 0 : _f.call(_e, params, res, void 0); if (currentCount === this.count) { this.runPluginHandler("onFinally", params, res, void 0); } return res; } catch (error) { if (currentCount !== this.count) { return new Promise(() => { }); } this.loading.value = false; this.error.value = error; (_h = (_g = this.options).onError) == null ? void 0 : _h.call(_g, error, params); this.runPluginHandler("onError", error, params); (_j = (_i = this.options).onFinally) == null ? void 0 : _j.call(_i, params, void 0, error); if (currentCount === this.count) { this.runPluginHandler("onFinally", params, void 0, error); } throw error; } } run(...params) { this.runAsync(...params).catch((error) => { if (!this.options.onError) { console.error(error); } }); } cancel() { this.count += 1; this.loading.value = false; this.runPluginHandler("onCancel"); } refresh() { this.run(...toValue(this.params) || []); } refreshAsync() { return this.runAsync(...toValue(this.params) || []); } mutate(data) { const targetData = isFunction(data) ? data(toValue(this.data)) : data; this.runPluginHandler("onMutate", targetData); this.data.value = targetData; } } export { Fetch as default }; //# sourceMappingURL=Fetch.mjs.map