fix(workbench): delay live refresh on first load (#1943)
This commit is contained in:
@@ -1,8 +1,14 @@
|
|||||||
import { onBeforeUnmount, onMounted, ref, type Ref } from "vue";
|
import { onBeforeUnmount, onMounted, ref, type Ref } from "vue";
|
||||||
|
|
||||||
export function useAutoRefresh(callback: () => Promise<void> | void, intervalMs = 30_000): { active: Ref<boolean>; refreshNow: () => Promise<void> } {
|
export interface AutoRefreshOptions {
|
||||||
|
immediate?: boolean;
|
||||||
|
initialDelayMs?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useAutoRefresh(callback: () => Promise<void> | void, intervalMs = 30_000, options: AutoRefreshOptions = {}): { active: Ref<boolean>; refreshNow: () => Promise<void> } {
|
||||||
const active = ref(false);
|
const active = ref(false);
|
||||||
let timer: ReturnType<typeof setInterval> | null = null;
|
let intervalTimer: ReturnType<typeof setInterval> | null = null;
|
||||||
|
let initialTimer: ReturnType<typeof setTimeout> | null = null;
|
||||||
|
|
||||||
async function refreshNow(): Promise<void> {
|
async function refreshNow(): Promise<void> {
|
||||||
await callback();
|
await callback();
|
||||||
@@ -10,13 +16,27 @@ export function useAutoRefresh(callback: () => Promise<void> | void, intervalMs
|
|||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
active.value = true;
|
active.value = true;
|
||||||
|
const startInterval = (): void => {
|
||||||
|
if (intervalTimer !== null) return;
|
||||||
|
intervalTimer = setInterval(() => void refreshNow(), intervalMs);
|
||||||
|
};
|
||||||
|
if (options.immediate === false) {
|
||||||
|
const delay = Math.max(0, Math.trunc(Number(options.initialDelayMs ?? intervalMs)));
|
||||||
|
initialTimer = setTimeout(() => {
|
||||||
|
initialTimer = null;
|
||||||
|
void refreshNow();
|
||||||
|
startInterval();
|
||||||
|
}, delay);
|
||||||
|
return;
|
||||||
|
}
|
||||||
void refreshNow();
|
void refreshNow();
|
||||||
timer = setInterval(() => void refreshNow(), intervalMs);
|
startInterval();
|
||||||
});
|
});
|
||||||
|
|
||||||
onBeforeUnmount(() => {
|
onBeforeUnmount(() => {
|
||||||
active.value = false;
|
active.value = false;
|
||||||
if (timer) clearInterval(timer);
|
if (initialTimer) clearTimeout(initialTimer);
|
||||||
|
if (intervalTimer) clearInterval(intervalTimer);
|
||||||
});
|
});
|
||||||
|
|
||||||
return { active, refreshNow };
|
return { active, refreshNow };
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ onBeforeUnmount(() => {
|
|||||||
});
|
});
|
||||||
watch(() => route.params.sessionId, () => void applyRouteSession());
|
watch(() => route.params.sessionId, () => void applyRouteSession());
|
||||||
watch(() => workbench.activeSessionId, (sessionId) => void reflectActiveSessionInUrl(sessionId));
|
watch(() => workbench.activeSessionId, (sessionId) => void reflectActiveSessionInUrl(sessionId));
|
||||||
useAutoRefresh(() => workbench.refreshLive(), 30_000);
|
useAutoRefresh(() => workbench.refreshLive(), 30_000, { immediate: false, initialDelayMs: 10_000 });
|
||||||
|
|
||||||
async function applyRouteSession(): Promise<boolean> {
|
async function applyRouteSession(): Promise<boolean> {
|
||||||
const sessionId = routeRequestId.value;
|
const sessionId = routeRequestId.value;
|
||||||
|
|||||||
Reference in New Issue
Block a user