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'; import { BROADCAST_CHANNEL_NAME } from './constants';
class RoleChannel { class RoleChannel {
#roleChannel;
constructor(openmct, channelName = BROADCAST_CHANNEL_NAME) { constructor(openmct, channelName = BROADCAST_CHANNEL_NAME) {
this.openmct = openmct; this.openmct = openmct;
this.channelName = channelName; this.channelName = channelName;
this.roleChannel = undefined;
} }
createRoleChannel() { createRoleChannel() {
this.roleChannel = new BroadcastChannel(this.channelName); this.#roleChannel = new BroadcastChannel(this.channelName);
} }
subscribeToRole(cb) { 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) { if (cb) {
@ -20,21 +21,23 @@ class RoleChannel {
}); });
} }
unsubscribeToRole() { unsubscribeToRole() {
this.roleChannel.close(); this.#roleChannel.close();
} }
reconnect() { reconnect() {
this.roleChannel.close(); this.#roleChannel.close();
this.createRoleChannel(); this.createRoleChannel();
} }
broadcastNewRole(role) { broadcastNewRole(role) {
if (!this.roleChannel.name) {
if (!this.#roleChannel.name) {
return false; return false;
} }
try { try {
this.roleChannel.postMessage(role); this.#roleChannel.postMessage(role);
} catch (e) { } catch (e) {
console.error(e);
this.reconnect(); this.reconnect();
this.broadcastNewRole(role); 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