diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/api/configs.ts | 4 | ||||
| -rw-r--r-- | src/store/configs.ts | 13 |
2 files changed, 13 insertions, 4 deletions
diff --git a/src/api/configs.ts b/src/api/configs.ts index 3995aa7..63a88c7 100644 --- a/src/api/configs.ts +++ b/src/api/configs.ts @@ -9,9 +9,9 @@ const upgradeCoreEndpoint = '/upgrade'; const upgradeGeoEndpoint = '/upgrade/geo'; const upgradeUIEndpoint = '/upgrade/ui'; -export async function fetchConfigs(apiConfig: ClashAPIConfig) { +export async function fetchConfigs(apiConfig: ClashAPIConfig, signal?: AbortSignal) { const { url, init } = getURLAndInit(apiConfig); - return await fetch(url + endpoint, init); + return await fetch(url + endpoint, { ...init, signal }); } // TODO support PUT /configs diff --git a/src/store/configs.ts b/src/store/configs.ts index df26517..b84471b 100644 --- a/src/store/configs.ts +++ b/src/store/configs.ts @@ -17,15 +17,24 @@ export const getConfigs = (s: State) => s.configs.configs; export const getHaveFetched = (s: State) => s.configs.haveFetchedConfig; export const getLogLevel = (s: State) => s.configs.configs['log-level']; +const STARTUP_TIMEOUT_MS = 2000; + export function fetchConfigs(apiConfig: ClashAPIConfig) { return async (dispatch: DispatchFn, getState: GetStateFn) => { let res: Response; + const haveFetched = getHaveFetched(getState()); + const controller = new AbortController(); + const timeoutId = haveFetched + ? null + : setTimeout(() => controller.abort(), STARTUP_TIMEOUT_MS); try { - res = await configsAPI.fetchConfigs(apiConfig); + res = await configsAPI.fetchConfigs(apiConfig, haveFetched ? undefined : controller.signal); } catch (err) { - // TypeError and AbortError + // TypeError and AbortError (includes timeout) dispatch(openModal('apiConfig')); return; + } finally { + if (timeoutId !== null) clearTimeout(timeoutId); } if (!res.ok) { |
