fix(web): stop polling plugin tasks when no active installs

The PluginInstallTaskProvider was unconditionally polling
getAsyncTasks every 3s on all /home/* routes. Now it only
syncs once on mount and starts periodic polling only when
there are active (non-terminal) install tasks.
This commit is contained in:
Junyan Qin
2026-04-17 21:02:14 +08:00
committed by WangCham
parent f6e7983890
commit 94da5bf05d

View File

@@ -401,14 +401,32 @@ export function PluginInstallTaskProvider({
}
}, [pollTask]);
// Initial sync on mount + periodic sync every 3s
// Initial sync on mount to recover any orphaned tasks
const syncOnMountRef = useRef(syncTasksFromBackend);
syncOnMountRef.current = syncTasksFromBackend;
useEffect(() => {
syncTasksFromBackend();
syncIntervalRef.current = setInterval(syncTasksFromBackend, 3000);
syncOnMountRef.current();
}, []);
// Only poll periodically when there are active (non-terminal) tasks
useEffect(() => {
const hasActiveTasks = tasks.some(
(t) => t.stage !== InstallStage.DONE && t.stage !== InstallStage.ERROR,
);
if (hasActiveTasks) {
syncIntervalRef.current = setInterval(syncTasksFromBackend, 3000);
} else {
if (syncIntervalRef.current) {
clearInterval(syncIntervalRef.current);
syncIntervalRef.current = null;
}
}
return () => {
if (syncIntervalRef.current) clearInterval(syncIntervalRef.current);
};
}, [syncTasksFromBackend]);
}, [tasks, syncTasksFromBackend]);
const addTask = useCallback(
(params: {