refactor: auto-refresh in server status

This commit is contained in:
Lykin 2024-01-12 11:38:25 +08:00
parent 042ef3075a
commit 4bf35e736e
1 changed files with 44 additions and 10 deletions

View File

@ -5,6 +5,7 @@ import IconButton from '@/components/common/IconButton.vue'
import Filter from '@/components/icons/Filter.vue' import Filter from '@/components/icons/Filter.vue'
import Refresh from '@/components/icons/Refresh.vue' import Refresh from '@/components/icons/Refresh.vue'
import useBrowserStore from 'stores/browser.js' import useBrowserStore from 'stores/browser.js'
import { timeout } from '@/utils/promise.js'
const props = defineProps({ const props = defineProps({
server: String, server: String,
@ -13,8 +14,8 @@ const props = defineProps({
const browserStore = useBrowserStore() const browserStore = useBrowserStore()
const serverInfo = ref({}) const serverInfo = ref({})
const pageState = reactive({ const pageState = reactive({
auto: false, autoRefresh: false,
autoInterval: 5, refreshInterval: 1,
loading: false, // loading status for refresh loading: false, // loading status for refresh
autoLoading: false, // loading status for auto refresh autoLoading: false, // loading status for auto refresh
}) })
@ -40,18 +41,48 @@ const refreshInfo = async (force) => {
} }
} }
let intervalID const startAutoRefresh = async () => {
if (pageState.autoRefresh) {
return
}
pageState.autoRefresh = true
if (!isNaN(pageState.refreshInterval)) {
pageState.refreshInterval = 5
}
pageState.refreshInterval = Math.min(pageState.refreshInterval, 1)
let lastExec = Date.now()
do {
if (!pageState.autoRefresh) {
break
}
await timeout(100)
if (pageState.loading || pageState.autoLoading || Date.now() - lastExec < pageState.refreshInterval * 1000) {
continue
}
lastExec = Date.now()
await refreshInfo()
} while (true)
stopAutoRefresh()
}
const stopAutoRefresh = () => {
pageState.autoRefresh = false
}
const onToggleRefresh = (on) => {
if (on) {
startAutoRefresh()
} else {
stopAutoRefresh()
}
}
onMounted(() => { onMounted(() => {
refreshInfo() refreshInfo()
intervalID = setInterval(() => {
if (pageState.auto === true) {
refreshInfo()
}
}, pageState.autoInterval * 1000)
}) })
onUnmounted(() => { onUnmounted(() => {
clearInterval(intervalID) stopAutoRefresh()
}) })
const scrollRef = ref(null) const scrollRef = ref(null)
@ -142,7 +173,10 @@ const infoFilter = ref('')
<template #header-extra> <template #header-extra>
<n-space align="center" inline> <n-space align="center" inline>
{{ $t('status.auto_refresh') }} {{ $t('status.auto_refresh') }}
<n-switch v-model:value="pageState.auto" :loading="pageState.autoLoading" /> <n-switch
:loading="pageState.autoLoading"
:value="pageState.autoRefresh"
@update:value="onToggleRefresh" />
<n-tooltip> <n-tooltip>
{{ $t('status.refresh') }} {{ $t('status.refresh') }}
<template #trigger> <template #trigger>