mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-26 23:30:09 +08:00
71 lines
2.1 KiB
TypeScript
71 lines
2.1 KiB
TypeScript
import { listen, type UnlistenFn } from '@tauri-apps/api/event'
|
|
import { getCurrentWindow } from '@tauri-apps/api/window'
|
|
import { useEffect } from 'react'
|
|
|
|
import { revalidateQueries } from '@/services/query-client'
|
|
import { useAppDispatch, useAppReads } from '@/store/app-store-context'
|
|
|
|
import { createEventBus } from './create-bus-handlers'
|
|
|
|
/**
|
|
* Mounts the event bus exactly once (layout root): one `listen` per contract
|
|
* event plus the frontend-internal test-all event. Replaces every scattered
|
|
* subscription; teardown disposes late-resolving registrations.
|
|
*/
|
|
export const useEventBus = (
|
|
handleNotice: (payload: [string, string]) => void,
|
|
) => {
|
|
const dispatch = useAppDispatch()
|
|
const reads = useAppReads()
|
|
|
|
useEffect(() => {
|
|
const revalidateKeys = (keys: readonly string[]) => {
|
|
void revalidateQueries(keys.map((key) => [key]))
|
|
}
|
|
const { readPendingFailures } = reads
|
|
const readRunState = () => {
|
|
void reads.readRunState().catch(() => {})
|
|
}
|
|
|
|
const bus = createEventBus({
|
|
dispatch,
|
|
handleNotice,
|
|
revalidateKeys,
|
|
readPendingFailures,
|
|
readRunState,
|
|
})
|
|
|
|
let disposed = false
|
|
const unlisteners: UnlistenFn[] = []
|
|
const registrations = (
|
|
Object.keys(bus.handlers) as Array<keyof typeof bus.handlers>
|
|
).map((name) =>
|
|
listen(name, ({ payload }) => {
|
|
;(bus.handlers[name] as (payload: unknown) => void)(payload)
|
|
}).then((unlisten) => {
|
|
if (disposed) {
|
|
unlisten()
|
|
return
|
|
}
|
|
unlisteners.push(unlisten)
|
|
}),
|
|
)
|
|
|
|
// Re-read event-only state after subscribing to close the initial race window.
|
|
void Promise.all(registrations).then(() => {
|
|
if (disposed) return
|
|
bus.onSubscribed()
|
|
})
|
|
|
|
const unlistenFocus = getCurrentWindow().onFocusChanged(({ payload }) => {
|
|
if (payload) bus.onWindowFocus()
|
|
})
|
|
|
|
return () => {
|
|
disposed = true
|
|
for (const unlisten of unlisteners.splice(0)) unlisten()
|
|
void unlistenFocus.then((unlisten) => unlisten())
|
|
}
|
|
}, [dispatch, handleNotice, reads])
|
|
}
|