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