fix: fetch pool retry

This commit is contained in:
Gabe Yuan
2024-04-20 11:52:16 +08:00
parent a172234fb0
commit a5cfb0ca1d

View File

@@ -98,26 +98,18 @@ export const fetchApi = async ({ input, init, transOpts, apiSetting }) => {
return fetch(input, init); return fetch(input, init);
}; };
/**
* 请求池实例
*/
export const fetchPool = taskPool(
fetchApi,
null,
DEFAULT_FETCH_INTERVAL,
DEFAULT_FETCH_LIMIT
);
/** /**
* 请求数据统一接口 * 请求数据统一接口
* @param {*} input * @param {*} param0
* @param {*} opts
* @returns * @returns
*/ */
export const fetchData = async ( export const fetchHandle = async ({
input, input,
{ useCache, usePool, transOpts, apiSetting, ...init } = {} useCache,
) => { transOpts,
apiSetting,
...init
}) => {
const cacheReq = await newCacheReq(input, init); const cacheReq = await newCacheReq(input, init);
let res; let res;
@@ -133,12 +125,7 @@ export const fetchData = async (
if (!res) { if (!res) {
// 发送请求 // 发送请求
if (usePool) { res = await fetchApi({ input, init, transOpts, apiSetting });
res = await fetchPool.push({ input, init, transOpts, apiSetting });
} else {
res = await fetchApi({ input, init, transOpts, apiSetting });
}
if (!res) { if (!res) {
throw new Error("Unknow error"); throw new Error("Unknow error");
} else if (!res.ok) { } else if (!res.ok) {
@@ -173,6 +160,29 @@ export const fetchData = async (
return await res.text(); return await res.text();
}; };
/**
* 请求池实例
*/
export const fetchPool = taskPool(
fetchHandle,
null,
DEFAULT_FETCH_INTERVAL,
DEFAULT_FETCH_LIMIT
);
/**
* 请求池分发
* @param {*} input
* @param {*} param1
* @returns
*/
export const fetchData = (input, { usePool, ...opts } = {}) => {
if (usePool) {
return fetchPool.push({ input, ...opts });
}
return fetchHandle({ input, ...opts });
};
/** /**
* fetch 兼容性封装 * fetch 兼容性封装
* @param {*} input * @param {*} input