ahooks
Version:
react hooks library
189 lines (188 loc) • 7.74 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _tslib = require("tslib");
var _utils = require("../../utils");
var _cancelledError = require("./utils/cancelledError");
var Fetch = exports["default"] = /*#__PURE__*/function () {
function Fetch(serviceRef, options, subscribe) {
var initState = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
(0, _classCallCheck2["default"])(this, Fetch);
this.serviceRef = serviceRef;
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 = Object.assign(Object.assign(Object.assign({}, this.state), {
loading: !options.manual
}), initState);
}
return (0, _createClass2["default"])(Fetch, [{
key: "setState",
value: function setState() {
var s = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
this.state = Object.assign(Object.assign({}, this.state), s);
this.subscribe();
}
}, {
key: "runPluginHandler",
value: 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 _a2;
var _a;
return (_a = i[event]) === null || _a === void 0 ? void 0 : (_a2 = _a).call.apply(_a2, [i].concat(rest));
}).filter(Boolean);
return Object.assign.apply(Object, [{}].concat((0, _toConsumableArray2["default"])(r)));
}
}, {
key: "runAsync",
value: function runAsync() {
for (var _len2 = arguments.length, params = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
params[_key2] = arguments[_key2];
}
return (0, _tslib.__awaiter)(this, void 0, void 0, /*#__PURE__*/_regenerator["default"].mark(function _callee() {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, currentCount, _l, _l$stopNow, stopNow, _l$returnNow, returnNow, state, _this$runPluginHandle, servicePromise, _this$serviceRef, res, _t;
return _regenerator["default"].wrap(function (_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
this.count += 1;
currentCount = this.count;
_l = this.runPluginHandler('onBefore', params), _l$stopNow = _l.stopNow, stopNow = _l$stopNow === void 0 ? false : _l$stopNow, _l$returnNow = _l.returnNow, returnNow = _l$returnNow === void 0 ? false : _l$returnNow, state = (0, _tslib.__rest)(_l, ["stopNow", "returnNow"]); // stop request
if (!stopNow) {
_context.next = 1;
break;
}
return _context.abrupt("return", Promise.resolve(state.data));
case 1:
this.setState(Object.assign({
loading: true,
params: params
}, state));
// return now
if (!returnNow) {
_context.next = 2;
break;
}
return _context.abrupt("return", Promise.resolve(state.data));
case 2:
(_b = (_a = this.options).onBefore) === null || _b === void 0 ? void 0 : _b.call(_a, params);
_context.prev = 3;
// replace service
_this$runPluginHandle = this.runPluginHandler('onRequest', this.serviceRef.current, params), servicePromise = _this$runPluginHandle.servicePromise;
if (!servicePromise) {
servicePromise = (_this$serviceRef = this.serviceRef).current.apply(_this$serviceRef, params);
}
_context.next = 4;
return servicePromise;
case 4:
res = _context.sent;
if (!(currentCount !== this.count)) {
_context.next = 5;
break;
}
throw new _cancelledError.CancelledError();
case 5:
// const formattedResult = this.options.formatResultRef.current ? this.options.formatResultRef.current(res) : res;
this.setState({
data: res,
error: undefined,
loading: false
});
(_d = (_c = this.options).onSuccess) === null || _d === void 0 ? void 0 : _d.call(_c, res, params);
this.runPluginHandler('onSuccess', res, params);
(_f = (_e = this.options).onFinally) === null || _f === void 0 ? void 0 : _f.call(_e, params, res, undefined);
if (currentCount === this.count) {
this.runPluginHandler('onFinally', params, res, undefined);
}
return _context.abrupt("return", res);
case 6:
_context.prev = 6;
_t = _context["catch"](3);
if (!(currentCount !== this.count)) {
_context.next = 7;
break;
}
throw (0, _cancelledError.isCancelledError)(_t) ? _t : new _cancelledError.CancelledError();
case 7:
this.setState({
error: _t,
loading: false
});
(_h = (_g = this.options).onError) === null || _h === void 0 ? void 0 : _h.call(_g, _t, params);
this.runPluginHandler('onError', _t, params);
(_k = (_j = this.options).onFinally) === null || _k === void 0 ? void 0 : _k.call(_j, params, undefined, _t);
if (currentCount === this.count) {
this.runPluginHandler('onFinally', params, undefined, _t);
}
throw _t;
case 8:
case "end":
return _context.stop();
}
}, _callee, this, [[3, 6]]);
}));
}
}, {
key: "run",
value: function run() {
var _this = this;
this.runAsync.apply(this, arguments)["catch"](function (error) {
// cancellation is not a failure: `run` never reports it
if ((0, _cancelledError.isCancelledError)(error)) {
return;
}
if (!_this.options.onError) {
console.error(error);
}
});
}
}, {
key: "cancel",
value: function cancel() {
this.count += 1;
this.setState({
loading: false
});
this.runPluginHandler('onCancel');
}
}, {
key: "refresh",
value: function refresh() {
// @ts-ignore
this.run.apply(this, (0, _toConsumableArray2["default"])(this.state.params || []));
}
}, {
key: "refreshAsync",
value: function refreshAsync() {
// @ts-ignore
return this.runAsync.apply(this, (0, _toConsumableArray2["default"])(this.state.params || []));
}
}, {
key: "mutate",
value: function mutate(data) {
var targetData = (0, _utils.isFunction)(data) ? data(this.state.data) : data;
this.runPluginHandler('onMutate', targetData);
this.setState({
data: targetData
});
}
}]);
}();