@td-design/rn-hooks
Version:
从ahooks里面抽取的一些在rn项目里可以使用的hooks。提高开发效率
56 lines (52 loc) • 2.65 kB
JavaScript
;
var _tslib = require('../_virtual/_tslib.js');
var index$2 = require('../useCreation/index.js');
var index = require('../useLatest/index.js');
var index$5 = require('../useMemoizedFn/index.js');
var index$3 = require('../useMount/index.js');
var index$4 = require('../useUnmount/index.js');
var index$1 = require('../useUpdate/index.js');
var Fetch = require('./Fetch.js');
function useRequestImpl(service, options, plugins) {
if (options === void 0) { options = {}; }
if (plugins === void 0) { plugins = []; }
var _a = options.manual, manual = _a === void 0 ? false : _a, rest = _tslib.__rest(options, ["manual"]);
if (__DEV__) {
if (options.defaultParams && !Array.isArray(options.defaultParams)) {
console.warn("expected defaultParams is array, got ".concat(typeof options.defaultParams));
}
}
var fetchOptions = _tslib.__assign({ manual: manual }, rest);
var serviceRef = index(service);
var forceUpdate = index$1();
var fetchInstance = index$2(function () {
var initState = plugins.map(function (p) { var _a; return (_a = p === null || p === void 0 ? void 0 : p.onInit) === null || _a === void 0 ? void 0 : _a.call(p, fetchOptions); }).filter(Boolean);
return new Fetch(serviceRef, fetchOptions, forceUpdate, Object.assign.apply(Object, _tslib.__spreadArray([{}], _tslib.__read(initState), false)));
}, []);
fetchInstance.options = fetchOptions;
fetchInstance.pluginImpls = plugins.map(function (plugin) { return plugin(fetchInstance, fetchOptions); });
index$3(function () {
if (!manual) {
// useCachePlugin can set fetchInstance.state.params from cache when init
var params = fetchInstance.state.params || options.defaultParams || [];
// @ts-ignore
fetchInstance.run.apply(fetchInstance, _tslib.__spreadArray([], _tslib.__read(params), false));
}
});
index$4(function () {
fetchInstance.cancel();
});
return {
loading: fetchInstance.state.loading,
data: fetchInstance.state.data,
error: fetchInstance.state.error,
params: fetchInstance.state.params || [],
cancel: index$5(fetchInstance.cancel.bind(fetchInstance)),
refresh: index$5(fetchInstance.refresh.bind(fetchInstance)),
refreshAsync: index$5(fetchInstance.refreshAsync.bind(fetchInstance)),
run: index$5(fetchInstance.run.bind(fetchInstance)),
runAsync: index$5(fetchInstance.runAsync.bind(fetchInstance)),
mutate: index$5(fetchInstance.mutate.bind(fetchInstance)),
};
}
module.exports = useRequestImpl;