优化定时器

This commit is contained in:
2026-04-22 13:53:42 +08:00
parent 9c3b014fde
commit a190e1bff6
4 changed files with 163 additions and 90 deletions

View File

@@ -270,6 +270,7 @@ import { ref, computed, watch, onMounted, onUnmounted } from 'vue'
import { isElectron } from '../utils/electronBridge'
import { getPermissions } from '@/utils/storage'
import { usePythonBridge } from '@/utils/pythonBridge'
import { ensureHostListGateMonitorRunning, stopHostListGateMonitor, syncHostListGateDecision } from '@/utils/hostListGateMonitor'
const props = defineProps({
visible: { type: Boolean, required: true }
@@ -339,11 +340,12 @@ const maxCount = ref()
const checkTaskGateState = ref(null)
const selectedLevels = ref(new Set())
const showLevelDropdown = ref(false)
const hostListDialogInstanceId = `host-list-dialog-${Math.random().toString(36).slice(2)}`
const resolveRestrictedMaxAnchorCount = (fallbackValue = 9999999) => {
const permissions = getPermissions()
return permissions?.webAi === 1 ? fallbackValue : 0
const hasAutotkPermission = permissions?.autotk === 1
const hasWebAiPermission = permissions?.webAi === 1
return hasAutotkPermission || hasWebAiPermission ? fallbackValue : 1
}
// 添加主播弹窗状态
@@ -402,23 +404,17 @@ watch(() => props.visible, (newVal) => {
if (newVal) {
document.body.style.overflow = 'hidden'
loadLocalGateConfig()
if (gateEnabled.value) {
claimGateOwnership()
}
if (gateEnabled.value) ensureHostListGateMonitorRunning()
loadHosts()
loadConfig()
} else {
document.body.style.overflow = ''
}
refreshHostListMonitor()
})
onMounted(() => {
loadLocalGateConfig()
if (gateEnabled.value) {
claimGateOwnership()
}
if (gateEnabled.value) ensureHostListGateMonitorRunning()
loadConfig()
if (props.visible) {
@@ -426,11 +422,13 @@ onMounted(() => {
loadHosts()
}
refreshHostListMonitor()
if (gateEnabled.value) {
void syncHostListGateDecision(true)
}
})
onUnmounted(() => {
releaseGateOwnership()
document.body.style.overflow = ''
})
// 监听过滤器变化,同步到后端配置
@@ -501,76 +499,6 @@ const loadConfig = async () => {
}
}
const getGateRuntime = () => {
if (typeof window === 'undefined') {
return { ownerId: null, timer: null }
}
if (!window.__hostListGateRuntime) {
window.__hostListGateRuntime = {
ownerId: null,
timer: null,
}
}
return window.__hostListGateRuntime
}
const isGateOwner = () => {
return getGateRuntime().ownerId === hostListDialogInstanceId
}
const claimGateOwnership = () => {
const runtime = getGateRuntime()
if (runtime.ownerId === hostListDialogInstanceId) return
if (runtime.timer) {
clearInterval(runtime.timer)
runtime.timer = null
}
runtime.ownerId = hostListDialogInstanceId
}
const releaseGateOwnership = () => {
const runtime = getGateRuntime()
if (runtime.ownerId !== hostListDialogInstanceId) return
if (runtime.timer) {
clearInterval(runtime.timer)
runtime.timer = null
}
runtime.ownerId = null
}
const refreshHostListMonitor = () => {
if (!isElectron()) return
const runtime = getGateRuntime()
const shouldRun = gateEnabled.value && isGateOwner()
if (!shouldRun) {
if (runtime.timer && isGateOwner()) {
clearInterval(runtime.timer)
runtime.timer = null
}
return
}
if (runtime.timer) return
runtime.timer = setInterval(async () => {
if (!gateEnabled.value || !isGateOwner()) {
refreshHostListMonitor()
return
}
await loadHosts()
await syncControlCheckTaskGate()
}, HOST_LIST_MONITOR_INTERVAL_MS)
}
const loadLocalGateConfig = () => {
try {
const savedGateEnabled = sessionStorage.getItem(HOST_LIST_GATE_ENABLED_SESSION_KEY)
@@ -634,7 +562,6 @@ const persistGateState = (value) => {
const syncControlCheckTaskGate = async (forceSync = false) => {
if (!isElectron()) return
if (!gateEnabled.value) return
if (!isGateOwner()) return
const hostCount = filteredHosts.value.length
const upperLimit = Number(maxCount.value)
@@ -766,16 +693,15 @@ watch(gateEnabled, async (enabled) => {
persistGateEnabled()
if (enabled) {
claimGateOwnership()
refreshHostListMonitor()
ensureHostListGateMonitorRunning()
void syncHostListGateDecision(true)
void syncControlCheckTaskGate(true)
return
}
checkTaskGateState.value = null
persistGateState(null)
releaseGateOwnership()
refreshHostListMonitor()
stopHostListGateMonitor()
if (!isElectron()) return
@@ -809,6 +735,8 @@ const filteredHosts = computed(() => {
watch(
[() => filteredHosts.value.length, minCount, maxCount, gateEnabled],
() => {
if (gateEnabled.value) ensureHostListGateMonitorRunning()
void syncHostListGateDecision()
void syncControlCheckTaskGate()
}
)