|
| 1 | +import { Browser } from "@/browser/browser"; |
| 2 | +import { Tab } from "@/browser/tabs/tab"; |
| 3 | +import { TabGroup } from "@/browser/tabs/tab-group"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Controller responsible for managing tabs within a tab group. |
| 7 | + * Handles adding/removing tabs, maintaining tab state, and cleaning up event listeners. |
| 8 | + */ |
| 9 | +export class TabGroupTabsController { |
| 10 | + /** Reference to the browser instance that owns this tab group */ |
| 11 | + private readonly browser: Browser; |
| 12 | + |
| 13 | + /** Reference to the tab group this controller manages */ |
| 14 | + private readonly tabGroup: TabGroup; |
| 15 | + |
| 16 | + /** Set of tab IDs that belong to this tab group. Uses Set for O(1) operations. */ |
| 17 | + private readonly tabIds: Set<string>; |
| 18 | + |
| 19 | + /** |
| 20 | + * Map of tab ID to array of listener disconnector functions. |
| 21 | + * Used to clean up event listeners when tabs are removed. |
| 22 | + */ |
| 23 | + private readonly tabListenersDisconnectors: Map<string, (() => void)[]> = new Map(); |
| 24 | + |
| 25 | + /** |
| 26 | + * Creates a new TabGroupTabsController instance. |
| 27 | + * @param tabGroup - The tab group this controller will manage |
| 28 | + */ |
| 29 | + constructor(tabGroup: TabGroup) { |
| 30 | + this.browser = tabGroup.creationDetails.browser; |
| 31 | + this.tabGroup = tabGroup; |
| 32 | + |
| 33 | + // Initialize empty set of tab IDs |
| 34 | + this.tabIds = new Set(); |
| 35 | + |
| 36 | + // Setup event listeners for focused tab |
| 37 | + tabGroup.connect("tab-removed", () => { |
| 38 | + if (this.tabIds.size === 0) { |
| 39 | + // Destroy the tab group |
| 40 | + this.tabGroup.destroy(); |
| 41 | + } |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Adds a tab to this tab group. |
| 47 | + * Sets up event listeners and emits the "tab-added" event. |
| 48 | + * Respects the maximum number of tabs allowed in the group. |
| 49 | + * |
| 50 | + * @param tab - The tab to add to the group |
| 51 | + * @returns true if the tab was added successfully, false if it was already in the group or would exceed the maximum tab limit |
| 52 | + */ |
| 53 | + public addTab(tab: Tab) { |
| 54 | + // Check if tab is already in this group |
| 55 | + const hasTab = this.tabIds.has(tab.id); |
| 56 | + if (hasTab) { |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + // Check if adding this tab would exceed the maximum allowed tabs |
| 61 | + // -1 means no limit |
| 62 | + if (this.tabGroup.maxTabs !== -1 && this.tabIds.size >= this.tabGroup.maxTabs) { |
| 63 | + return false; |
| 64 | + } |
| 65 | + |
| 66 | + // Add tab ID to our set |
| 67 | + this.tabIds.add(tab.id); |
| 68 | + |
| 69 | + // Setup event listeners for tab lifecycle management |
| 70 | + const disconnectDestroyListener = tab.connect("destroyed", () => { |
| 71 | + this.removeTab(tab); |
| 72 | + }); |
| 73 | + |
| 74 | + const disconnectFocusedListener = tab.connect("focused", () => { |
| 75 | + this.tabGroup.focusedTab.set(tab); |
| 76 | + }); |
| 77 | + |
| 78 | + // Store the disconnector function for cleanup later |
| 79 | + this.tabListenersDisconnectors.set(tab.id, [disconnectDestroyListener, disconnectFocusedListener]); |
| 80 | + |
| 81 | + // Notify tab group that a new tab was added |
| 82 | + this.tabGroup.emit("tab-added", tab); |
| 83 | + return true; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Removes a tab from this tab group. |
| 88 | + * Cleans up event listeners and emits the "tab-removed" event. |
| 89 | + * |
| 90 | + * @param tab - The tab to remove from the group |
| 91 | + * @returns true if the tab was removed successfully, false if it wasn't in the group |
| 92 | + */ |
| 93 | + public removeTab(tab: Tab) { |
| 94 | + // Check if tab exists in this group |
| 95 | + const hasTab = this.tabIds.has(tab.id); |
| 96 | + if (!hasTab) { |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + // Remove tab ID from our set |
| 101 | + this.tabIds.delete(tab.id); |
| 102 | + |
| 103 | + // Clean up event listeners to prevent memory leaks |
| 104 | + const disconnectors = this.tabListenersDisconnectors.get(tab.id); |
| 105 | + if (disconnectors) { |
| 106 | + disconnectors.forEach((disconnector) => { |
| 107 | + disconnector(); |
| 108 | + }); |
| 109 | + } |
| 110 | + this.tabListenersDisconnectors.delete(tab.id); |
| 111 | + |
| 112 | + // Notify tab group that a tab was removed |
| 113 | + this.tabGroup.emit("tab-removed", tab); |
| 114 | + return true; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Gets all tabs that belong to this tab group. |
| 119 | + * Filters out any tabs that may have been destroyed but not properly cleaned up. |
| 120 | + * |
| 121 | + * @returns Array of Tab instances that are currently in this group |
| 122 | + */ |
| 123 | + public get(): Tab[] { |
| 124 | + const tabOrchestrator = this.browser.tabs; |
| 125 | + |
| 126 | + // Convert Set to Array and map tab IDs to actual Tab instances |
| 127 | + const tabs = Array.from(this.tabIds).map((id) => { |
| 128 | + return tabOrchestrator.tabManager.getTabById(id); |
| 129 | + }); |
| 130 | + |
| 131 | + // Filter out any undefined tabs (in case a tab was destroyed but not properly removed) |
| 132 | + return tabs.filter((tab) => tab !== undefined); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Cleans up all event listeners for all tabs in this group. |
| 137 | + * Should be called when the tab group is being destroyed to prevent memory leaks. |
| 138 | + */ |
| 139 | + public cleanupListeners() { |
| 140 | + // Disconnect all event listeners |
| 141 | + this.tabListenersDisconnectors.forEach((disconnectors) => { |
| 142 | + disconnectors.forEach((disconnector) => { |
| 143 | + disconnector(); |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + // Clear the disconnectors map |
| 148 | + this.tabListenersDisconnectors.clear(); |
| 149 | + } |
| 150 | +} |
0 commit comments