|
| 1 | +/* -*- indent-tabs-mode: nil; tab-width: 2; -*- */ |
| 2 | +/* vim: set ts=2 sw=2 et ai : */ |
| 3 | +/** |
| 4 | + Container Tab Groups |
| 5 | + Copyright (C) 2023 Menhera.org |
| 6 | +
|
| 7 | + This program is free software: you can redistribute it and/or modify |
| 8 | + it under the terms of the GNU General Public License as published by |
| 9 | + the Free Software Foundation, either version 3 of the License, or |
| 10 | + (at your option) any later version. |
| 11 | +
|
| 12 | + This program is distributed in the hope that it will be useful, |
| 13 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + GNU General Public License for more details. |
| 16 | +
|
| 17 | + You should have received a copy of the GNU General Public License |
| 18 | + along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + @license |
| 20 | +**/ |
| 21 | + |
| 22 | +/** |
| 23 | + * Batched tab removal handler to prevent memory leaks and improve performance |
| 24 | + * when removing multiple tabs in quick succession. |
| 25 | + * |
| 26 | + * This handler debounces tab removal events and processes them in batches, |
| 27 | + * reducing the number of expensive operations and preventing UI freezes. |
| 28 | + */ |
| 29 | +export class BatchedTabRemovalHandler { |
| 30 | + private static instance: BatchedTabRemovalHandler; |
| 31 | + private static readonly DEBOUNCE_DELAY_MS = 100; // 100ms buffer for batching |
| 32 | + |
| 33 | + private pendingRemovals = new Set<number>(); |
| 34 | + private debounceTimer: ReturnType<typeof setTimeout> | null = null; |
| 35 | + |
| 36 | + // Event listener management (simple Observer pattern) |
| 37 | + private listeners: ((tabIds: number[]) => void)[] = []; |
| 38 | + |
| 39 | + private constructor() { |
| 40 | + // Private constructor for singleton |
| 41 | + } |
| 42 | + |
| 43 | + public static getInstance(): BatchedTabRemovalHandler { |
| 44 | + if (!BatchedTabRemovalHandler.instance) { |
| 45 | + BatchedTabRemovalHandler.instance = new BatchedTabRemovalHandler(); |
| 46 | + } |
| 47 | + return BatchedTabRemovalHandler.instance; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Add a tab ID to the pending removal queue. |
| 52 | + * The removal will be processed after the debounce delay. |
| 53 | + */ |
| 54 | + public addRemoval(tabId: number): void { |
| 55 | + this.pendingRemovals.add(tabId); |
| 56 | + this.scheduleProcessing(); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Register a listener to be called when a batch of tabs is removed. |
| 61 | + * The listener will receive an array of tab IDs. |
| 62 | + */ |
| 63 | + public onBatchRemoved(listener: (tabIds: number[]) => void): void { |
| 64 | + this.listeners.push(listener); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Schedule batch processing with debounce. |
| 69 | + * If called multiple times within the debounce delay, the timer is reset. |
| 70 | + */ |
| 71 | + private scheduleProcessing(): void { |
| 72 | + if (this.debounceTimer) { |
| 73 | + clearTimeout(this.debounceTimer); |
| 74 | + } |
| 75 | + this.debounceTimer = setTimeout(() => { |
| 76 | + this.processBatch(); |
| 77 | + }, BatchedTabRemovalHandler.DEBOUNCE_DELAY_MS); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Process the batch of pending tab removals. |
| 82 | + * Notifies all registered listeners with the batch of tab IDs. |
| 83 | + */ |
| 84 | + private processBatch(): void { |
| 85 | + const tabIds = Array.from(this.pendingRemovals); |
| 86 | + this.pendingRemovals.clear(); |
| 87 | + this.debounceTimer = null; |
| 88 | + |
| 89 | + if (tabIds.length > 0) { |
| 90 | + // Notify all listeners |
| 91 | + this.listeners.forEach(listener => listener(tabIds)); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments