feat(grid): add clock-hand progress animation to auto refresh

This commit is contained in:
zipg authored and GitHub committed 2026-09-24 12:13:47 +08:00
1 parent 6517284d95
commit e8e2625801
10 files changed
+185 -2

No files matched your search

@@ -29,4 +29,50 @@ describe("useDataGridAutoRefresh", () => {
expect(refresh).not.toHaveBeenCalled();
autoRefresh.stop();
});
it("bumps the sweep key when the countdown is armed and on every tick", () => {
vi.useFakeTimers();
const autoRefresh = useDataGridAutoRefresh({ canRefresh: computed(() => true), refresh: vi.fn(), initialIntervalSeconds: 5 });
expect(autoRefresh.sweepKey.value).toBe(0);
autoRefresh.toggle();
expect(autoRefresh.sweepKey.value).toBe(1);
vi.advanceTimersByTime(5000);
expect(autoRefresh.sweepKey.value).toBe(2);
vi.advanceTimersByTime(5000);
expect(autoRefresh.sweepKey.value).toBe(3);
autoRefresh.stop();
});
it("bumps the sweep key when the interval changes while enabled", () => {
vi.useFakeTimers();
const autoRefresh = useDataGridAutoRefresh({ canRefresh: computed(() => true), refresh: vi.fn(), initialIntervalSeconds: 5 });
autoRefresh.toggle();
expect(autoRefresh.sweepKey.value).toBe(1);
autoRefresh.setIntervalSeconds(30);
expect(autoRefresh.sweepKey.value).toBe(2);
expect(autoRefresh.intervalSeconds.value).toBe(30);
autoRefresh.stop();
});
it("keeps sweeping through skipped refreshes and freezes once disabled", () => {
vi.useFakeTimers();
const autoRefresh = useDataGridAutoRefresh({ canRefresh: computed(() => false), refresh: vi.fn(), initialIntervalSeconds: 1 });
autoRefresh.toggle();
vi.advanceTimersByTime(2000);
expect(autoRefresh.sweepKey.value).toBe(3);
autoRefresh.toggle();
vi.advanceTimersByTime(5000);
expect(autoRefresh.enabled.value).toBe(false);
expect(autoRefresh.sweepKey.value).toBe(3);
});
});
@@ -9,6 +9,10 @@ export interface UseDataGridAutoRefreshOptions {
export function useDataGridAutoRefresh(options: UseDataGridAutoRefreshOptions) {
const intervalSeconds = ref(options.initialIntervalSeconds ?? 10);
const enabled = ref(false);
// Bumped whenever the countdown is (re)armed: when the timer is started and on
// every tick. The toolbar keys its single-sweep clock hand off this, so the
// sweep restarts in phase with the real timer instead of free-running.
const sweepKey = ref(0);
let timer: ReturnType<typeof setInterval> | undefined;
function stop() {
@@ -17,6 +21,9 @@ export function useDataGridAutoRefresh(options: UseDataGridAutoRefreshOptions) {
}
function tick() {
// The sweep restarts even when the refresh itself is skipped: the timer is
// still counting down to the next attempt.
sweepKey.value += 1;
if (!enabled.value || !options.canRefresh.value) return;
void options.refresh();
}
@@ -24,6 +31,7 @@ export function useDataGridAutoRefresh(options: UseDataGridAutoRefreshOptions) {
function start() {
stop();
if (!enabled.value) return;
sweepKey.value += 1;
timer = setInterval(tick, intervalSeconds.value * 1000);
}
@@ -43,6 +51,7 @@ export function useDataGridAutoRefresh(options: UseDataGridAutoRefreshOptions) {
return {
intervalSeconds,
enabled,
sweepKey,
start,
stop,
setIntervalSeconds,