|
| 1 | +/** |
| 2 | + * @file Long Task & Long Animation Frame (LoAF) tracking via PerformanceObserver |
| 3 | + * |
| 4 | + * Both APIs are Chromium-only (Chrome, Edge). |
| 5 | + * - Long Tasks: tasks blocking the main thread for >50 ms |
| 6 | + * - LoAF (Chrome 123+): richer attribution per long animation frame |
| 7 | + * |
| 8 | + * Sets up observers and fires `onEntry` per detected entry — fire and forget. |
| 9 | + */ |
| 10 | + |
| 11 | +import type { EventContext, Json } from '@hawk.so/types'; |
| 12 | +import log from '../utils/log'; |
| 13 | + |
| 14 | +/** |
| 15 | + * Configuration for main-thread blocking detection |
| 16 | + * |
| 17 | + * Both features are Chromium-only (Chrome, Edge). |
| 18 | + * Feature detection is performed automatically — on unsupported browsers |
| 19 | + * the observers simply won't start. |
| 20 | + */ |
| 21 | +export interface MainThreadBlockingOptions { |
| 22 | + /** |
| 23 | + * Track Long Tasks (tasks blocking the main thread for >50 ms). |
| 24 | + * Uses PerformanceObserver with `longtask` entry type. |
| 25 | + * |
| 26 | + * Chromium-only (Chrome, Edge) |
| 27 | + * |
| 28 | + * @default true |
| 29 | + */ |
| 30 | + longTasks?: boolean; |
| 31 | + |
| 32 | + /** |
| 33 | + * Track Long Animation Frames (LoAF) — frames taking >50 ms. |
| 34 | + * Provides richer attribution data than Long Tasks. |
| 35 | + * Uses PerformanceObserver with `long-animation-frame` entry type. |
| 36 | + * |
| 37 | + * Chromium-only (Chrome 123+, Edge 123+) |
| 38 | + * |
| 39 | + * @default true |
| 40 | + */ |
| 41 | + longAnimationFrames?: boolean; |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Payload passed to the callback when a long task / LoAF is detected |
| 46 | + */ |
| 47 | +export interface LongTaskEvent { |
| 48 | + title: string; |
| 49 | + context: EventContext; |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * LoAF entry shape (spec is still evolving) |
| 54 | + */ |
| 55 | +interface LoAFEntry extends PerformanceEntry { |
| 56 | + blockingDuration?: number; |
| 57 | + scripts?: { |
| 58 | + name: string; |
| 59 | + invoker?: string; |
| 60 | + invokerType?: string; |
| 61 | + sourceURL?: string; |
| 62 | + duration: number; |
| 63 | + }[]; |
| 64 | +} |
| 65 | + |
| 66 | +function supportsEntryType(type: string): boolean { |
| 67 | + try { |
| 68 | + return ( |
| 69 | + typeof PerformanceObserver !== 'undefined' && |
| 70 | + typeof PerformanceObserver.supportedEntryTypes !== 'undefined' && |
| 71 | + PerformanceObserver.supportedEntryTypes.includes(type) |
| 72 | + ); |
| 73 | + } catch { |
| 74 | + return false; |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +function observeLongTasks(onEntry: (e: LongTaskEvent) => void): void { |
| 79 | + if (!supportsEntryType('longtask')) { |
| 80 | + log('Long Tasks API is not supported in this browser', 'info'); |
| 81 | + |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + try { |
| 86 | + new PerformanceObserver((list) => { |
| 87 | + for (const entry of list.getEntries()) { |
| 88 | + const durationMs = Math.round(entry.duration); |
| 89 | + |
| 90 | + onEntry({ |
| 91 | + title: `Long Task ${durationMs} ms`, |
| 92 | + context: { |
| 93 | + kind: 'longtask', |
| 94 | + startTime: Math.round(entry.startTime), |
| 95 | + durationMs, |
| 96 | + }, |
| 97 | + }); |
| 98 | + } |
| 99 | + }).observe({ type: 'longtask', buffered: true }); |
| 100 | + } catch { /* unsupported — ignore */ } |
| 101 | +} |
| 102 | + |
| 103 | +function observeLoAF(onEntry: (e: LongTaskEvent) => void): void { |
| 104 | + if (!supportsEntryType('long-animation-frame')) { |
| 105 | + log('Long Animation Frames (LoAF) API is not supported in this browser', 'info'); |
| 106 | + |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + try { |
| 111 | + new PerformanceObserver((list) => { |
| 112 | + for (const entry of list.getEntries()) { |
| 113 | + const loaf = entry as LoAFEntry; |
| 114 | + const durationMs = Math.round(loaf.duration); |
| 115 | + const blockingDurationMs = loaf.blockingDuration != null |
| 116 | + ? Math.round(loaf.blockingDuration) |
| 117 | + : undefined; |
| 118 | + |
| 119 | + const scripts = loaf.scripts |
| 120 | + ?.filter((s) => s.sourceURL) |
| 121 | + .reduce<Record<string, Json>>((acc, s, i) => { |
| 122 | + acc[`script_${i}`] = { |
| 123 | + name: s.name, |
| 124 | + invoker: s.invoker ?? '', |
| 125 | + invokerType: s.invokerType ?? '', |
| 126 | + sourceURL: s.sourceURL ?? '', |
| 127 | + duration: Math.round(s.duration), |
| 128 | + }; |
| 129 | + |
| 130 | + return acc; |
| 131 | + }, {}); |
| 132 | + |
| 133 | + const blockingNote = blockingDurationMs != null |
| 134 | + ? ` (blocking ${blockingDurationMs} ms)` |
| 135 | + : ''; |
| 136 | + |
| 137 | + const context: EventContext = { |
| 138 | + kind: 'loaf', |
| 139 | + startTime: Math.round(loaf.startTime), |
| 140 | + durationMs, |
| 141 | + }; |
| 142 | + |
| 143 | + if (blockingDurationMs != null) { |
| 144 | + context.blockingDurationMs = blockingDurationMs; |
| 145 | + } |
| 146 | + |
| 147 | + if (scripts && Object.keys(scripts).length > 0) { |
| 148 | + context.scripts = scripts; |
| 149 | + } |
| 150 | + |
| 151 | + onEntry({ |
| 152 | + title: `Long Animation Frame ${durationMs} ms${blockingNote}`, |
| 153 | + context, |
| 154 | + }); |
| 155 | + } |
| 156 | + }).observe({ type: 'long-animation-frame', buffered: true }); |
| 157 | + } catch { /* unsupported — ignore */ } |
| 158 | +} |
| 159 | + |
| 160 | +/** |
| 161 | + * Set up observers for main-thread blocking detection. |
| 162 | + * Each detected entry fires `onEntry` immediately. |
| 163 | + */ |
| 164 | +export function observeMainThreadBlocking( |
| 165 | + options: MainThreadBlockingOptions, |
| 166 | + onEntry: (e: LongTaskEvent) => void |
| 167 | +): void { |
| 168 | + if (options.longTasks ?? true) { |
| 169 | + observeLongTasks(onEntry); |
| 170 | + } |
| 171 | + |
| 172 | + if (options.longAnimationFrames ?? true) { |
| 173 | + observeLoAF(onEntry); |
| 174 | + } |
| 175 | +} |
0 commit comments