mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-08 02:52:07 +08:00
refactor: remove unused UI notification functions and streamline initialization logic
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
import { invoke } from '@tauri-apps/api/core'
|
||||
import { useEffect, useRef } from 'react'
|
||||
|
||||
import { hideInitialOverlay } from '../utils'
|
||||
@@ -10,7 +9,6 @@ export const useAppInitialization = () => {
|
||||
if (initRef.current) return
|
||||
initRef.current = true
|
||||
|
||||
let isInitialized = false
|
||||
let isCancelled = false
|
||||
const timers = new Set<number>()
|
||||
|
||||
@@ -26,76 +24,17 @@ export const useAppInitialization = () => {
|
||||
return id
|
||||
}
|
||||
|
||||
const notifyBackend = async (stage?: string) => {
|
||||
if (isCancelled) return
|
||||
|
||||
try {
|
||||
if (stage) {
|
||||
await invoke('update_ui_stage', { stage })
|
||||
} else {
|
||||
await invoke('notify_ui_ready')
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(`[Initialization] Failed to notify backend:`, err)
|
||||
}
|
||||
}
|
||||
|
||||
const removeLoadingOverlay = () => {
|
||||
hideInitialOverlay({ schedule: scheduleTimeout })
|
||||
}
|
||||
|
||||
const performInitialization = async () => {
|
||||
if (isCancelled || isInitialized) return
|
||||
isInitialized = true
|
||||
|
||||
try {
|
||||
removeLoadingOverlay()
|
||||
await notifyBackend('Loading')
|
||||
|
||||
await new Promise<void>((resolve) => {
|
||||
const check = () => {
|
||||
const root = document.getElementById('root')
|
||||
if (root && root.children.length > 0) {
|
||||
resolve()
|
||||
} else {
|
||||
scheduleTimeout(check, 50)
|
||||
}
|
||||
}
|
||||
check()
|
||||
scheduleTimeout(resolve, 2000)
|
||||
})
|
||||
|
||||
await notifyBackend('DomReady')
|
||||
await new Promise((resolve) => requestAnimationFrame(resolve))
|
||||
await notifyBackend('ResourcesLoaded')
|
||||
await notifyBackend()
|
||||
} catch (error) {
|
||||
if (!isCancelled) {
|
||||
console.error('[Initialization] Failed:', error)
|
||||
removeLoadingOverlay()
|
||||
notifyBackend().catch(console.error)
|
||||
}
|
||||
}
|
||||
const performInitialization = () => {
|
||||
if (isCancelled) return
|
||||
removeLoadingOverlay()
|
||||
}
|
||||
|
||||
const checkBackendReady = async () => {
|
||||
try {
|
||||
if (isCancelled) return
|
||||
|
||||
await invoke('update_ui_stage', { stage: 'Loading' })
|
||||
performInitialization()
|
||||
} catch {
|
||||
scheduleTimeout(performInitialization, 1500)
|
||||
}
|
||||
}
|
||||
|
||||
scheduleTimeout(checkBackendReady, 100)
|
||||
scheduleTimeout(() => {
|
||||
if (!isInitialized) {
|
||||
removeLoadingOverlay()
|
||||
notifyBackend().catch(console.error)
|
||||
}
|
||||
}, 5000)
|
||||
scheduleTimeout(performInitialization, 100)
|
||||
scheduleTimeout(performInitialization, 5000)
|
||||
|
||||
return () => {
|
||||
isCancelled = true
|
||||
|
||||
Reference in New Issue
Block a user