Skip to content

Commit 1770f5e

Browse files
basmeermanclaude
andcommitted
fix: event listener leak and ArduinoJson buffer sizes
- Move visibilitychange listener out of connectDataWs() to prevent duplicate listeners accumulating on each WS reconnect - Increase ArduinoJson document sizes: full sync 512→640, diff 256→384, initial sync 512→640 for safety margin with 25+ fields Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent d2f77d0 commit 1770f5e

File tree

3 files changed

+904
-902
lines changed

3 files changed

+904
-902
lines changed

SmartEVSE-3/data/app.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -172,19 +172,19 @@ function connectDataWs() {
172172
/* Resume polling while WS is disconnected */
173173
loadData();
174174
};
175-
176-
/* Pause/resume on visibility change */
177-
document.addEventListener('visibilitychange', function() {
178-
if (document.hidden) {
179-
if (dataWs) dataWs.close();
180-
} else if (!dataWs || dataWs.readyState !== WebSocket.OPEN) {
181-
clearTimeout(dataWsReconnectTimer);
182-
dataWsReconnectAttempts = 0;
183-
connectDataWs();
184-
}
185-
});
186175
}
187176

177+
/* Visibility-aware pause/resume for data WS (registered once) */
178+
document.addEventListener('visibilitychange', function() {
179+
if (document.hidden) {
180+
if (dataWs) dataWs.close();
181+
} else if (!dataWs || dataWs.readyState !== WebSocket.OPEN) {
182+
clearTimeout(dataWsReconnectTimer);
183+
dataWsReconnectAttempts = 0;
184+
connectDataWs();
185+
}
186+
});
187+
188188
/* ========== Theme (dark mode) ========== */
189189
function getTheme() {
190190
var stored = localStorage.getItem('evse_theme');

SmartEVSE-3/src/network_common.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ static void ws_data_timer_fn(void *arg) {
208208
if (fullSync) wsDataSyncCounter = 0;
209209

210210
// Build JSON
211-
DynamicJsonDocument doc(fullSync ? 512 : 256);
211+
DynamicJsonDocument doc(fullSync ? 640 : 384);
212212

213213
if (fullSync) {
214214
wsBuildFullState(doc);
@@ -1291,7 +1291,7 @@ static void fn_http_server(struct mg_connection *c, int ev, void *ev_data) {
12911291
}
12921292

12931293
// Send immediate full sync to newly connected client
1294-
DynamicJsonDocument doc(512);
1294+
DynamicJsonDocument doc(640);
12951295
wsBuildFullState(doc);
12961296
String json;
12971297
serializeJson(doc, json);

0 commit comments

Comments
 (0)