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';
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;

View File

@ -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) => {
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);
}
}