From 20101911d38e611a6e545ae13ea839aa2f59ea6c Mon Sep 17 00:00:00 2001 From: Michael Rogers Date: Wed, 7 Jun 2023 17:36:53 -0500 Subject: [PATCH] Reconnect channel on error and UserIndicator updates --- src/api/user/RoleChannel.js | 21 +++++++++++-- .../components/UserIndicator.vue | 31 +++++++++++-------- 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/src/api/user/RoleChannel.js b/src/api/user/RoleChannel.js index ab9c64f514..64619b0b6d 100644 --- a/src/api/user/RoleChannel.js +++ b/src/api/user/RoleChannel.js @@ -1,17 +1,32 @@ import { BROADCAST_CHANNEL_NAME } from './constants'; class RoleChannel { + constructor(openmct, channelName = BROADCAST_CHANNEL_NAME) { + this.openmct = openmct; + this.channelName = channelName; + this.roleChannel = undefined; + } createRoleChannel() { - this.roleChannel = new BroadcastChannel(BROADCAST_CHANNEL_NAME); + this.roleChannel = new BroadcastChannel(this.channelName); + } + subscribeToRole(cb) { this.roleChannel.onmessage = (event => { const role = event.data; this.openmct.user.setActiveRole(role); + if (cb) { + cb(role); + } }); } unsubscribeToRole() { this.roleChannel.close(); } + reconnect() { + this.roleChannel.close(); + this.createRoleChannel(); + } + broadcastNewRole(role) { if (!this.roleChannel.name) { return false; @@ -20,6 +35,8 @@ class RoleChannel { try { this.roleChannel.postMessage(role); } catch (e) { + this.reconnect(); + this.broadcastNewRole(role); /** FIXME: there doesn't seem to be a reliable way to test for open/closed * status of a broadcast channel; channel.name exists even after the * channel is closed. Failure to update the subscribed tabs, should @@ -31,5 +48,5 @@ class RoleChannel { } } -export default new RoleChannel(); +export default RoleChannel; diff --git a/src/plugins/userIndicator/components/UserIndicator.vue b/src/plugins/userIndicator/components/UserIndicator.vue index 213441ebde..c646450754 100644 --- a/src/plugins/userIndicator/components/UserIndicator.vue +++ b/src/plugins/userIndicator/components/UserIndicator.vue @@ -37,25 +37,27 @@ export default { return { userName: undefined, role: undefined, - loggedIn: false + loggedIn: false, + roleChannelProvider: undefined }; }, async mounted() { this.getUserInfo(); - RoleChannelProvider.createRoleChannel(); + this.roleChannelProvider = new RoleChannelProvider(this.openmct); + this.roleChannelProvider.createRoleChannel(); + this.roleChannelProvider.subscribeToRole(this.setRoleSelection); await this.fetchOrPromptForRole(); }, beforeDestroy() { - RoleChannelProvider.unsubscribeToRole(); + this.roleChannelProvider.unsubscribeToRole(); }, methods: { - getUserInfo() { - this.openmct.user.getCurrentUser().then((user) => { - this.userName = user.getName(); - this.role = this.openmct.user.getActiveRole(); - this.loggedIn = this.openmct.user.isLoggedIn(); - }); + async getUserInfo() { + const user = await this.openmct.user.getCurrentUser(); + this.userName = user.getName(); + this.role = this.openmct.user.getActiveRole(); + this.loggedIn = this.openmct.user.isLoggedIn(); }, async fetchOrPromptForRole() { const UserAPI = this.openmct.user; @@ -93,18 +95,21 @@ export default { callback: () => { dialog.dismiss(); //TODO: introduce a notification of success - this.setRole(this.selectedRole); + this.updateRole(this.selectedRole); } } ] }); }, - - setRole(role) { + setRoleSelection(role) { this.role = role; + }, + + updateRole(role) { + this.setRoleSelection(role); this.openmct.user.setActiveRole(role); // update other tabs through broadcast channel - RoleChannelProvider.broadcastNewRole(role); + this.roleChannelProvider.broadcastNewRole(role); } }