Skip to content

Commit 8241311

Browse files
authored
Merge pull request #396 from netease-youdao/fix/mac-start
fix:修复应用启动时一直处于加载状态的问题
2 parents 3124d32 + 34d55ac commit 8241311

2 files changed

Lines changed: 40 additions & 7 deletions

File tree

src/renderer/App.tsx

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,28 @@ const App: React.FC = () => {
5151
const pendingPermission = pendingPermissions[0] ?? null;
5252
const isWindows = window.electron.platform === 'win32';
5353

54+
const waitWithTimeout = useCallback(
55+
async <T,>(promise: Promise<T>, timeoutMs: number, label: string): Promise<T> => {
56+
return await new Promise<T>((resolve, reject) => {
57+
const timer = window.setTimeout(() => {
58+
reject(new Error(`${label} timed out after ${timeoutMs}ms`));
59+
}, timeoutMs);
60+
61+
promise.then(
62+
(value) => {
63+
window.clearTimeout(timer);
64+
resolve(value);
65+
},
66+
(error) => {
67+
window.clearTimeout(timer);
68+
reject(error);
69+
}
70+
);
71+
});
72+
},
73+
[]
74+
);
75+
5476
// 初始化应用
5577
useEffect(() => {
5678
if (hasInitialized.current) {
@@ -60,18 +82,23 @@ const App: React.FC = () => {
6082

6183
const initializeApp = async () => {
6284
try {
85+
console.info('[App] initializeApp: start');
6386
// 标记平台,用于 CSS 条件样式(如 Windows 标题栏按钮区域留白)
6487
document.documentElement.classList.add(`platform-${window.electron.platform}`);
6588

6689
// 初始化配置
67-
await configService.init();
90+
console.info('[App] initializeApp: configService.init');
91+
await waitWithTimeout(configService.init(), 5000, 'configService.init');
6892

6993
// 初始化主题
94+
console.info('[App] initializeApp: themeService.initialize');
7095
themeService.initialize();
7196

7297
// 初始化语言
73-
await i18nService.initialize();
98+
console.info('[App] initializeApp: i18nService.initialize');
99+
await waitWithTimeout(i18nService.initialize(), 5000, 'i18nService.initialize');
74100

101+
console.info('[App] initializeApp: configService.getConfig');
75102
const config = await configService.getConfig();
76103

77104
const apiConfig: ApiConfig = {
@@ -112,20 +139,24 @@ const App: React.FC = () => {
112139
) ?? resolvedModels[0];
113140
dispatch(setSelectedModel(preferredModel));
114141
}
115-
116-
// 初始化定时任务服务
117-
await scheduledTaskService.init();
118142

119143
setIsInitialized(true);
144+
console.info('[App] initializeApp: shell ready');
145+
146+
// 初始化定时任务服务,但不阻塞首屏
147+
void waitWithTimeout(scheduledTaskService.init(), 5000, 'scheduledTaskService.init').catch((error) => {
148+
console.error('[App] initializeApp: scheduledTaskService.init failed:', error);
149+
});
150+
120151
} catch (error) {
121152
console.error('Failed to initialize app:', error);
122153
setInitError(i18nService.t('initializationError'));
123154
setIsInitialized(true);
124155
}
125156
};
126157

127-
initializeApp();
128-
}, []);
158+
void initializeApp();
159+
}, [dispatch, waitWithTimeout]);
129160

130161
useEffect(() => {
131162
const unsubscribe = i18nService.subscribe(() => {

src/renderer/services/scheduledTask.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ class ScheduledTaskService {
7272
}
7373
} catch (err: unknown) {
7474
store.dispatch(setError(err instanceof Error ? err.message : String(err)));
75+
} finally {
76+
store.dispatch(setLoading(false));
7577
}
7678
}
7779

0 commit comments

Comments
 (0)