| 24 | * Provides change detection, debouncing, batching, and memory management |
| 25 | */ |
| 26 | export class ViewPerformanceService { |
| 27 | private plugin: ViewPerformanceServiceContext; |
| 28 | |
| 29 | // Global task version cache shared across all views |
| 30 | private globalTaskVersionCache = new Map<string, string>(); |
| 31 | private lastGlobalRefreshTime = 0; |
| 32 | private globalTaskCount = 0; |
| 33 | |
| 34 | // View-specific state |
| 35 | private viewHandlers = new Map<string, ViewUpdateHandler>(); |
| 36 | private viewDebounceTimers = new Map<string, number>(); |
| 37 | private viewPendingUpdates = new Map<string, Set<string>>(); |
| 38 | private viewConfigs = new Map<string, ViewPerformanceConfig>(); |
| 39 | |
| 40 | // Event coordination |
| 41 | private updateInProgress = new Set<string>(); |
| 42 | private eventListener: unknown = null; |
| 43 | |
| 44 | constructor(plugin: ViewPerformanceServiceContext) { |
| 45 | this.plugin = plugin; |
| 46 | this.setupGlobalEventListener(); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Register a view with the performance service |
| 51 | */ |
| 52 | registerView(config: ViewPerformanceConfig, handler: ViewUpdateHandler): void { |
| 53 | this.viewConfigs.set(config.viewId, { |
| 54 | debounceDelay: 100, |
| 55 | maxBatchSize: 5, |
| 56 | changeDetectionEnabled: true, |
| 57 | ...config, |
| 58 | }); |
| 59 | this.viewHandlers.set(config.viewId, handler); |
| 60 | this.viewPendingUpdates.set(config.viewId, new Set()); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Unregister a view from the performance service |
| 65 | */ |
| 66 | unregisterView(viewId: string): void { |
| 67 | // Clean up debounce timer |
| 68 | const timer = this.viewDebounceTimers.get(viewId); |
| 69 | if (timer) { |
| 70 | window.clearTimeout(timer); |
| 71 | this.viewDebounceTimers.delete(viewId); |
| 72 | } |
| 73 | |
| 74 | // Clean up state |
| 75 | this.viewConfigs.delete(viewId); |
| 76 | this.viewHandlers.delete(viewId); |
| 77 | this.viewPendingUpdates.delete(viewId); |
| 78 | this.updateInProgress.delete(viewId); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Setup global event listener for task updates |
| 83 | */ |
nothing calls this directly
no outgoing calls
no test coverage detected