Skip to content

Commit 9891fa3

Browse files
committed
add better implementation to detect the FPS
1 parent 9e05bbe commit 9891fa3

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

packages/@headlessui-react/src/test-utils/execute-timeline.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,29 @@ executeTimeline.fullTransition = (duration: number) => {
130130
]
131131
}
132132

133-
// Assuming that we run at 60 frames per second
134-
let frame = 1000 / 60
133+
let state: {
134+
before: number
135+
fps: number
136+
handle: ReturnType<typeof requestAnimationFrame> | null
137+
} = {
138+
before: Date.now(),
139+
fps: 0,
140+
handle: null,
141+
}
142+
143+
state.handle = requestAnimationFrame(function loop() {
144+
let now = Date.now()
145+
state.fps = Math.round(1000 / (now - state.before))
146+
state.before = now
147+
state.handle = requestAnimationFrame(loop)
148+
})
149+
150+
afterAll(() => {
151+
if (state.handle) cancelAnimationFrame(state.handle)
152+
})
135153

136-
function isWithinFrame(actual: number, expected: number, frames = 2) {
137-
let buffer = frame * frames
154+
function isWithinFrame(actual: number, expected: number) {
155+
let buffer = state.fps
138156

139157
let min = expected - buffer
140158
let max = expected + buffer

0 commit comments

Comments
 (0)