Skip to content

Commit 585d53e

Browse files
committed
Bugfix - resource management in lifecycle
1 parent cf926fb commit 585d53e

4 files changed

Lines changed: 73 additions & 30 deletions

File tree

‎dist/Predbat-Table-Card-Next.js‎

Lines changed: 33 additions & 15 deletions
Large diffs are not rendered by default.

‎src/predbat-card.ts‎

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,7 @@ class PredbatTableCard extends HTMLElement {
4848

4949
this._reactiveDataState.carCharging = this._configManager.carChargeSwitch;
5050

51-
if (this._predbatRestApiService) {
52-
this._predbatRestApiService.disconnect();
53-
this._predbatRestApiService = null;
54-
}
55-
56-
PredbatRestApiService.restApiAvailable(restApiPort)
57-
.then((restApiAvailable) => {
58-
this._restApiAvailable = restApiAvailable;
59-
60-
if (restApiAvailable) this._predbatRestApiService = new PredbatRestApiService(restApiPort, this.setPlanData);
61-
})
62-
.catch(() => this._restApiAvailable = false);
51+
this._initialiseRestApi();
6352
}
6453

6554
// * New state received from Home Assistant
@@ -170,6 +159,7 @@ class PredbatTableCard extends HTMLElement {
170159
if (this._predbatRestApiService) this._render();
171160
};
172161

162+
this._initialiseRestApi();
173163
this._render();
174164
}
175165

@@ -183,6 +173,9 @@ class PredbatTableCard extends HTMLElement {
183173
this._predbatRestApiService.disconnect();
184174
this._predbatRestApiService = null;
185175
}
176+
177+
this._lastPlanUpdated = null;
178+
this._planData = null;
186179
}
187180

188181
// * Register card with Home Assistant
@@ -206,6 +199,29 @@ class PredbatTableCard extends HTMLElement {
206199
this._render();
207200
};
208201

202+
private _initialiseRestApi(): void {
203+
if (this._predbatRestApiService) {
204+
this._predbatRestApiService.disconnect();
205+
this._predbatRestApiService = null;
206+
}
207+
208+
this._restApiAvailable = null;
209+
210+
PredbatRestApiService.restApiAvailable(restApiPort)
211+
.then((restApiAvailable) => {
212+
this._restApiAvailable = restApiAvailable;
213+
214+
if (restApiAvailable) this._predbatRestApiService = new PredbatRestApiService(restApiPort, this.setPlanData);
215+
216+
this._render();
217+
})
218+
.catch(() => {
219+
this._restApiAvailable = false;
220+
221+
this._render();
222+
});
223+
}
224+
209225
private _render(): void {
210226
if (
211227
this._restApiAvailable === null ||

‎src/services/predbat-service.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ export class PredbatRestApiService {
66
private _dataCallback: ((predbatData: PredbatData) => void) | null;
77
private _port: number;
88
private _updateInterval: ReturnType<typeof setInterval> | null = null;
9-
private _retryTimeout: ReturnType<typeof setTimeout> | null = null;
9+
private _retryTimeout: ReturnType<typeof setInterval> | null = null;
10+
private _initialUpdateTimeout: ReturnType<typeof setTimeout> | null = null;
1011
private _isAvailable = false;
1112
private _fetchInProgress = false;
12-
private _dataSource: 'REST_API' | 'HASS_STATES' | null = null;
1313

1414
public constructor(port: number, dataCallback: (predbatData: PredbatData) => void) {
1515
this._port = port;
@@ -94,7 +94,8 @@ export class PredbatRestApiService {
9494
nextUpdate.setHours(nextHour, nextMinute, 0, 0);
9595
const delay = nextUpdate.getTime() - now.getTime() + 5000;
9696

97-
setTimeout(() => {
97+
this._initialUpdateTimeout = setTimeout(() => {
98+
this._initialUpdateTimeout = null;
9899
this._fetchData();
99100
this._updateInterval = setInterval(() => {
100101
this._fetchData();
@@ -145,6 +146,12 @@ export class PredbatRestApiService {
145146
this._updateInterval = null;
146147
}
147148

149+
if (this._initialUpdateTimeout) {
150+
clearTimeout(this._initialUpdateTimeout);
151+
this._initialUpdateTimeout = null;
152+
}
153+
154+
this._dataCallback = null;
148155
this._isAvailable = false;
149156
}
150157
}

‎src/services/weather-service.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ export class WeatherService {
156156
await this._unsubscribeWeather();
157157
this._unsubscribeWeather = null;
158158
}
159+
160+
this._forecast = null;
159161
}
160162

161163
public set connection(connection: Connection) {

0 commit comments

Comments
 (0)