UNPKG

@td-design/rn-hooks

Version:

从ahooks里面抽取的一些在rn项目里可以使用的hooks。提高开发效率

143 lines (139 loc) 6.7 kB
'use strict'; var _tslib = require('../_virtual/_tslib.js'); var index = require('../utils/index.js'); var Fetch = /** @class */ (function () { function Fetch(service, options, subscribe, initState) { if (initState === void 0) { initState = {}; } this.service = service; this.options = options; this.subscribe = subscribe; this.initState = initState; this.pluginImpls = []; this.count = 0; this.state = { loading: false, params: undefined, data: undefined, error: undefined, }; this.state = _tslib.__assign(_tslib.__assign(_tslib.__assign({}, this.state), { loading: !options.manual }), initState); } Fetch.prototype.setState = function (s) { if (s === void 0) { s = {}; } this.state = _tslib.__assign(_tslib.__assign({}, this.state), s); this.subscribe(); }; Fetch.prototype.runPluginHandler = function (event) { var rest = []; for (var _i = 1; _i < arguments.length; _i++) { rest[_i - 1] = arguments[_i]; } // @ts-ignore var result = this.pluginImpls.map(function (impl) { var _a; return (_a = impl[event]) === null || _a === void 0 ? void 0 : _a.call.apply(_a, _tslib.__spreadArray([impl], _tslib.__read(rest), false)); }).filter(Boolean); return Object.assign.apply(Object, _tslib.__spreadArray([{}], _tslib.__read(result), false)); }; Fetch.prototype.runAsync = function () { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k; var params = []; for (var _i = 0; _i < arguments.length; _i++) { params[_i] = arguments[_i]; } return _tslib.__awaiter(this, void 0, void 0, function () { var currentCount, _l, _m, stopNow, _o, returnNow, state, servicePromise, result, e_1, error; var _p; return _tslib.__generator(this, function (_q) { switch (_q.label) { case 0: this.count += 1; currentCount = this.count; _l = this.runPluginHandler('onBefore', params), _m = _l.stopNow, stopNow = _m === void 0 ? false : _m, _o = _l.returnNow, returnNow = _o === void 0 ? false : _o, state = _tslib.__rest(_l, ["stopNow", "returnNow"]); if (stopNow) { return [2 /*return*/, new Promise(function () { })]; } this.setState(_tslib.__assign({ loading: true, params: params }, state)); if (returnNow) { return [2 /*return*/, Promise.resolve(state.data)]; } (_b = (_a = this.options).onBefore) === null || _b === void 0 ? void 0 : _b.call(_a, params); _q.label = 1; case 1: _q.trys.push([1, 3, , 4]); servicePromise = this.runPluginHandler('onRequest', this.service.current, params).servicePromise; if (!servicePromise) { servicePromise = (_p = this.service).current.apply(_p, _tslib.__spreadArray([], _tslib.__read(params), false)); } return [4 /*yield*/, servicePromise]; case 2: result = _q.sent(); if (currentCount !== this.count) { return [2 /*return*/, new Promise(function () { })]; } this.setState({ loading: false, data: result, error: undefined, }); (_d = (_c = this.options).onSuccess) === null || _d === void 0 ? void 0 : _d.call(_c, result, params); this.runPluginHandler('onSuccess', result, params); (_f = (_e = this.options).onFinally) === null || _f === void 0 ? void 0 : _f.call(_e, params, result, undefined); if (currentCount === this.count) { this.runPluginHandler('onFinally', params, result, undefined); } return [2 /*return*/, result]; case 3: e_1 = _q.sent(); if (currentCount !== this.count) { return [2 /*return*/, new Promise(function () { })]; } error = e_1; this.setState({ loading: false, error: error, }); (_h = (_g = this.options).onError) === null || _h === void 0 ? void 0 : _h.call(_g, error, params); this.runPluginHandler('onError', error, params); (_k = (_j = this.options).onFinally) === null || _k === void 0 ? void 0 : _k.call(_j, params, undefined, error); if (currentCount === this.count) { this.runPluginHandler('onFinally', params, undefined, error); } throw error; case 4: return [2 /*return*/]; } }); }); }; Fetch.prototype.run = function () { var _this = this; var params = []; for (var _i = 0; _i < arguments.length; _i++) { params[_i] = arguments[_i]; } this.runAsync.apply(this, _tslib.__spreadArray([], _tslib.__read(params), false)).catch(function (error) { if (!_this.options.onError) { console.error(error); } }); }; Fetch.prototype.cancel = function () { this.count += 1; this.setState({ loading: false, }); this.runPluginHandler('onCancel'); }; Fetch.prototype.refresh = function () { this.run.apply(this, _tslib.__spreadArray([], _tslib.__read((this.state.params || [])), false)); }; Fetch.prototype.refreshAsync = function () { return this.runAsync.apply(this, _tslib.__spreadArray([], _tslib.__read((this.state.params || [])), false)); }; Fetch.prototype.mutate = function (data) { var targetData = index.isFunction(data) ? data(this.state.data) : data; this.runPluginHandler('onMutate', targetData); this.setState({ data: targetData, }); }; return Fetch; }()); module.exports = Fetch;