55 * than how long a question sat on screen.
66 */
77
8- let excludedMs = 0
8+ /** Closed pauses, as `[start, end]`. Only a handful happen in a session. */
9+ const pauses : Array < [ number , number ] > = [ ]
910let pausedAt : number | undefined
1011let depth = 0
1112
@@ -17,7 +18,7 @@ function pauseStartupClock(): void {
1718
1819function resumeStartupClock ( ) : void {
1920 if ( depth > 0 && -- depth === 0 && pausedAt !== undefined ) {
20- excludedMs += Date . now ( ) - pausedAt
21+ pauses . push ( [ pausedAt , Date . now ( ) ] )
2122 pausedAt = undefined
2223 }
2324}
@@ -33,8 +34,16 @@ export async function withStartupClockPaused<T>(work: () => Promise<T>): Promise
3334 }
3435}
3536
36- /** Milliseconds since `since`, not counting paused stretches. */
37+ /**
38+ * Milliseconds since `since`, not counting paused stretches.
39+ *
40+ * Only the part of a pause that falls after `since` is subtracted, so a
41+ * baseline taken after a prompt is not charged for the time it took to answer.
42+ */
3743export function startupElapsedMs ( since : number ) : number {
38- const open = pausedAt === undefined ? 0 : Date . now ( ) - pausedAt
39- return Math . max ( 0 , Date . now ( ) - since - excludedMs - open )
44+ const now = Date . now ( )
45+ const open = pausedAt === undefined ? [ ] : [ [ pausedAt , now ] as [ number , number ] ]
46+ const excluded = [ ...pauses , ...open ]
47+ . reduce ( ( total , [ start , end ] ) => total + Math . max ( 0 , Math . min ( end , now ) - Math . max ( start , since ) ) , 0 )
48+ return Math . max ( 0 , now - since - excluded )
4049}
0 commit comments