Skip to content

Commit

Permalink
fix: Fix race condition in frontend extension (Koenkk#23412)
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Jul 24, 2024
1 parent b7d9d9e commit 5fcf295
Showing 1 changed file with 6 additions and 20 deletions.
26 changes: 6 additions & 20 deletions lib/extension/frontend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,6 @@ export default class Frontend extends Extension {
private fileServer: RequestHandler;
private wss: WebSocket.Server = null;

constructor(
zigbee: Zigbee,
mqtt: MQTT,
state: State,
publishEntityState: PublishEntityState,
eventBus: EventBus,
enableDisableExtension: (enable: boolean, name: string) => Promise<void>,
restartCallback: () => Promise<void>,
addExtension: (extension: Extension) => Promise<void>,
) {
super(zigbee, mqtt, state, publishEntityState, eventBus, enableDisableExtension, restartCallback, addExtension);
this.eventBus.onMQTTMessagePublished(this, this.onMQTTPublishMessage);
}

private isHttpsConfigured(): boolean {
if (this.sslCert && this.sslKey) {
if (!fs.existsSync(this.sslCert) || !fs.existsSync(this.sslKey)) {
Expand Down Expand Up @@ -82,6 +68,8 @@ export default class Frontend extends Extension {
this.wss = new WebSocket.Server({noServer: true});
this.wss.on('connection', this.onWebSocketConnection);

this.eventBus.onMQTTMessagePublished(this, this.onMQTTPublishMessage);

if (!this.host) {
this.server.listen(this.port);
logger.info(`Started frontend on port ${this.port}`);
Expand Down Expand Up @@ -169,12 +157,10 @@ export default class Frontend extends Extension {
this.retainedMessages.set(topic, payload);
}

if (this.wss) {
for (const client of this.wss.clients) {
/* istanbul ignore else */
if (client.readyState === WebSocket.OPEN) {
client.send(stringify({topic, payload}));
}
for (const client of this.wss.clients) {
/* istanbul ignore else */
if (client.readyState === WebSocket.OPEN) {
client.send(stringify({topic, payload}));
}
}
}
Expand Down

0 comments on commit 5fcf295

Please sign in to comment.