Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 34 additions & 19 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,26 @@ import {

const log = loggers.app;

/**
* 分发自动执行任务(autostart / autoRunOnLaunch),启动成功时记录本次进程已执行过,
* 以便更新重启后跳过重复分发
*/
function dispatchAutoStartTasks(onSettled?: (started: boolean) => void) {
document.dispatchEvent(
new CustomEvent('mxu-start-tasks', {
detail: {
source: 'autostart',
onSettled: (started: boolean) => {
if (started) {
useAppStore.getState().setAutoTasksRanInSession(true);
}
onSettled?.(started);
},
},
}),
);
}

type LoadingState = 'loading' | 'success' | 'error';

// 页面过渡动画时长(ms)
Expand Down Expand Up @@ -1069,25 +1089,24 @@ function App() {
autoStartTasksPending = false;
autoTaskLaunchPendingRef.current = true;
setTimeout(() => {
document.dispatchEvent(
new CustomEvent('mxu-start-tasks', {
detail: {
source: 'autostart',
onSettled: () => {
autoTaskLaunchPendingRef.current = false;
// 启动失败时任务不会产生 running -> idle 状态变化,需要在这里补一次安装检查;
// 启动成功时仍处于运行态,本次检查会直接返回,并在任务结束后再次触发。
tryAutoInstallUpdate();
},
},
}),
);
dispatchAutoStartTasks(() => {
autoTaskLaunchPendingRef.current = false;
// 启动失败时任务不会产生 running -> idle 状态变化,需要在这里补一次安装检查;
// 启动成功时仍处于运行态,本次检查会直接返回,并在任务结束后再次触发。
tryAutoInstallUpdate();
});
}, 500);
};

// 检查是否刚更新完成(重启后)
const isAutoStartModeNow = useAppStore.getState().isAutoStartMode;
const updateCompleteInfo = consumeUpdateCompleteInfo();
if (updateCompleteInfo?.autoTasksAlreadyRan && autoStartTasksPending) {
Comment thread
zmdyy0318 marked this conversation as resolved.
Resolved
// 更新前本次进程已执行过自动任务(任务结束后才自动安装并重启),
// 重启沿用原命令行参数会再次进入自动执行,这里跳过以避免重复执行
log.info('更新重启:更新前已执行过自动任务,跳过本次自动执行');
autoStartTasksPending = false;
}
if (updateCompleteInfo) {
const currentVersionNow = result.interface.version || '';

Expand Down Expand Up @@ -1603,9 +1622,7 @@ function App() {
pendingAutoTasksRef.current = false;
log.info('下载失败或取消,分发挂起的自动任务');
setTimeout(() => {
document.dispatchEvent(
new CustomEvent('mxu-start-tasks', { detail: { source: 'autostart' } }),
);
dispatchAutoStartTasks();
}, 500);
}
}, [downloadStatus]);
Expand All @@ -1616,9 +1633,7 @@ function App() {
pendingAutoTasksRef.current = false;
log.info('弹窗关闭,分发挂起的自动任务');
setTimeout(() => {
document.dispatchEvent(
new CustomEvent('mxu-start-tasks', { detail: { source: 'autostart' } }),
);
dispatchAutoStartTasks();
}, 500);
}
}, [showInstallConfirmModal]);
Expand Down
1 change: 1 addition & 0 deletions src/components/InstallConfirmModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export function InstallConfirmModal() {
releaseNote: updateInfo.releaseNote,
channel: updateInfo.channel,
timestamp: Date.now(),
autoTasksAlreadyRan: useAppStore.getState().autoTasksRanInSession,
Comment thread
zmdyy0318 marked this conversation as resolved.
Resolved
Comment thread
zmdyy0318 marked this conversation as resolved.
Resolved
Comment thread
zmdyy0318 marked this conversation as resolved.
Resolved
});
}
await restartApp();
Expand Down
6 changes: 6 additions & 0 deletions src/services/updateService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1280,6 +1280,12 @@ export interface UpdateCompleteInfo {
* - false/undefined: 直接显示更新完成弹窗
*/
requireVersionCheck?: boolean;
/**
* 安装前本次进程是否已成功启动过自动执行任务(autostart / autoRunOnLaunch)
* - true: 重启后不再分发自动任务,避免同一批任务重复执行
* - false/undefined: 重启后按正常流程分发
*/
autoTasksAlreadyRan?: boolean;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/stores/appStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2115,6 +2115,10 @@ export const useAppStore = create<AppState>()(
isAutoStartMode: false,
setIsAutoStartMode: (mode) => set({ isAutoStartMode: mode }),

// 本次进程内自动执行的任务是否已成功启动过
autoTasksRanInSession: false,
setAutoTasksRanInSession: (ran) => set({ autoTasksRanInSession: ran }),

// 启动后自动执行的实例 ID
autoStartInstanceId: undefined,
setAutoStartInstanceId: (id) =>
Expand Down
5 changes: 5 additions & 0 deletions src/stores/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,11 @@ export interface AppState {
isAutoStartMode: boolean;
setIsAutoStartMode: (mode: boolean) => void;

// 本次进程内自动执行(autostart / autoRunOnLaunch)的任务是否已成功启动过
// 用于更新重启后避免重复执行同一批自动任务
autoTasksRanInSession: boolean;
setAutoTasksRanInSession: (ran: boolean) => void;

// 启动后自动执行的实例 ID
autoStartInstanceId: string | undefined;
setAutoStartInstanceId: (id: string | undefined) => void;
Expand Down
Loading