UNPKG

@taro-hooks/use-request

Version:
98 lines 4.32 kB
var _excluded = ["manual"]; 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); } 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; } import { useLatest, useMount, useUnmount, useUpdate, useMemoizedFn } from '@taro-hooks/ahooks'; import { useMemo } from '@taro-hooks/core'; import { escapeState, FRAMEWORK } from '@taro-hooks/shared'; import Fetch from './Fetch'; function useRequestImplement(service, options, plugins) { if (options === void 0) { options = {}; } if (plugins === void 0) { plugins = []; } var _options = options, _options$manual = _options.manual, manual = _options$manual === void 0 ? false : _options$manual, rest = _objectWithoutPropertiesLoose(_options, _excluded); var fetchOptions = _extends({ manual: manual }, rest); var serviceRef = useLatest(service); var update = useUpdate(); var fetchInstance = useMemo(function () { var initState = plugins.map(function (p) { return p == null ? void 0 : p.onInit == null ? void 0 : p.onInit(fetchOptions); }).filter(Boolean); var fetch = new Fetch( // @ts-ignore serviceRef, fetchOptions, update, Object.assign.apply(Object, [{}].concat(initState))); return fetch; }, []); // react can not call hooks in other hooks. we use env to fix it if (FRAMEWORK === 'react') { fetchInstance.options = fetchOptions; // run all plugins hooks fetchInstance.pluginImpls = plugins.map(function (p) { return p(fetchInstance, fetchOptions); }); } else if (FRAMEWORK === 'vue') { // TODO: maybe find a better way to hold reactive // @ts-ignore fetchInstance.value.options = fetchOptions; // run all plugins hooks // @ts-ignore fetchInstance.value.pluginImpls = plugins.map(function (p) { return ( // @ts-ignore p(fetchInstance.value, fetchOptions) ); }); } useMount(function () { if (!manual) { // useCachePlugin can set fetchInstance.state.params from cache when init var instance = escapeState(fetchInstance); var params = instance.state.params || options.defaultParams || []; // @ts-ignore instance.run.apply(instance, params); } }); useUnmount(function () { var _escapeState; (_escapeState = escapeState(fetchInstance)) == null ? void 0 : _escapeState.cancel == null ? void 0 : _escapeState.cancel(); }); // due to vue reactive, need reduce single for useMemo // ugly var vueRefResult = useMemo(function () { return { loading: escapeState(fetchInstance).state.loading, data: escapeState(fetchInstance).state.data, error: escapeState(fetchInstance).state.error, params: escapeState(fetchInstance).state.params || [], cancel: escapeState(fetchInstance).cancel.bind(escapeState(fetchInstance)), refresh: escapeState(fetchInstance).refresh.bind(escapeState(fetchInstance)), refreshAsync: escapeState(fetchInstance).refreshAsync.bind(escapeState(fetchInstance)), run: escapeState(fetchInstance).run.bind(escapeState(fetchInstance)), runAsync: escapeState(fetchInstance).runAsync.bind(escapeState(fetchInstance)), mutate: escapeState(fetchInstance).mutate.bind(escapeState(fetchInstance)) }; }, [fetchInstance]); if (FRAMEWORK === 'vue') { return vueRefResult; } return { loading: fetchInstance.state.loading, data: fetchInstance.state.data, error: fetchInstance.state.error, params: fetchInstance.state.params || [], cancel: useMemoizedFn(fetchInstance.cancel.bind(fetchInstance)), refresh: useMemoizedFn(fetchInstance.refresh.bind(fetchInstance)), refreshAsync: useMemoizedFn(fetchInstance.refreshAsync.bind(fetchInstance)), run: useMemoizedFn(fetchInstance.run.bind(fetchInstance)), runAsync: useMemoizedFn(fetchInstance.runAsync.bind(fetchInstance)), mutate: useMemoizedFn(fetchInstance.mutate.bind(fetchInstance)) }; } export default useRequestImplement;