mirror of
https://github.com/jhbruhn/respira.git
synced 2026-01-27 02:13:41 +00:00
fix: Revert RAF implementation and restore original setTimeout polling
Co-authored-by: jhbruhn <1036566+jhbruhn@users.noreply.github.com>
This commit is contained in:
parent
cca86ff53e
commit
59c02d6b4d
1 changed files with 28 additions and 50 deletions
|
|
@ -42,7 +42,7 @@ interface MachineState {
|
||||||
isDeleting: boolean;
|
isDeleting: boolean;
|
||||||
|
|
||||||
// Polling control
|
// Polling control
|
||||||
pollRafId: number | null;
|
pollIntervalId: NodeJS.Timeout | null;
|
||||||
serviceCountIntervalId: NodeJS.Timeout | null;
|
serviceCountIntervalId: NodeJS.Timeout | null;
|
||||||
|
|
||||||
// Actions
|
// Actions
|
||||||
|
|
@ -81,7 +81,7 @@ export const useMachineStore = create<MachineState>((set, get) => ({
|
||||||
isPairingError: false,
|
isPairingError: false,
|
||||||
isCommunicating: false,
|
isCommunicating: false,
|
||||||
isDeleting: false,
|
isDeleting: false,
|
||||||
pollRafId: null,
|
pollIntervalId: null,
|
||||||
serviceCountIntervalId: null,
|
serviceCountIntervalId: null,
|
||||||
|
|
||||||
// Connect to machine
|
// Connect to machine
|
||||||
|
|
@ -370,70 +370,48 @@ export const useMachineStore = create<MachineState>((set, get) => ({
|
||||||
return 2000; // Default for idle states
|
return 2000; // Default for idle states
|
||||||
};
|
};
|
||||||
|
|
||||||
// Track last poll time for throttling
|
// Main polling function
|
||||||
// We use requestAnimationFrame for smooth UI updates but still need to
|
const poll = async () => {
|
||||||
// respect polling intervals to avoid excessive API calls
|
await refreshStatus();
|
||||||
let lastPollTime = 0;
|
|
||||||
|
|
||||||
// Main polling function using requestAnimationFrame
|
// Refresh progress during sewing
|
||||||
// Benefits over setTimeout:
|
if (get().machineStatus === MachineStatus.SEWING) {
|
||||||
// - Synchronized with browser refresh rate (typically 60fps)
|
await refreshProgress();
|
||||||
// - Automatically paused when tab is not visible (better battery life)
|
|
||||||
// - Smoother animations and UI updates
|
|
||||||
// - More efficient rendering
|
|
||||||
const poll = async (timestamp: number) => {
|
|
||||||
// Initialize lastPollTime on first call
|
|
||||||
if (lastPollTime === 0) {
|
|
||||||
lastPollTime = timestamp;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if enough time has passed since last poll
|
// follows the apps logic:
|
||||||
const interval = getPollInterval();
|
// Check if we have a cached pattern and pattern info needs refreshing
|
||||||
const elapsed = timestamp - lastPollTime;
|
const { useMachineCacheStore } = await import("./useMachineCacheStore");
|
||||||
|
if (
|
||||||
if (elapsed >= interval) {
|
useMachineCacheStore.getState().resumeAvailable &&
|
||||||
lastPollTime = timestamp;
|
get().patternInfo?.totalStitches == 0
|
||||||
|
) {
|
||||||
await refreshStatus();
|
await refreshPatternInfo();
|
||||||
|
|
||||||
// Refresh progress during sewing
|
|
||||||
if (get().machineStatus === MachineStatus.SEWING) {
|
|
||||||
await refreshProgress();
|
|
||||||
}
|
|
||||||
|
|
||||||
// follows the apps logic:
|
|
||||||
// Check if we have a cached pattern and pattern info needs refreshing
|
|
||||||
const { useMachineCacheStore } = await import("./useMachineCacheStore");
|
|
||||||
if (
|
|
||||||
useMachineCacheStore.getState().resumeAvailable &&
|
|
||||||
get().patternInfo?.totalStitches == 0
|
|
||||||
) {
|
|
||||||
await refreshPatternInfo();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Schedule next poll using requestAnimationFrame
|
// Schedule next poll with updated interval
|
||||||
const pollRafId = requestAnimationFrame(poll);
|
const newInterval = getPollInterval();
|
||||||
set({ pollRafId });
|
const pollIntervalId = setTimeout(poll, newInterval);
|
||||||
|
set({ pollIntervalId });
|
||||||
};
|
};
|
||||||
|
|
||||||
// Start polling with requestAnimationFrame
|
// Start polling
|
||||||
const pollRafId = requestAnimationFrame(poll);
|
const initialInterval = getPollInterval();
|
||||||
|
const pollIntervalId = setTimeout(poll, initialInterval);
|
||||||
|
|
||||||
// Service count polling (every 10 seconds)
|
// Service count polling (every 10 seconds)
|
||||||
// Keep using setInterval for this as 10s is too long for RAF
|
|
||||||
const serviceCountIntervalId = setInterval(refreshServiceCount, 10000);
|
const serviceCountIntervalId = setInterval(refreshServiceCount, 10000);
|
||||||
|
|
||||||
set({ pollRafId, serviceCountIntervalId });
|
set({ pollIntervalId, serviceCountIntervalId });
|
||||||
},
|
},
|
||||||
|
|
||||||
// Stop polling
|
// Stop polling
|
||||||
_stopPolling: () => {
|
_stopPolling: () => {
|
||||||
const { pollRafId, serviceCountIntervalId } = get();
|
const { pollIntervalId, serviceCountIntervalId } = get();
|
||||||
|
|
||||||
if (pollRafId) {
|
if (pollIntervalId) {
|
||||||
cancelAnimationFrame(pollRafId);
|
clearTimeout(pollIntervalId);
|
||||||
set({ pollRafId: null });
|
set({ pollIntervalId: null });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (serviceCountIntervalId) {
|
if (serviceCountIntervalId) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue