UNPKG

@antdv/pro-utils

Version:

@antdv/pro-utils

125 lines (124 loc) 4.07 kB
var __defProp = Object.defineProperty; var __defProps = Object.defineProperties; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropDescs = Object.getOwnPropertyDescriptors; var __getOwnPropNames = Object.getOwnPropertyNames; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var __async = (__this, __arguments, generator) => { return new Promise((resolve, reject) => { var fulfilled = (value) => { try { step(generator.next(value)); } catch (e) { reject(e); } }; var rejected = (value) => { try { step(generator.throw(value)); } catch (e) { reject(e); } }; var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); step((generator = generator.apply(__this, __arguments)).next()); }); }; var stdin_exports = {}; __export(stdin_exports, { useRequest: () => useRequest }); module.exports = __toCommonJS(stdin_exports); var import_vue = require("vue"); var import_utils = require("../../vueuse/utils"); var import_unitl = require("./unitl"); function noop() { } function useRequest(promise, initialState, options) { const { immediate = true, delay = 0, onError = noop, onSuccess = noop, resetOnExecute = true, shallow = true, throwError } = options != null ? options : {}; const state = shallow ? (0, import_vue.shallowRef)(initialState) : (0, import_vue.ref)(initialState); const isReady = (0, import_vue.ref)(false); const isLoading = (0, import_vue.ref)(false); const error = (0, import_vue.shallowRef)(void 0); function execute(delay2 = 0, ...args) { return __async(this, null, function* () { if (resetOnExecute) state.value = initialState; error.value = void 0; isReady.value = false; isLoading.value = true; if (delay2 > 0) yield (0, import_utils.promiseTimeout)(delay2); const _promise = typeof promise === "function" ? promise(...args) : promise; try { const data = yield _promise; state.value = data; isReady.value = true; onSuccess(data); } catch (e) { error.value = e; onError(e); if (throwError) throw e; } finally { isLoading.value = false; } return state.value; }); } if (immediate) execute(delay); const shell = { state, isReady, isLoading, error, execute }; function waitUntilIsLoaded() { return new Promise((resolve, reject) => { (0, import_unitl.until)(isLoading).toBe(false).then(() => resolve(shell)).catch(reject); }); } return __spreadProps(__spreadValues({}, shell), { then(onFulfilled, onRejected) { return waitUntilIsLoaded().then(onFulfilled, onRejected); } }); }