|
| 1 | +/** |
| 2 | + * Frame Lifecycle Hooks |
| 3 | + * Allows external modules to subscribe to frame events without coupling to FrameManager |
| 4 | + */ |
| 5 | + |
| 6 | +import { logger } from '../monitoring/logger.js'; |
| 7 | +import type { Frame, Event, Anchor } from './frame-manager.js'; |
| 8 | + |
| 9 | +/** |
| 10 | + * Data passed to frame close hooks |
| 11 | + */ |
| 12 | +export interface FrameCloseData { |
| 13 | + frame: Frame; |
| 14 | + events: Event[]; |
| 15 | + anchors: Anchor[]; |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Hook function type for frame close events |
| 20 | + */ |
| 21 | +export type FrameCloseHook = (data: FrameCloseData) => Promise<void>; |
| 22 | + |
| 23 | +/** |
| 24 | + * Hook function type for frame create events |
| 25 | + */ |
| 26 | +export type FrameCreateHook = (frame: Frame) => Promise<void>; |
| 27 | + |
| 28 | +/** |
| 29 | + * Registered hook with metadata |
| 30 | + */ |
| 31 | +interface RegisteredHook<T> { |
| 32 | + name: string; |
| 33 | + handler: T; |
| 34 | + priority: number; |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Frame Lifecycle Hooks Registry |
| 39 | + * Singleton that manages all frame lifecycle hooks |
| 40 | + */ |
| 41 | +class FrameLifecycleHooksRegistry { |
| 42 | + private closeHooks: RegisteredHook<FrameCloseHook>[] = []; |
| 43 | + private createHooks: RegisteredHook<FrameCreateHook>[] = []; |
| 44 | + |
| 45 | + /** |
| 46 | + * Register a hook to be called when a frame is closed |
| 47 | + * @param name - Unique name for the hook (for logging/debugging) |
| 48 | + * @param handler - Async function to call when frame closes |
| 49 | + * @param priority - Higher priority hooks run first (default: 0) |
| 50 | + */ |
| 51 | + onFrameClosed( |
| 52 | + name: string, |
| 53 | + handler: FrameCloseHook, |
| 54 | + priority: number = 0 |
| 55 | + ): () => void { |
| 56 | + const hook: RegisteredHook<FrameCloseHook> = { name, handler, priority }; |
| 57 | + this.closeHooks.push(hook); |
| 58 | + this.closeHooks.sort((a, b) => b.priority - a.priority); |
| 59 | + |
| 60 | + logger.debug('Registered frame close hook', { name, priority }); |
| 61 | + |
| 62 | + // Return unregister function |
| 63 | + return () => { |
| 64 | + this.closeHooks = this.closeHooks.filter((h) => h !== hook); |
| 65 | + logger.debug('Unregistered frame close hook', { name }); |
| 66 | + }; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Register a hook to be called when a frame is created |
| 71 | + * @param name - Unique name for the hook (for logging/debugging) |
| 72 | + * @param handler - Async function to call when frame is created |
| 73 | + * @param priority - Higher priority hooks run first (default: 0) |
| 74 | + */ |
| 75 | + onFrameCreated( |
| 76 | + name: string, |
| 77 | + handler: FrameCreateHook, |
| 78 | + priority: number = 0 |
| 79 | + ): () => void { |
| 80 | + const hook: RegisteredHook<FrameCreateHook> = { name, handler, priority }; |
| 81 | + this.createHooks.push(hook); |
| 82 | + this.createHooks.sort((a, b) => b.priority - a.priority); |
| 83 | + |
| 84 | + logger.debug('Registered frame create hook', { name, priority }); |
| 85 | + |
| 86 | + // Return unregister function |
| 87 | + return () => { |
| 88 | + this.createHooks = this.createHooks.filter((h) => h !== hook); |
| 89 | + logger.debug('Unregistered frame create hook', { name }); |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Trigger all close hooks (called by FrameManager) |
| 95 | + * Hooks are fire-and-forget - errors don't affect frame closure |
| 96 | + */ |
| 97 | + async triggerClose(data: FrameCloseData): Promise<void> { |
| 98 | + if (this.closeHooks.length === 0) return; |
| 99 | + |
| 100 | + const results = await Promise.allSettled( |
| 101 | + this.closeHooks.map(async (hook) => { |
| 102 | + try { |
| 103 | + await hook.handler(data); |
| 104 | + } catch (error) { |
| 105 | + logger.warn(`Frame close hook "${hook.name}" failed`, { |
| 106 | + error: error instanceof Error ? error.message : String(error), |
| 107 | + frameId: data.frame.frame_id, |
| 108 | + frameName: data.frame.name, |
| 109 | + }); |
| 110 | + } |
| 111 | + }) |
| 112 | + ); |
| 113 | + |
| 114 | + const failed = results.filter((r) => r.status === 'rejected').length; |
| 115 | + if (failed > 0) { |
| 116 | + logger.debug('Some frame close hooks failed', { |
| 117 | + total: this.closeHooks.length, |
| 118 | + failed, |
| 119 | + frameId: data.frame.frame_id, |
| 120 | + }); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Trigger all create hooks (called by FrameManager) |
| 126 | + * Hooks are fire-and-forget - errors don't affect frame creation |
| 127 | + */ |
| 128 | + async triggerCreate(frame: Frame): Promise<void> { |
| 129 | + if (this.createHooks.length === 0) return; |
| 130 | + |
| 131 | + const results = await Promise.allSettled( |
| 132 | + this.createHooks.map(async (hook) => { |
| 133 | + try { |
| 134 | + await hook.handler(frame); |
| 135 | + } catch (error) { |
| 136 | + logger.warn(`Frame create hook "${hook.name}" failed`, { |
| 137 | + error: error instanceof Error ? error.message : String(error), |
| 138 | + frameId: frame.frame_id, |
| 139 | + frameName: frame.name, |
| 140 | + }); |
| 141 | + } |
| 142 | + }) |
| 143 | + ); |
| 144 | + |
| 145 | + const failed = results.filter((r) => r.status === 'rejected').length; |
| 146 | + if (failed > 0) { |
| 147 | + logger.debug('Some frame create hooks failed', { |
| 148 | + total: this.createHooks.length, |
| 149 | + failed, |
| 150 | + frameId: frame.frame_id, |
| 151 | + }); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Get count of registered hooks (useful for testing) |
| 157 | + */ |
| 158 | + getHookCounts(): { close: number; create: number } { |
| 159 | + return { |
| 160 | + close: this.closeHooks.length, |
| 161 | + create: this.createHooks.length, |
| 162 | + }; |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Clear all hooks (useful for testing) |
| 167 | + */ |
| 168 | + clearAll(): void { |
| 169 | + this.closeHooks = []; |
| 170 | + this.createHooks = []; |
| 171 | + logger.debug('Cleared all frame lifecycle hooks'); |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +/** |
| 176 | + * Singleton instance of the hooks registry |
| 177 | + */ |
| 178 | +export const frameLifecycleHooks = new FrameLifecycleHooksRegistry(); |
0 commit comments