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
28 changes: 26 additions & 2 deletions src/web-ui/src/features/dispatch/DispatchJobObserver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ describe('DispatchJobObserver', () => {
cleanup();
});

it('continues polling while hidden so background notifications can observe changes', async () => {
it('pauses polling while hidden to reduce background CPU usage', async () => {
registerRunningJob();
mocks.status.mockResolvedValue(status({ state: 'running' }));
Object.defineProperty(document, 'visibilityState', {
Expand All @@ -855,7 +855,7 @@ describe('DispatchJobObserver', () => {
const cleanup = installDispatchJobObserver(createContext());

await vi.advanceTimersByTimeAsync(0);
expect(mocks.status).toHaveBeenCalledTimes(1);
expect(mocks.status).not.toHaveBeenCalled();

cleanup();
Object.defineProperty(document, 'visibilityState', {
Expand All @@ -864,6 +864,30 @@ describe('DispatchJobObserver', () => {
});
});

it('resumes polling when the window becomes visible again', async () => {
registerRunningJob();
mocks.status.mockResolvedValue(status({ state: 'running' }));
Object.defineProperty(document, 'visibilityState', {
configurable: true,
value: 'hidden',
});
const cleanup = installDispatchJobObserver(createContext());

await vi.advanceTimersByTimeAsync(0);
expect(mocks.status).not.toHaveBeenCalled();

Object.defineProperty(document, 'visibilityState', {
configurable: true,
value: 'visible',
});
document.dispatchEvent(new Event('visibilitychange'));

await vi.advanceTimersByTimeAsync(0);
expect(mocks.status).toHaveBeenCalled();

cleanup();
});

it('does not present target cancellation as a successful completion', async () => {
registerRunningJob();
const context = createContext();
Expand Down
14 changes: 13 additions & 1 deletion src/web-ui/src/features/dispatch/DispatchJobObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,7 @@ export function installDispatchJobObserver(context: FlowChatContext): () => void

async function run(requestedJobId?: string): Promise<void> {
if (!ownsLease() || isPeerDeviceModeActive()) return;
if (typeof document !== 'undefined' && document.visibilityState === 'hidden') return;
if (inFlight) {
queuedJobId = requestedJobId;
return;
Expand Down Expand Up @@ -1095,8 +1096,19 @@ export function installDispatchJobObserver(context: FlowChatContext): () => void
void run();
}, DISPATCH_JOB_POLL_INTERVAL_MS);
handleVisibilityChanged = () => {
if (typeof document === 'undefined' || document.visibilityState === 'visible') {
if (typeof document === 'undefined') return;
if (document.visibilityState === 'visible') {
if (interval === null) {
interval = setInterval(() => {
void run();
}, DISPATCH_JOB_POLL_INTERVAL_MS);
}
schedule();
} else {
if (interval !== null) {
clearInterval(interval);
interval = null;
}
}
};
if (typeof document !== 'undefined') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ export const BackgroundCommandOutputPanel: React.FC<BackgroundCommandOutputPanel
if (metadata?.status && metadata.status !== 'running') {
return;
}
if (typeof document !== 'undefined' && document.visibilityState === 'hidden') return;
void readOutput(false);
}, isPeerDeviceModeActive()
? PEER_MODE_BACKGROUND_COMMAND_POLL_MS
Expand Down
1 change: 1 addition & 0 deletions src/web-ui/src/tools/editor/components/CodeEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1960,6 +1960,7 @@ const CodeEditor: React.FC<CodeEditorProps> = ({
}

const tick = () => {
if (typeof document !== 'undefined' && document.visibilityState === 'hidden') return;
void checkFileModification();
};
const pollOffsetMs = getPollOffsetMs(filePath);
Expand Down
1 change: 1 addition & 0 deletions src/web-ui/src/tools/editor/components/MarkdownEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ const MarkdownEditor: React.FC<MarkdownEditorProps> = ({
return;
}
const tick = () => {
if (typeof document !== 'undefined' && document.visibilityState === 'hidden') return;
void checkMarkdownDisk();
};
const pollOffsetMs = getPollOffsetMs(filePath);
Expand Down