UNPKG

@td-design/rn-hooks

Version:

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

120 lines (116 loc) 4.98 kB
'use strict'; var _tslib = require('../../_virtual/_tslib.js'); var react = require('react'); var cache = require('../utils/cache.js'); var cachePromise = require('../utils/cachePromise.js'); var cacheSubscribe = require('../utils/cacheSubscribe.js'); var index = require('../../useCreation/index.js'); var index$1 = require('../../useUnmount/index.js'); var useCachePlugin = function (fetchInstance, _a) { var cacheKey = _a.cacheKey, _b = _a.cacheTime, cacheTime = _b === void 0 ? 5 * 60 * 1000 : _b, _c = _a.staleTime, staleTime = _c === void 0 ? 0 : _c, customSetCache = _a.setCache, customGetCache = _a.getCache; var unSubscribeRef = react.useRef(); var currentPromiseRef = react.useRef(); var _setCache = function (key, cachedData) { if (customSetCache) { customSetCache(cachedData); } else { cache.setCache(key, cacheTime, cachedData); } cacheSubscribe.trigger(key, cachedData.data); }; var _getCache = function (key, params) { if (params === void 0) { params = []; } if (customGetCache) { return customGetCache(params); } return cache.getCache(key); }; index(function () { if (!cacheKey) return; // get data from cache when init var cacheData = _getCache(cacheKey); if (cacheData && Object.hasOwnProperty.call(cacheData, 'data')) { fetchInstance.state.data = cacheData.data; fetchInstance.state.params = cacheData.params; if (staleTime === -1 || new Date().getTime() - cacheData.time <= staleTime) { fetchInstance.state.loading = false; } } // subscribe same cachekey update, trigger update unSubscribeRef.current = cacheSubscribe.subscribe(cacheKey, function (data) { fetchInstance.setState({ data: data }); }); }, []); index$1(function () { var _a; (_a = unSubscribeRef.current) === null || _a === void 0 ? void 0 : _a.call(unSubscribeRef); }); if (!cacheKey) return {}; return { onBefore: function (params) { var cacheData = _getCache(cacheKey, params); if (!cacheData || !Object.hasOwnProperty.call(cacheData, 'data')) return {}; // If the data is fresh, stop request if (staleTime === -1 || new Date().getTime() - cacheData.time <= staleTime) { return { loading: false, data: cacheData === null || cacheData === void 0 ? void 0 : cacheData.data, error: undefined, returnNow: true, }; } // If the data is stale, return data, and request continue return { data: cacheData === null || cacheData === void 0 ? void 0 : cacheData.data, error: undefined, }; }, onRequest: function (service, args) { var servicePromise = cachePromise.getCachePromise(cacheKey); // If has servicePromise, and is not trigger by self, then use it if (servicePromise && servicePromise !== currentPromiseRef.current) return { servicePromise: servicePromise }; servicePromise = service.apply(void 0, _tslib.__spreadArray([], _tslib.__read(args), false)); currentPromiseRef.current = servicePromise; cachePromise.setCachePromise(cacheKey, servicePromise); return { servicePromise: servicePromise }; }, onSuccess: function (data, params) { var _a; if (cacheKey) { // cancel subscribe, avoid trgger self (_a = unSubscribeRef.current) === null || _a === void 0 ? void 0 : _a.call(unSubscribeRef); _setCache(cacheKey, { data: data, params: params, time: new Date().getTime(), }); // resubscribe unSubscribeRef.current = cacheSubscribe.subscribe(cacheKey, function (d) { fetchInstance.setState({ data: d }); }); } }, onMutate: function (data) { var _a; if (cacheKey) { // cancel subscribe, avoid trgger self (_a = unSubscribeRef.current) === null || _a === void 0 ? void 0 : _a.call(unSubscribeRef); _setCache(cacheKey, { data: data, params: fetchInstance.state.params, time: new Date().getTime(), }); // resubscribe unSubscribeRef.current = cacheSubscribe.subscribe(cacheKey, function (d) { fetchInstance.setState({ data: d }); }); } }, }; }; exports.useCachePlugin = useCachePlugin;