From 75f334f4ae7520467b73acc53ae9f04b8aee519a Mon Sep 17 00:00:00 2001 From: hensm Date: Sun, 4 Sep 2022 07:32:42 +0100 Subject: [PATCH] Refresh device manager with options bridge status and propagate to popup --- ext/src/background/ReceiverSelector.ts | 12 ++++--- ext/src/background/background.ts | 14 ++++++-- ext/src/background/castManager.ts | 6 +++- ext/src/background/deviceManager.ts | 21 ++++++++---- ext/src/messaging.ts | 44 +++++++++++++++----------- ext/src/ui/options/Bridge.svelte | 11 ++++--- ext/src/ui/popup/Popup.svelte | 3 +- ext/src/ui/popup/styles/index.css | 1 + 8 files changed, 75 insertions(+), 37 deletions(-) diff --git a/ext/src/background/ReceiverSelector.ts b/ext/src/background/ReceiverSelector.ts index 1e6805a..60e9fa0 100644 --- a/ext/src/background/ReceiverSelector.ts +++ b/ext/src/background/ReceiverSelector.ts @@ -149,11 +149,15 @@ export default class ReceiverSelector extends TypedEventTarget { + 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."); diff --git a/ext/src/background/castManager.ts b/ext/src/background/castManager.ts index 7da48e0..bdbb26c 100644 --- a/ext/src/background/castManager.ts +++ b/ext/src/background/castManager.ts @@ -761,7 +761,11 @@ function createSelector() { } } - selector.update(deviceManager.getDevices(), connectedSessionIds); + selector.update( + deviceManager.getDevices(), + deviceManager.getBridgeInfo()?.isVersionCompatible ?? false, + connectedSessionIds + ); }; deviceManager.addEventListener("deviceUp", onDeviceChange); diff --git a/ext/src/background/deviceManager.ts b/ext/src/background/deviceManager.ts index cc7ce78..d9ba0fa 100644 --- a/ext/src/background/deviceManager.ts +++ b/ext/src/background/deviceManager.ts @@ -47,7 +47,6 @@ export default new (class extends TypedEventTarget { */ async refresh() { this.bridgePort?.disconnect(); - this.receiverDevices.clear(); try { this.bridgeInfo = await bridge.getInfo(); @@ -218,20 +217,28 @@ export default new (class extends TypedEventTarget { }; 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); }; })(); diff --git a/ext/src/messaging.ts b/ext/src/messaging.ts index 0a2b81d..b59f84c 100644 --- a/ext/src/messaging.ts +++ b/ext/src/messaging.ts @@ -46,11 +46,11 @@ type ExtMessageDefinitions = { "popup:init": { appInfo?: ReceiverSelectorAppInfo; pageInfo?: ReceiverSelectorPageInfo; - isBridgeCompatible: boolean; }; /** Updates selector popup with new data. */ "popup:update": { devices: ReceiverDevice[]; + isBridgeCompatible: boolean; connectedSessionIds?: string[]; defaultMediaType?: ReceiverSelectorMediaType; availableMediaTypes?: ReceiverSelectorMediaType; @@ -102,6 +102,12 @@ type ExtMessageDefinitions = { "main:sendReceiverMessage": ReceiverSelectorReceiverMessage; /** Allows the selector popup to send cast NS_MEDIA messages. */ "main:sendMediaMessage": ReceiverSelectorMediaMessage; + + /** + * Tells the device manager to clear its device list and re-connect + * to the bridge. + */ + "main:refreshDeviceManager": void; }; /** @@ -319,21 +325,23 @@ export default new (class Messenger { return browser.tabs.connect(tabId, connectInfo) as Port; } - onConnect = { - addListener(cb: (port: Port) => void) { - browser.runtime.onConnect.addListener( - cb as (port: browser.runtime.Port) => void - ); - }, - removeListener(cb: (port: Port) => void) { - browser.runtime.onConnect.removeListener( - cb as (port: browser.runtime.Port) => void - ); - }, - hasListener(cb: (port: Port) => void) { - return browser.runtime.onConnect.hasListener( - cb as (port: browser.runtime.Port) => void - ); - } - }; + sendMessage( + message: Message, + options?: browser.runtime._SendMessageOptions + ): Promise; + sendMessage( + extensionId: string, + options?: browser.runtime._SendMessageOptions + ): Promise; + sendMessage( + messageOrExtensionId: string | Message, + options?: browser.runtime._SendMessageOptions + ) { + return browser.runtime.sendMessage(messageOrExtensionId, options); + } + + onConnect = browser.runtime.onConnect as WebExtEvent<(port: Port) => void>; + onMessage = browser.runtime.onMessage as WebExtEvent< + (message: Message, sender: browser.runtime.MessageSender) => void + >; })(); diff --git a/ext/src/ui/options/Bridge.svelte b/ext/src/ui/options/Bridge.svelte index 13d20c9..b67082f 100644 --- a/ext/src/ui/options/Bridge.svelte +++ b/ext/src/ui/options/Bridge.svelte @@ -10,9 +10,10 @@ BridgeAuthenticationError } from "../../lib/bridge"; import logger from "../../lib/logger"; - import type { Options } from "../../lib/options"; + import messaging from "../../messaging"; + const _ = browser.i18n.getMessage; export let opts: Options; @@ -26,13 +27,15 @@ let statusTitle: string; let statusText: Nullable = null; - async function checkBridgeStatus() { + async function refreshBridgeStatus() { // Reset state bridgeInfo = null; bridgeInfoError = null; isLoadingInfo = true; statusText = null; + messaging.sendMessage({ subject: "main:refreshDeviceManager" }); + try { bridgeInfo = await bridge.getInfo(); } catch (err) { @@ -78,7 +81,7 @@ } onMount(() => { - checkBridgeStatus(); + refreshBridgeStatus(); }); // Updates @@ -197,7 +200,7 @@ type="button" class="ghost bridge__refresh" title={_("optionsBridgeRefresh")} - on:click={checkBridgeStatus} + on:click={refreshBridgeStatus} /> diff --git a/ext/src/ui/popup/Popup.svelte b/ext/src/ui/popup/Popup.svelte index 4560f87..4cff72e 100644 --- a/ext/src/ui/popup/Popup.svelte +++ b/ext/src/ui/popup/Popup.svelte @@ -174,10 +174,11 @@ case "popup:init": appInfo = message.data.appInfo; pageInfo = message.data.pageInfo; - isBridgeCompatible = message.data.isBridgeCompatible; break; case "popup:update": { + isBridgeCompatible = message.data.isBridgeCompatible; + updateKnownApp(); if ( diff --git a/ext/src/ui/popup/styles/index.css b/ext/src/ui/popup/styles/index.css index af264d9..907361f 100755 --- a/ext/src/ui/popup/styles/index.css +++ b/ext/src/ui/popup/styles/index.css @@ -92,6 +92,7 @@ body { .receiver-list__not-found { align-items: center; + color: var(--secondary-color); display: flex; height: 50px; justify-content: center;