feat: 添加定时获取挂起会话列表的功能

This commit is contained in:
Baobhan Sith
2025-05-10 10:14:38 +08:00
parent 8fb665a69d
commit 3605ce0aee
2 changed files with 25 additions and 4 deletions
@@ -151,8 +151,12 @@ export const requestUnmarkSshSuspend = (sessionId: string): void => {
/**
* 获取挂起的 SSH 会话列表 (通过 HTTP API)
*/
export const fetchSuspendedSshSessions = async (): Promise<void> => {
isLoadingSuspendedSessions.value = true;
export const fetchSuspendedSshSessions = async (options?: { showLoadingIndicator?: boolean }): Promise<void> => {
const shouldShowLoading = options?.showLoadingIndicator ?? true;
if (shouldShowLoading) {
isLoadingSuspendedSessions.value = true;
}
try {
// 假设后端 API 端点为 /api/ssh/suspended-sessions
// 并且它返回 SuspendedSshSession[] 类型的数据
@@ -170,7 +174,9 @@ export const fetchSuspendedSshSessions = async (): Promise<void> => {
// 即使失败,也可能需要清空旧数据或保留旧数据,具体取决于产品需求
// suspendedSshSessions.value = []; // 例如,失败时清空
} finally {
isLoadingSuspendedSessions.value = false;
if (shouldShowLoading) {
isLoadingSuspendedSessions.value = false;
}
}
};
@@ -107,7 +107,7 @@
</template>
<script setup lang="ts">
import { ref, onMounted, computed, nextTick, watch } from 'vue'; // +++ 导入 nextTick watch +++
import { ref, onMounted, onUnmounted, computed, nextTick, watch } from 'vue'; // +++ 导入 nextTick, watch 和 onUnmounted +++
import { useI18n } from 'vue-i18n';
import { storeToRefs } from 'pinia';
import { useSessionStore } from '../stores/session.store';
@@ -243,8 +243,23 @@ const removeSession = (session: SuspendedSshSession) => { // 参数类型改为
}
};
let fetchIntervalId: number | undefined;
onMounted(async () => {
// 立即获取一次数据 (显示加载指示器)
await sessionStore.fetchSuspendedSshSessions();
// 设置定时器,每3秒获取一次数据 (不显示加载指示器)
fetchIntervalId = window.setInterval(async () => {
await sessionStore.fetchSuspendedSshSessions({ showLoadingIndicator: false });
}, 3000);
});
onUnmounted(() => {
// 组件卸载时清除定时器
if (fetchIntervalId) {
clearInterval(fetchIntervalId);
}
});
</script>