Files
clash-verge-rev/src/pages/_layout/hooks/use-layout-events.ts
T
wonfen daae17a40c fix(service): surface a service core that stopped for good
When the service watchdog gave up or the core vanished, the owner
monitor marked the core stopped without telling the user, and on macOS
left the system proxy on the dead mixed port (#8029).

- On SameOwnerFailure, clear the proxy through the helper while the
  session is still ours; displaced or unreachable owners still leave the
  machine-wide setting alone.
- Retain the stop as a core failure with its own message, recorded
  under the lifecycle lock and shown now or on next focus; a running
  core resets the notice dedup.
- Log each watchdog restart the service reports, since launchd discards
  the service's own log.
2026-09-25 00:06:59 +08:00

87 lines
2.9 KiB
TypeScript

import { getCurrentWindow } from '@tauri-apps/api/window'
import { useEffect } from 'react'
import { revalidateProfiles } from '@/hooks/use-profiles'
import { runStateQueryKey } from '@/hooks/use-system-state'
import type { RunState } from '@/services/cmds'
import { subscribeVergeEvents } from '@/services/events'
import { revalidateQueries, setCacheData } from '@/services/query-client'
import { forgetShownStartupError } from '../utils/notification-handlers'
export const useLayoutEvents = (
handleNotice: (payload: [string, string]) => void,
) => {
useEffect(() => {
let lastProfileId: string | null = null
let lastProfileUpdateTime = 0
const refreshThrottle = 800
const revalidateKeys = (keys: readonly string[]) => {
void revalidateQueries(keys.map((key) => [key]))
}
const handleProfileChanged = (newProfileId: string) => {
const now = Date.now()
if (
lastProfileId === newProfileId &&
now - lastProfileUpdateTime < refreshThrottle
) {
return
}
lastProfileId = newProfileId
lastProfileUpdateTime = now
void revalidateProfiles()
}
const unsubscribe = subscribeVergeEvents(
{
'profile-changed': handleProfileChanged,
'verge://refresh-profiles': () => void revalidateProfiles(),
'verge://refresh-clash-config': () => {
revalidateKeys([
'getProxyView',
'getVersion',
'getClashConfig',
'getClashInfo',
'getClashMode',
'getRuntimeConfig',
'getRules',
'getRuleProviders',
])
},
'verge://refresh-verge-config': () => {
revalidateKeys([
'getVergeConfig',
'getSystemProxy',
'getAutotemProxy',
])
},
// Transitions carry the full run-state snapshot, so write it directly to cache.
'verge://run-state-changed': (payload) => {
void setCacheData<RunState>(runStateQueryKey, payload)
if (payload.mode !== 'NotRunning') forgetShownStartupError()
},
'verge://notice-message': handleNotice,
},
// Re-read event-only state after subscribing to close the initial race window.
() => {
revalidateKeys(['getRuntimeState', 'getVergeConfig'])
handleNotice(['dns_override::auto_disabled', ''])
handleNotice(['enhance::discarded_keys', ''])
handleNotice(['service_core::sidecar_fallback', ''])
handleNotice(['service_core::repair_required', ''])
handleNotice(['service_core::app_data_not_owned', ''])
handleNotice(['core_start::error', ''])
},
)
const unlistenFocus = getCurrentWindow().onFocusChanged(({ payload }) => {
if (payload) handleNotice(['core_start::error', ''])
})
return () => {
unsubscribe()
void unlistenFocus.then((unlisten) => unlisten())
}
}, [handleNotice])
}