UNPKG

@td-design/rn-hooks

Version:

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

48 lines (44 loc) 1.5 kB
'use strict'; var react = require('react'); var useRetryPlugin = function (fetchInstance, _a) { var retryInterval = _a.retryInterval, retryCount = _a.retryCount; var timerRef = react.useRef(); var countRef = react.useRef(0); var triggerByRetry = react.useRef(false); if (!retryCount) return {}; return { onBefore: function () { if (!triggerByRetry.current) { countRef.current = 0; } triggerByRetry.current = false; if (timerRef.current) { clearTimeout(timerRef.current); } }, onSuccess: function () { countRef.current = 0; }, onError: function () { countRef.current += 1; if (retryCount === -1 || countRef.current <= retryCount) { var timeout = retryInterval !== null && retryInterval !== void 0 ? retryInterval : Math.min(1000 * Math.pow(2, countRef.current), 30000); timerRef.current = setTimeout(function () { triggerByRetry.current = true; fetchInstance.refresh(); }, timeout); } else { countRef.current = 0; } }, onCancel: function () { countRef.current = 0; if (timerRef.current) { clearTimeout(timerRef.current); } }, }; }; exports.useRetryPlugin = useRetryPlugin;