| 1609 | }> = [] |
| 1610 | |
| 1611 | export function getSlowOperations(): ReadonlyArray<{ |
| 1612 | operation: string |
| 1613 | durationMs: number |
| 1614 | timestamp: number |
| 1615 | }> { |
| 1616 | // Most common case: nothing tracked. Return a stable reference so the |
| 1617 | // caller's setState() can bail via Object.is instead of re-rendering at 2fps. |
| 1618 | if (STATE.slowOperations.length === 0) { |
| 1619 | return EMPTY_SLOW_OPERATIONS |
| 1620 | } |
| 1621 | const now = Date.now() |
| 1622 | // Only allocate a new array when something actually expired; otherwise keep |
| 1623 | // the reference stable across polls while ops are still fresh. |
| 1624 | if ( |
| 1625 | STATE.slowOperations.some(op => now - op.timestamp >= SLOW_OPERATION_TTL_MS) |
| 1626 | ) { |
| 1627 | STATE.slowOperations = STATE.slowOperations.filter( |
| 1628 | op => now - op.timestamp < SLOW_OPERATION_TTL_MS, |
| 1629 | ) |
| 1630 | if (STATE.slowOperations.length === 0) { |
| 1631 | return EMPTY_SLOW_OPERATIONS |
| 1632 | } |
| 1633 | } |
| 1634 | // Safe to return directly: addSlowOperation() reassigns STATE.slowOperations |
| 1635 | // before pushing, so the array held in React state is never mutated. |
| 1636 | return STATE.slowOperations |
| 1637 | } |
| 1638 | |
| 1639 | export function getMainThreadAgentType(): string | undefined { |
| 1640 | return STATE.mainThreadAgentType |