Skip to content

Commit

Permalink
Fix issue #4693 by
Browse files Browse the repository at this point in the history
reading window.requestAnimationFrame within the autoBatchEnhancer
function instead of in the top-level module scope. This ensures that
we use the fake version of requestAnimationFrame if jest is using
fake timers.
  • Loading branch information
ensconced committed Nov 11, 2024
1 parent a0858eb commit 3907062
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions packages/toolkit/src/autoBatchEnhancer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ const createQueueWithTimer = (timeout: number) => {
}
}

// requestAnimationFrame won't exist in SSR environments.
// Fall back to a vague approximation just to keep from erroring.
const rAF =
typeof window !== 'undefined' && window.requestAnimationFrame
? window.requestAnimationFrame
: createQueueWithTimer(10)

export type AutoBatchOptions =
| { type: 'tick' }
| { type: 'timer'; timeout: number }
Expand Down Expand Up @@ -66,7 +59,10 @@ export const autoBatchEnhancer =
options.type === 'tick'
? queueMicrotask
: options.type === 'raf'
? rAF
? // requestAnimationFrame won't exist in SSR environments. Fall back to a vague approximation just to keep from erroring.
typeof window !== 'undefined' && window.requestAnimationFrame
? window.requestAnimationFrame
: createQueueWithTimer(10)
: options.type === 'callback'
? options.queueNotification
: createQueueWithTimer(options.timeout)
Expand Down

0 comments on commit 3907062

Please sign in to comment.