Reconnect channel on error and UserIndicator updates

This commit is contained in:
Michael Rogers 2023-06-07 17:36:53 -05:00
parent 1f9cc05ef2
commit 20101911d3
2 changed files with 37 additions and 15 deletions

View File

@ -1,17 +1,32 @@
import { BROADCAST_CHANNEL_NAME } from './constants'; import { BROADCAST_CHANNEL_NAME } from './constants';
class RoleChannel { class RoleChannel {
constructor(openmct, channelName = BROADCAST_CHANNEL_NAME) {
this.openmct = openmct;
this.channelName = channelName;
this.roleChannel = undefined;
}
createRoleChannel() { createRoleChannel() {
this.roleChannel = new BroadcastChannel(BROADCAST_CHANNEL_NAME); this.roleChannel = new BroadcastChannel(this.channelName);
}
subscribeToRole(cb) {
this.roleChannel.onmessage = (event => { this.roleChannel.onmessage = (event => {
const role = event.data; const role = event.data;
this.openmct.user.setActiveRole(role); this.openmct.user.setActiveRole(role);
if (cb) {
cb(role);
}
}); });
} }
unsubscribeToRole() { unsubscribeToRole() {
this.roleChannel.close(); this.roleChannel.close();
} }
reconnect() {
this.roleChannel.close();
this.createRoleChannel();
}
broadcastNewRole(role) { broadcastNewRole(role) {
if (!this.roleChannel.name) { if (!this.roleChannel.name) {
return false; return false;
@ -20,6 +35,8 @@ class RoleChannel {
try { try {
this.roleChannel.postMessage(role); this.roleChannel.postMessage(role);
} catch (e) { } catch (e) {
this.reconnect();
this.broadcastNewRole(role);
/** FIXME: there doesn't seem to be a reliable way to test for open/closed /** 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 * status of a broadcast channel; channel.name exists even after the
* channel is closed. Failure to update the subscribed tabs, should * channel is closed. Failure to update the subscribed tabs, should
@ -31,5 +48,5 @@ class RoleChannel {
} }
} }
export default new RoleChannel(); export default RoleChannel;

View File

@ -37,25 +37,27 @@ export default {
return { return {
userName: undefined, userName: undefined,
role: undefined, role: undefined,
loggedIn: false loggedIn: false,
roleChannelProvider: undefined
}; };
}, },
async mounted() { async mounted() {
this.getUserInfo(); this.getUserInfo();
RoleChannelProvider.createRoleChannel(); this.roleChannelProvider = new RoleChannelProvider(this.openmct);
this.roleChannelProvider.createRoleChannel();
this.roleChannelProvider.subscribeToRole(this.setRoleSelection);
await this.fetchOrPromptForRole(); await this.fetchOrPromptForRole();
}, },
beforeDestroy() { beforeDestroy() {
RoleChannelProvider.unsubscribeToRole(); this.roleChannelProvider.unsubscribeToRole();
}, },
methods: { methods: {
getUserInfo() { async getUserInfo() {
this.openmct.user.getCurrentUser().then((user) => { const user = await this.openmct.user.getCurrentUser();
this.userName = user.getName(); this.userName = user.getName();
this.role = this.openmct.user.getActiveRole(); this.role = this.openmct.user.getActiveRole();
this.loggedIn = this.openmct.user.isLoggedIn(); this.loggedIn = this.openmct.user.isLoggedIn();
});
}, },
async fetchOrPromptForRole() { async fetchOrPromptForRole() {
const UserAPI = this.openmct.user; const UserAPI = this.openmct.user;
@ -93,18 +95,21 @@ export default {
callback: () => { callback: () => {
dialog.dismiss(); dialog.dismiss();
//TODO: introduce a notification of success //TODO: introduce a notification of success
this.setRole(this.selectedRole); this.updateRole(this.selectedRole);
} }
} }
] ]
}); });
}, },
setRoleSelection(role) {
setRole(role) {
this.role = role; this.role = role;
},
updateRole(role) {
this.setRoleSelection(role);
this.openmct.user.setActiveRole(role); this.openmct.user.setActiveRole(role);
// update other tabs through broadcast channel // update other tabs through broadcast channel
RoleChannelProvider.broadcastNewRole(role); this.roleChannelProvider.broadcastNewRole(role);
} }
} }