Implement exponential backoff for WebSocket reconnection and enhance error handling in recipe management functions

This commit is contained in:
MayaTheShy
2026-03-22 02:38:21 -04:00
parent 0fb57d7c94
commit ea75c1eabc

View File

@@ -140,6 +140,7 @@ export const useInventoryStore = create((set, get) => ({
ws.onopen = () => { ws.onopen = () => {
console.log('✅ Connected to server'); console.log('✅ Connected to server');
_lastWsMessage = Date.now(); _lastWsMessage = Date.now();
_reconnectDelay = 1000;
set({ connected: true, ws }); set({ connected: true, ws });
}; };
@@ -170,9 +171,11 @@ export const useInventoryStore = create((set, get) => ({
ws.onclose = () => { ws.onclose = () => {
console.log('❌ Disconnected from server'); console.log('❌ Disconnected from server');
set({ connected: false, ws: null }); set({ connected: false, ws: null });
const delay = _reconnectDelay;
_reconnectDelay = Math.min(_reconnectDelay * 2, 30000);
setTimeout(() => { setTimeout(() => {
get().connect(); get().connect();
}, 3000); }, delay);
}; };
ws.onerror = (error) => { ws.onerror = (error) => {
@@ -253,25 +256,29 @@ export const useInventoryStore = create((set, get) => ({
enableAllRecipes: async () => { enableAllRecipes: async () => {
try { try {
await fetch(`${API_URL}/recipes/enable-all`, { const response = await fetch(`${API_URL}/recipes/enable-all`, {
method: 'POST', method: 'POST',
headers: authHeaders(), headers: authHeaders(),
body: JSON.stringify({ commandId: newCommandId() }), body: JSON.stringify({ commandId: newCommandId() }),
}); });
return await response.json();
} catch (error) { } catch (error) {
console.error('❌ Error enabling all recipes:', error); console.error('❌ Error enabling all recipes:', error);
return { success: false, error: error.message };
} }
}, },
disableAllRecipes: async () => { disableAllRecipes: async () => {
try { try {
await fetch(`${API_URL}/recipes/disable-all`, { const response = await fetch(`${API_URL}/recipes/disable-all`, {
method: 'POST', method: 'POST',
headers: authHeaders(), headers: authHeaders(),
body: JSON.stringify({ commandId: newCommandId() }), body: JSON.stringify({ commandId: newCommandId() }),
}); });
return await response.json();
} catch (error) { } catch (error) {
console.error('❌ Error disabling all recipes:', error); console.error('❌ Error disabling all recipes:', error);
return { success: false, error: error.message };
} }
}, },