UNPKG

vue-admin-core

Version:
121 lines (116 loc) 4.09 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var vue = require('vue'); var lodashEs = require('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", vue.ref(false)); __publicField(this, "params", vue.ref([])); __publicField(this, "data", vue.ref(void 0)); __publicField(this, "error", vue.ref(void 0)); this.loading.value = lodashEs.isBoolean(vue.toValue(initState.loading)) ? vue.toValue(initState.loading || false) : !options.manual; this.params.value = vue.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 = lodashEs.isBoolean(vue.toValue(state.loading)) ? vue.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(...vue.toValue(this.params) || []); } refreshAsync() { return this.runAsync(...vue.toValue(this.params) || []); } mutate(data) { const targetData = lodashEs.isFunction(data) ? data(vue.toValue(this.data)) : data; this.runPluginHandler("onMutate", targetData); this.data.value = targetData; } } exports.default = Fetch; //# sourceMappingURL=Fetch.js.map