mirror of
https://github.com/hensm/fx_cast.git
synced 2026-06-12 02:29:59 +00:00
Restructure background script (#70)
Splits some background script functionality into separate modules: - Receiver selector handling is moved to ./SelectorManager. - Status bridge handling is moved to ./StatusManager. - Menu creation and updates are handled in ./createMenus. - Shim creation is handled in ./createShim. TypedEventTarget allows EventTarget-derived classes to export typed events. Options type definition is moved to ./lib/options, module assumes more responsibility for update handling and provides a "changed" event. Private cast._requestSession method allows bypassing receiver selector.
This commit is contained in:
@@ -1,19 +1,23 @@
|
||||
"use strict";
|
||||
|
||||
import ReceiverSelector, {
|
||||
ReceiverSelectorMediaType } from "./ReceiverSelector";
|
||||
ReceiverSelectorEvents
|
||||
, ReceiverSelectorMediaType } from "./ReceiverSelector";
|
||||
|
||||
import { TypedEventTarget } from "../lib/typedEvents";
|
||||
import { getWindowCenteredProps } from "../lib/utils";
|
||||
import { Message, Receiver } from "../types";
|
||||
|
||||
|
||||
export default class PopupReceiverSelector
|
||||
extends EventTarget
|
||||
extends TypedEventTarget<ReceiverSelectorEvents>
|
||||
implements ReceiverSelector {
|
||||
|
||||
private windowId: number;
|
||||
private openerWindowId: number;
|
||||
|
||||
private messagePort: browser.runtime.Port;
|
||||
private messagePortDisconnected: boolean;
|
||||
|
||||
private receivers: Receiver[];
|
||||
private defaultMediaType: ReceiverSelectorMediaType;
|
||||
@@ -21,6 +25,8 @@ export default class PopupReceiverSelector
|
||||
|
||||
private wasReceiverSelected: boolean = false;
|
||||
|
||||
private _isOpen: boolean = false;
|
||||
|
||||
|
||||
constructor () {
|
||||
super();
|
||||
@@ -48,6 +54,9 @@ export default class PopupReceiverSelector
|
||||
|
||||
this.messagePort = port;
|
||||
this.messagePort.onMessage.addListener(this.onPopupMessage);
|
||||
this.messagePort.onDisconnect.addListener(() => {
|
||||
this.messagePortDisconnected = true;
|
||||
});
|
||||
|
||||
this.messagePort.postMessage({
|
||||
subject: "popup:/populateReceiverList"
|
||||
@@ -60,6 +69,9 @@ export default class PopupReceiverSelector
|
||||
});
|
||||
}
|
||||
|
||||
get isOpen () {
|
||||
return this._isOpen;
|
||||
}
|
||||
|
||||
public async open (
|
||||
receivers: Receiver[]
|
||||
@@ -85,6 +97,8 @@ export default class PopupReceiverSelector
|
||||
, ...centeredProps
|
||||
});
|
||||
|
||||
this._isOpen = true;
|
||||
|
||||
this.windowId = popup.id;
|
||||
this.openerWindowId = openerWindow.id;
|
||||
|
||||
@@ -98,8 +112,16 @@ export default class PopupReceiverSelector
|
||||
this.onWindowsFocusChanged);
|
||||
}
|
||||
|
||||
public close (): void {
|
||||
browser.windows.remove(this.windowId);
|
||||
public async close (): Promise<void> {
|
||||
if (this.windowId) {
|
||||
await browser.windows.remove(this.windowId);
|
||||
}
|
||||
|
||||
this._isOpen = false;
|
||||
|
||||
if (this.messagePort && !this.messagePortDisconnected) {
|
||||
this.messagePort.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user