@@ -14,7 +14,13 @@ export const prepareAutoBatched =
1414let promise : Promise < any >
1515const queueMicrotaskShim =
1616 typeof queueMicrotask === 'function'
17- ? queueMicrotask . bind ( typeof window !== 'undefined' ? window : global )
17+ ? queueMicrotask . bind (
18+ typeof window !== 'undefined'
19+ ? window
20+ : typeof global !== 'undefined'
21+ ? global
22+ : globalThis
23+ )
1824 : // reuse resolved promise, and allocate it lazily
1925 ( cb : ( ) => void ) =>
2026 ( promise || ( promise = Promise . resolve ( ) ) ) . then ( cb ) . catch ( ( err : any ) =>
@@ -23,18 +29,25 @@ const queueMicrotaskShim =
2329 } , 0 )
2430 )
2531
26- export type AutoBatchOptions =
27- | { type : 'tick' }
28- | { type : 'timer' ; timeout : number }
29- | { type : 'raf' }
30- | { type : 'callback' ; queueNotification : ( notify : ( ) => void ) => void }
31-
3232const createQueueWithTimer = ( timeout : number ) => {
3333 return ( notify : ( ) => void ) => {
3434 setTimeout ( notify , timeout )
3535 }
3636}
3737
38+ // requestAnimationFrame won't exist in SSR environments.
39+ // Fall back to a vague approximation just to keep from erroring.
40+ const rAF =
41+ typeof window !== 'undefined' && window . requestAnimationFrame
42+ ? window . requestAnimationFrame
43+ : createQueueWithTimer ( 10 )
44+
45+ export type AutoBatchOptions =
46+ | { type : 'tick' }
47+ | { type : 'timer' ; timeout : number }
48+ | { type : 'raf' }
49+ | { type : 'callback' ; queueNotification : ( notify : ( ) => void ) => void }
50+
3851/**
3952 * A Redux store enhancer that watches for "low-priority" actions, and delays
4053 * notifying subscribers until either the queued callback executes or the
@@ -73,7 +86,7 @@ export const autoBatchEnhancer =
7386 options . type === 'tick'
7487 ? queueMicrotaskShim
7588 : options . type === 'raf'
76- ? requestAnimationFrame
89+ ? rAF
7790 : options . type === 'callback'
7891 ? options . queueNotification
7992 : createQueueWithTimer ( options . timeout )
0 commit comments