Refresh device manager with options bridge status and propagate to popup

This commit is contained in:
hensm
2022-09-04 07:32:42 +01:00
parent 25bd72f0fe
commit 75f334f4ae
8 changed files with 75 additions and 37 deletions

View File

@@ -149,11 +149,15 @@ export default class ReceiverSelector extends TypedEventTarget<ReceiverSelectorE
}
/** Updates receiver devices displayed in the receiver selector. */
public update(devices: ReceiverDevice[], connectedSessionIds: string[]) {
public update(
devices: ReceiverDevice[],
isBridgeCompatible: boolean,
connectedSessionIds: string[]
) {
this.devices = devices;
this.messagePort?.postMessage({
subject: "popup:update",
data: { devices, connectedSessionIds }
data: { devices, isBridgeCompatible, connectedSessionIds }
});
}
@@ -205,8 +209,7 @@ export default class ReceiverSelector extends TypedEventTarget<ReceiverSelectorE
subject: "popup:init",
data: {
appInfo: this.appInfo,
pageInfo: this.pageInfo,
isBridgeCompatible: this.isBridgeCompatible
pageInfo: this.pageInfo
}
});
@@ -214,6 +217,7 @@ export default class ReceiverSelector extends TypedEventTarget<ReceiverSelectorE
subject: "popup:update",
data: {
devices: this.devices,
isBridgeCompatible: this.isBridgeCompatible,
defaultMediaType: this.defaultMediaType,
availableMediaTypes: this.availableMediaTypes
}

View File

@@ -1,14 +1,16 @@
import defaultOptions from "../defaultOptions";
import logger from "../lib/logger";
import options from "../lib/options";
import bridge, { BridgeInfo } from "../lib/bridge";
import { baseConfigStorage, fetchBaseConfig } from "../lib/chromecastConfigApi";
import defaultOptions from "../defaultOptions";
import messaging from "../messaging";
import castManager from "./castManager";
import deviceManager from "./deviceManager";
import { initMenus } from "./menus";
import { initWhitelist } from "./whitelist";
import { baseConfigStorage, fetchBaseConfig } from "../lib/chromecastConfigApi";
const _ = browser.i18n.getMessage;
@@ -130,6 +132,14 @@ async function init() {
await initMenus();
await initWhitelist();
messaging.onMessage.addListener(message => {
switch (message.subject) {
case "main:refreshDeviceManager":
deviceManager.refresh();
break;
}
});
browser.browserAction.onClicked.addListener(async tab => {
if (tab.id === undefined) {
logger.error("Tab ID not found in browser action handler.");

View File

@@ -761,7 +761,11 @@ function createSelector() {
}
}
selector.update(deviceManager.getDevices(), connectedSessionIds);
selector.update(
deviceManager.getDevices(),
deviceManager.getBridgeInfo()?.isVersionCompatible ?? false,
connectedSessionIds
);
};
deviceManager.addEventListener("deviceUp", onDeviceChange);

View File

@@ -47,7 +47,6 @@ export default new (class extends TypedEventTarget<EventMap> {
*/
async refresh() {
this.bridgePort?.disconnect();
this.receiverDevices.clear();
try {
this.bridgeInfo = await bridge.getInfo();
@@ -218,20 +217,28 @@ export default new (class extends TypedEventTarget<EventMap> {
};
private onBridgeDisconnect = () => {
const deviceIds = [...this.receiverDevices.keys()];
delete this.bridgeInfo;
this.receiverDevices.clear();
// Notify listeners of device availablility
for (const [, receiverDevice] of this.receiverDevices) {
for (const deviceId of deviceIds) {
const event = new CustomEvent("deviceDown", {
detail: { deviceId: receiverDevice.id }
detail: { deviceId }
});
this.dispatchEvent(event);
}
this.receiverDevices.clear();
// Re-initialize after 10 seconds
/**
* Reconnect 10 seconds after disconnect if not already
* reconnected (like immediately after a refresh).
*/
window.setTimeout(() => {
this.refresh();
if (!this.bridgeInfo) {
this.refresh();
}
}, 10000);
};
})();