mirror of
https://github.com/nasa/openmct.git
synced 2024-12-27 00:31:06 +00:00
Reconnect channel on error and UserIndicator updates
This commit is contained in:
parent
1f9cc05ef2
commit
20101911d3
@ -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;
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user