Skip to content

Commit 209e617

Browse files
committed
🐛 use idle phase start when requestIdleCallback isn't available
1 parent 53b1f76 commit 209e617

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/phaseTracking.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,14 @@ const idlePhaseTracker = createPhaseTracker((callback: (idlePhase: IdlePhase) =>
1414
nextTask(() => {
1515
shouldRequestAnimationFrame = true
1616

17-
let start: number | undefined
17+
const start = Date.now()
18+
const deadline = start + 16
1819
callback({
19-
get start() {
20-
start = start ?? Date.now()
21-
return start
22-
},
20+
start,
2321
deadline: {
2422
didTimeout: false,
2523
timeRemaining(): DOMHighResTimeStamp {
26-
start = start ?? Date.now()
27-
return Math.max(start + 16 - Date.now(), 0)
24+
return Math.max(deadline - Date.now(), 0)
2825
},
2926
},
3027
})
@@ -142,7 +139,10 @@ export function getIdlePhase(): IdlePhase | undefined {
142139
const deadline = idlePhaseTracker.getPhase()?.deadline
143140
const idlePhaseStart = idlePhaseTracker.getPhase()?.start
144141
const animationFramePhaseStart = animationFrameTracker.getPhase()?.start
145-
const start = animationFramePhaseStart ?? idlePhaseStart
142+
const start =
143+
typeof requestIdleCallback === 'undefined'
144+
? idlePhaseStart
145+
: animationFramePhaseStart ?? idlePhaseStart
146146

147147
return start === undefined || deadline === undefined ? undefined : { start, deadline }
148148
}

0 commit comments

Comments
 (0)