Add automatic background refresh
Some checks are pending
Validate Decky / web-client (push) Waiting to run

This commit is contained in:
Elijah 2026-07-20 09:00:24 -07:00
parent e9d76cc4a2
commit ee2ef03982
9 changed files with 163 additions and 25 deletions

View file

@ -0,0 +1,37 @@
import { describe, expect, it } from 'vitest';
import { AUTO_REFRESH_IDLE_MS, AUTO_REFRESH_REPEAT_MS, shouldAutoRefresh } from './auto-refresh';
const idleState = {
now: AUTO_REFRESH_REPEAT_MS,
lastInteractionAt: 0,
lastRefreshAt: 0,
connected: true,
busy: false,
editing: false,
dragging: false,
visible: true
};
describe('automatic refresh scheduling', () => {
it('refreshes after the user has been idle long enough', () => {
expect(shouldAutoRefresh(idleState)).toBe(true);
});
it('waits for ten seconds of inactivity', () => {
expect(shouldAutoRefresh({ ...idleState, lastInteractionAt: idleState.now - AUTO_REFRESH_IDLE_MS + 1 })).toBe(false);
});
it('does not repeatedly refresh more often than the background interval', () => {
expect(shouldAutoRefresh({ ...idleState, lastRefreshAt: idleState.now - AUTO_REFRESH_REPEAT_MS + 1 })).toBe(false);
});
it.each([
['disconnected', { connected: false }],
['busy', { busy: true }],
['editing', { editing: true }],
['dragging', { dragging: true }],
['hidden', { visible: false }]
])('pauses while %s', (_name, change) => {
expect(shouldAutoRefresh({ ...idleState, ...change })).toBe(false);
});
});

View file

@ -0,0 +1,23 @@
export const AUTO_REFRESH_IDLE_MS = 10_000;
export const AUTO_REFRESH_REPEAT_MS = 30_000;
export interface AutoRefreshState {
now: number;
lastInteractionAt: number;
lastRefreshAt: number;
connected: boolean;
busy: boolean;
editing: boolean;
dragging: boolean;
visible: boolean;
}
export function shouldAutoRefresh(state: AutoRefreshState): boolean {
return state.connected
&& state.visible
&& !state.busy
&& !state.editing
&& !state.dragging
&& state.now - state.lastInteractionAt >= AUTO_REFRESH_IDLE_MS
&& state.now - state.lastRefreshAt >= AUTO_REFRESH_REPEAT_MS;
}