summaryrefslogtreecommitdiff
path: root/src/store
diff options
context:
space:
mode:
Diffstat (limited to 'src/store')
-rw-r--r--src/store/configs.ts13
1 files changed, 11 insertions, 2 deletions
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) {