Skip to content

Commit

Permalink
Only render things once - fixes WebThingsIO#3103
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrancis committed Aug 21, 2023
1 parent d3c08ba commit d740bba
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions static/js/models/gateway-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class GatewayModel extends Model {
this.groups = new Map();
this.onMessage = this.onMessage.bind(this);
this.queue = Promise.resolve(true);
this.connectWebSocket();
this.connectWebSockets();
return this;
}

Expand Down Expand Up @@ -137,16 +137,27 @@ class GatewayModel extends Model {
}
}

connectWebSocket() {
connectWebSockets() {
const thingsHref = `${window.location.origin}/things?jwt=${API.jwt}`;
const wsHref = thingsHref.replace(/^http/, 'ws');
this.ws = new ReopeningWebSocket(wsHref);
this.ws.addEventListener('open', this.refreshThings.bind(this));
this.ws.addEventListener('message', this.onMessage);
const groupsHref = `${window.location.origin}/groups?jwt=${API.jwt}`;
const groupsWsHref = groupsHref.replace(/^http/, 'ws');

this.ws = new ReopeningWebSocket(wsHref);
this.groupsWs = new ReopeningWebSocket(groupsWsHref);
this.groupsWs.addEventListener('open', this.refreshThings.bind(this));

// Wait for things and groups sockets to open before showing Things
const thingsReady = new Promise((resolve) => {
this.ws.addEventListener('open', resolve);
});
const groupsReady = new Promise((resolve) => {
this.groupsWs.addEventListener('open', resolve);
});
Promise.all([thingsReady, groupsReady]).then(() => {
this.refreshThings();
});

this.ws.addEventListener('message', this.onMessage);
this.groupsWs.addEventListener('message', this.onMessage);
}

Expand Down

0 comments on commit d740bba

Please sign in to comment.