Skip to content

Commit 427b594

Browse files
committed
Changes based on review feedback
1 parent 45688bc commit 427b594

8 files changed

Lines changed: 27 additions & 24 deletions

src/Frontend/src/components/audit/AuditList.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import AuditListItem from "@/components/audit/AuditListItem.vue";
88
import { onBeforeMount, ref, watch } from "vue";
99
import RefreshConfig from "../RefreshConfig.vue";
1010
import LoadingSpinner from "@/components/LoadingSpinner.vue";
11-
import createAutoRefresh from "@/composables/autoRefresh";
11+
import useFetchWithAutoRefresh from "@/composables/autoRefresh";
1212
1313
const store = useAuditStore();
1414
const { messages, totalCount, sortBy, messageFilterString, selectedEndpointName, itemsPerPage, dateRange } = storeToRefs(store);
1515
const route = useRoute();
1616
const router = useRouter();
1717
const autoRefreshValue = ref<number | null>(null);
18-
const { refreshNow, isRefreshing, updateInterval, start, stop } = createAutoRefresh("audit-list", store.refresh, 3000);
18+
const { refreshNow, isRefreshing, updateInterval, start, stop } = useFetchWithAutoRefresh("audit-list", store.refresh, 3000);
1919
const firstLoad = ref(true);
2020
2121
onBeforeMount(() => {

src/Frontend/src/composables/autoRefresh.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import { watch, ref, shallowReadonly, type WatchStopHandle } from "vue";
2-
import { useDocumentVisibility, useTimeoutPoll } from "@vueuse/core";
2+
import { useCounter, useDocumentVisibility, useTimeoutPoll } from "@vueuse/core";
33

4-
export default function createAutoRefresh(name: string, fetch: () => Promise<void>, intervalMs: number) {
5-
let refCount = 0;
4+
export default function useFetchWithAutoRefresh(name: string, fetch: () => Promise<void>, intervalMs: number) {
65
let watchStop: WatchStopHandle | null = null;
6+
const { count, inc, dec, reset } = useCounter(0);
77
const interval = ref(intervalMs);
88
const isRefreshing = ref(false);
99
const fetchWrapper = async () => {
10+
if (isRefreshing.value) {
11+
return;
12+
}
1013
isRefreshing.value = true;
1114
await fetch();
1215
isRefreshing.value = false;
@@ -20,8 +23,8 @@ export default function createAutoRefresh(name: string, fetch: () => Promise<voi
2023
const visibility = useDocumentVisibility();
2124

2225
const start = async () => {
23-
refCount++;
24-
if (refCount === 1) {
26+
inc();
27+
if (count.value === 1) {
2528
console.debug(`[AutoRefresh] Starting auto-refresh for ${name} every ${interval.value}ms`);
2629
resume();
2730
watchStop = watch(visibility, (current, previous) => {
@@ -36,22 +39,22 @@ export default function createAutoRefresh(name: string, fetch: () => Promise<voi
3639
}
3740
});
3841
} else {
39-
console.debug(`[AutoRefresh] Incremented refCount for ${name} to ${refCount}`);
42+
console.debug(`[AutoRefresh] Incremented refCount for ${name} to ${count.value}`);
4043
// Because another component has started using the auto-refresh, do an immediate refresh to ensure it has up-to-date data
4144
await fetchWrapper();
4245
}
4346
};
4447

4548
const stop = () => {
46-
refCount--;
47-
if (refCount <= 0) {
49+
dec();
50+
if (count.value <= 0) {
4851
console.debug(`[AutoRefresh] Stopping auto-refresh for ${name}`);
4952
pause();
5053
watchStop?.();
5154
watchStop = null;
52-
refCount = 0;
55+
reset();
5356
} else {
54-
console.debug(`[AutoRefresh] Decremented refCount for ${name} to ${refCount}`);
57+
console.debug(`[AutoRefresh] Decremented refCount for ${name} to ${count.value}`);
5558
}
5659
};
5760

src/Frontend/src/composables/useAutoRefresh.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { onMounted, onUnmounted } from "vue";
2-
import createAutoRefresh from "./autoRefresh";
2+
import useFetchWithAutoRefresh from "./autoRefresh";
33

44
export function useAutoRefresh(name: string, refresh: () => Promise<void>, intervalMs: number) {
5-
const { start, stop } = createAutoRefresh(name, refresh, intervalMs);
5+
const { start, stop } = useFetchWithAutoRefresh(name, refresh, intervalMs);
66

77
function useAutoRefresh() {
88
onMounted(start);
@@ -22,7 +22,7 @@ export function useAutoRefresh(name: string, refresh: () => Promise<void>, inter
2222
* @param intervalMs - Refresh interval in milliseconds
2323
* @returns A composable function that sets up auto-refresh and returns the store
2424
*/
25-
export function createStoreAutoRefresh<TStore extends { refresh: () => Promise<void> }>(name: string, useStore: () => TStore, intervalMs: number) {
25+
export function useStoreAutoRefresh<TStore extends { refresh: () => Promise<void> }>(name: string, useStore: () => TStore, intervalMs: number) {
2626
const refresh = () => {
2727
if (!store) {
2828
return Promise.resolve();
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import { useCustomChecksStore } from "@/stores/CustomChecksStore";
2-
import { createStoreAutoRefresh } from "./useAutoRefresh";
2+
import { useStoreAutoRefresh } from "./useAutoRefresh";
33

4-
export default createStoreAutoRefresh("customChecks", useCustomChecksStore, 5000);
4+
export default useStoreAutoRefresh("customChecks", useCustomChecksStore, 5000);
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import { useHeartbeatInstancesStore } from "@/stores/HeartbeatInstancesStore";
2-
import { createStoreAutoRefresh } from "./useAutoRefresh";
2+
import { useStoreAutoRefresh } from "./useAutoRefresh";
33

4-
export default createStoreAutoRefresh("heartbeatInstances", useHeartbeatInstancesStore, 5000);
4+
export default useStoreAutoRefresh("heartbeatInstances", useHeartbeatInstancesStore, 5000);
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import { useHeartbeatsStore } from "@/stores/HeartbeatsStore";
2-
import { createStoreAutoRefresh } from "./useAutoRefresh";
2+
import { useStoreAutoRefresh } from "./useAutoRefresh";
33

4-
export default createStoreAutoRefresh("heartbeats", useHeartbeatsStore, 5000);
4+
export default useStoreAutoRefresh("heartbeats", useHeartbeatsStore, 5000);
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import { useThroughputStore } from "@/stores/ThroughputStore";
2-
import { createStoreAutoRefresh } from "./useAutoRefresh";
2+
import { useStoreAutoRefresh } from "./useAutoRefresh";
33

4-
export default createStoreAutoRefresh("throughput", useThroughputStore, 60 * 60 * 1000 /* 1 hour */);
4+
export default useStoreAutoRefresh("throughput", useThroughputStore, 60 * 60 * 1000 /* 1 hour */);

src/Frontend/src/stores/CustomChecksStore.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const useCustomChecksStore = defineStore("CustomChecksStore", () => {
1111
const failingCount = ref(0);
1212
const failedChecks = ref<CustomCheck[]>([]);
1313

14-
const { count, inc, dec } = useCounter();
14+
const { count, inc, dec } = useCounter(0);
1515
const skipRefresh = computed(() => count.value > 0);
1616

1717
const refresh = async () => {

0 commit comments

Comments
 (0)