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 4ebca6d commit fe61d49
Showing 1 changed file with 2 additions and 8 deletions.
10 changes: 2 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,8 @@ 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.
window?.requestAnimationFrame ?? createQueueWithTimer(10)
: options.type === 'callback'
? options.queueNotification
: createQueueWithTimer(options.timeout)
Expand Down

0 comments on commit fe61d49

Please sign in to comment.