fix(ext): handle stopped lifecycle state

This commit is contained in:
Simon
2026-06-11 19:53:30 +08:00
parent e270ba15b5
commit edb769b826
4 changed files with 21 additions and 15 deletions
+18 -12
View File
@@ -39,6 +39,18 @@ export class MultiPageAgent extends PageAgentCore {
const includeInitialTab = config.includeInitialTab ?? true
const experimentalIncludeAllTabs = config.experimentalIncludeAllTabs ?? false
/**
* Project agent status into chrome.storage. The content script polls
* `isAgentRunning` + `agentHeartbeat` (eventually consistent by design).
*
* When the agent is in side-panel and user closed the side-panel.
* There is no chance for isAgentRunning to be set false.
* (unload event doesn't work well in side panel.)
* (I'm trying not to use long-lived connection because the lifecycle of a sw is hard to predict.)
* This heartbeat mechanism acts as a backup.
*/
let heartBeatInterval: number | null = null
super({
...config,
pageController: pageController as any,
@@ -56,22 +68,16 @@ export class MultiPageAgent extends PageAgentCore {
},
onDispose: () => {
if (heartBeatInterval) {
clearInterval(heartBeatInterval)
heartBeatInterval = null
}
chrome.storage.local.set({ isAgentRunning: false }).catch(console.error)
tabsController.dispose()
},
})
/**
* Project agent status into chrome.storage. The content script polls
* `isAgentRunning` + `agentHeartbeat` (eventually consistent by design).
*
* When the agent is in side-panel and user closed the side-panel.
* There is no chance for isAgentRunning to be set false.
* (unload event doesn't work well in side panel.)
* (I'm trying not to use long-lived connection because the lifecycle of a sw is hard to predict.)
* This heartbeat mechanism acts as a backup.
*/
let heartBeatInterval: number | null = null
this.addEventListener('statuschange', () => {
const running = this.status === 'running'