Files
claude-code-haha/desktop/electron/services/previewLifecycle.ts
T
程序员阿江(Relakkes) 4c39fb0629 fix(desktop): reap terminal PTYs on committed navigation, not on started
Review caught a way the previous listener could kill a user's running shells with
no reload at all. installMainWindowNavigationGuards cancels external http(s)
navigation in `will-navigate` and hands it to the system browser, but Chromium
dispatches DidStartNavigation before the throttle that cancellation runs in — so
a blocked navigation still emits `did-start-navigation`. Dropping a URL anywhere
outside the composer (useComposerFileDrop only preventDefaults over the composer
panel with files attached) would have started a top-level navigation, had it
cancelled, and taken every `npm run dev` and its children with it while the app
stayed on screen.

`did-navigate` fires only once a main-frame navigation has committed, so a
cancelled one never reaches it, and Electron does not emit it for in-page
navigation — which also removes the need for the same-document predicate. The
reload paths this cleanup exists for all commit, so they still reap.

rendererNavigation.ts goes with the predicate: previewLifecycle.ts was its only
remaining caller, so it returns to the standalone form it had before.

Mutating the listener back to `did-start-navigation` reddens the cancelled
navigation case, which is what shows the test pins the commit boundary rather
than merely the existence of a listener.
2026-08-04 16:33:00 +08:00

43 lines
1.0 KiB
TypeScript

type NavigationDetails = {
isSameDocument?: boolean
isMainFrame?: boolean
}
export type PreviewCleanupWebContents = {
on(
event: 'did-start-navigation',
handler: (
details: NavigationDetails,
url?: string,
isInPlace?: boolean,
isMainFrame?: boolean,
) => void,
): unknown
}
function isMainFrameNavigation(
details: NavigationDetails,
deprecatedIsMainFrame?: boolean,
) {
return details.isMainFrame ?? deprecatedIsMainFrame === true
}
function isSameDocumentNavigation(
details: NavigationDetails,
deprecatedIsInPlace?: boolean,
) {
return details.isSameDocument ?? deprecatedIsInPlace === true
}
export function installPreviewCleanupOnRendererNavigation(
webContents: PreviewCleanupWebContents,
closePreview: () => void,
): void {
webContents.on('did-start-navigation', (details, _url, isInPlace, isMainFrame) => {
if (!isMainFrameNavigation(details, isMainFrame)) return
if (isSameDocumentNavigation(details, isInPlace)) return
closePreview()
})
}