Skip to content

Commit

Permalink
Fix issue reduxjs#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 506355f
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 506355f

Please # to comment.