UNPKG

@taro-hooks/use-request

Version:
187 lines (186 loc) 6.56 kB
"use strict"; var _excluded = ["stopNow", "returnNow"]; function _await(value, then, direct) { if (direct) { return then ? then(value) : value; } if (!value || !value.then) { value = Promise.resolve(value); } return then ? value.then(then) : value; } function _catch(body, recover) { try { var result = body(); } catch (e) { return recover(e); } if (result && result.then) { return result.then(void 0, recover); } return result; } function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; } function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } Object.defineProperty(exports, "__esModule", { value: true }); var shared_1 = require("@taro-hooks/shared"); var Fetch = /*#__PURE__*/function () { function Fetch(serviceRef, options, subscribe, initState) { if (initState === void 0) { initState = {}; } this.serviceRef = void 0; this.options = void 0; this.subscribe = void 0; this.initState = void 0; this.pluginImpls = void 0; this.count = 0; this.state = { loading: false, params: undefined, data: undefined, error: undefined }; this.serviceRef = serviceRef; this.options = options; this.subscribe = subscribe; this.initState = initState; this.state = _extends({}, this.state, { loading: !options.manual }, initState); } var _proto = Fetch.prototype; _proto.setState = function setState(s) { if (s === void 0) { s = {}; } this.state = _extends({}, this.state, s); this.subscribe(); }; _proto.runPluginHandler = function runPluginHandler(event) { for (var _len = arguments.length, rest = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { rest[_key - 1] = arguments[_key]; } // @ts-ignore var r = this.pluginImpls.map(function (i) { var _i$event; return (_i$event = i[event]) == null ? void 0 : _i$event.call.apply(_i$event, [i].concat(rest)); }).filter(Boolean); return Object.assign.apply(Object, [{}].concat(r)); }; _proto.runAsync = function runAsync() { for (var _len2 = arguments.length, params = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { params[_key2] = arguments[_key2]; } try { var _this = this; _this.count += 1; var currentCount = _this.count; var _this$runPluginHandle = _this.runPluginHandler('onBefore', params), _this$runPluginHandle2 = _this$runPluginHandle.stopNow, stopNow = _this$runPluginHandle2 === void 0 ? false : _this$runPluginHandle2, _this$runPluginHandle3 = _this$runPluginHandle.returnNow, returnNow = _this$runPluginHandle3 === void 0 ? false : _this$runPluginHandle3, state = _objectWithoutPropertiesLoose(_this$runPluginHandle, _excluded); // stop request if (stopNow) { return _await(new Promise(function () {})); } _this.setState(_extends({ loading: true, params: params }, state)); // return now if (returnNow) { return Promise.resolve(state.data); } _this.options.onBefore == null ? void 0 : _this.options.onBefore(params); return _await(_catch(function () { // replace service var _this$runPluginHandle4 = _this.runPluginHandler('onRequest', _this.serviceRef.current, params), servicePromise = _this$runPluginHandle4.servicePromise; if (!servicePromise) { var _this$serviceRef; servicePromise = (_this$serviceRef = _this.serviceRef).current.apply(_this$serviceRef, params); } return _await(servicePromise, function (res) { if (currentCount !== _this.count) { // prevent run.then when request is canceled return new Promise(function () {}); } // const formattedResult = this.options.formatResultRef.current ? this.options.formatResultRef.current(res) : res; _this.setState({ data: res, error: undefined, loading: false }); _this.options.onSuccess == null ? void 0 : _this.options.onSuccess(res, params); _this.runPluginHandler('onSuccess', res, params); _this.options.onFinally == null ? void 0 : _this.options.onFinally(params, res, undefined); if (currentCount === _this.count) { _this.runPluginHandler('onFinally', params, res, undefined); } return res; }); }, function (error) { if (currentCount !== _this.count) { // prevent run.then when request is canceled return new Promise(function () {}); } _this.setState({ error: error, loading: false }); _this.options.onError == null ? void 0 : _this.options.onError(error, params); _this.runPluginHandler('onError', error, params); _this.options.onFinally == null ? void 0 : _this.options.onFinally(params, undefined, error); if (currentCount === _this.count) { _this.runPluginHandler('onFinally', params, undefined, error); } throw error; })); } catch (e) { return Promise.reject(e); } }; _proto.run = function run() { var _this2 = this; this.runAsync.apply(this, arguments)["catch"](function (error) { if (!_this2.options.onError) { console.error(error); } }); }; _proto.cancel = function cancel() { this.count += 1; this.setState({ loading: false }); this.runPluginHandler('onCancel'); }; _proto.refresh = function refresh() { // @ts-ignore this.run.apply(this, this.state.params || []); }; _proto.refreshAsync = function refreshAsync() { // @ts-ignore return this.runAsync.apply(this, this.state.params || []); }; _proto.mutate = function mutate(data) { var targetData; if ((0, shared_1.isFunction)(data)) { // @ts-ignore targetData = data(this.state.data); } else { targetData = data; } this.runPluginHandler('onMutate', targetData); this.setState({ data: targetData }); }; return Fetch; }(); exports["default"] = Fetch;