Moved roleChannel to private field

This commit is contained in:
Michael Rogers 2023-06-20 17:28:52 -05:00
parent af1fc68766
commit 6444e9139e

View File

@ -1,17 +1,18 @@
import { BROADCAST_CHANNEL_NAME } from './constants';
class RoleChannel {
#roleChannel;
constructor(openmct, channelName = BROADCAST_CHANNEL_NAME) {
this.openmct = openmct;
this.channelName = channelName;
this.roleChannel = undefined;
}
createRoleChannel() {
this.roleChannel = new BroadcastChannel(this.channelName);
this.#roleChannel = new BroadcastChannel(this.channelName);
}
subscribeToRole(cb) {
this.roleChannel.onmessage = (event => {
this.#roleChannel.onmessage = (event => {
const role = event.data;
this.openmct.user.setActiveRole(role);
if (cb) {
@ -20,21 +21,23 @@ class RoleChannel {
});
}
unsubscribeToRole() {
this.roleChannel.close();
this.#roleChannel.close();
}
reconnect() {
this.roleChannel.close();
this.#roleChannel.close();
this.createRoleChannel();
}
broadcastNewRole(role) {
if (!this.roleChannel.name) {
if (!this.#roleChannel.name) {
return false;
}
try {
this.roleChannel.postMessage(role);
this.#roleChannel.postMessage(role);
} catch (e) {
console.error(e);
this.reconnect();
this.broadcastNewRole(role);
/** FIXME: there doesn't seem to be a reliable way to test for open/closed