Skip to content

Commit

Permalink
add better implementation to detect the FPS
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinMalfait committed Feb 1, 2021
1 parent d2491b9 commit 5e6a2b9
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions packages/@headlessui-react/src/test-utils/execute-timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,29 @@ executeTimeline.fullTransition = (duration: number) => {
]
}

// Assuming that we run at 60 frames per second
let frame = 1000 / 60
let state: {
before: number
fps: number
handle: ReturnType<typeof requestAnimationFrame> | null
} = {
before: Date.now(),
fps: 0,
handle: null,
}

state.handle = requestAnimationFrame(function loop() {
let now = Date.now()
state.fps = Math.round(1000 / (now - state.before))
state.before = now
state.handle = requestAnimationFrame(loop)
})

afterAll(() => {
if (state.handle) cancelAnimationFrame(state.handle)
})

function isWithinFrame(actual: number, expected: number, frames = 2) {
let buffer = frame * frames
function isWithinFrame(actual: number, expected: number) {
let buffer = state.fps

let min = expected - buffer
let max = expected + buffer
Expand Down

0 comments on commit 5e6a2b9

Please sign in to comment.