diff options
| author | Larvan2 <[email protected]> | 2026-06-04 21:25:02 +0800 |
|---|---|---|
| committer | Larvan2 <[email protected]> | 2026-06-04 21:25:02 +0800 |
| commit | a8f8bb1d6b9925425b5b1121de0ea1c0de1645ed (patch) | |
| tree | 0ff036d5c79f4315f715231c78f9557419c83bc8 /src | |
| parent | cc6efa25afc9853771565a5f68d1cc24b3a945b2 (diff) | |
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) { |
