@@ -14,7 +14,13 @@ export const prepareAutoBatched =
14
14
let promise : Promise < any >
15
15
const queueMicrotaskShim =
16
16
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
+ )
18
24
: // reuse resolved promise, and allocate it lazily
19
25
( cb : ( ) => void ) =>
20
26
( promise || ( promise = Promise . resolve ( ) ) ) . then ( cb ) . catch ( ( err : any ) =>
@@ -23,18 +29,25 @@ const queueMicrotaskShim =
23
29
} , 0 )
24
30
)
25
31
26
- export type AutoBatchOptions =
27
- | { type : 'tick' }
28
- | { type : 'timer' ; timeout : number }
29
- | { type : 'raf' }
30
- | { type : 'callback' ; queueNotification : ( notify : ( ) => void ) => void }
31
-
32
32
const createQueueWithTimer = ( timeout : number ) => {
33
33
return ( notify : ( ) => void ) => {
34
34
setTimeout ( notify , timeout )
35
35
}
36
36
}
37
37
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
+
38
51
/**
39
52
* A Redux store enhancer that watches for "low-priority" actions, and delays
40
53
* notifying subscribers until either the queued callback executes or the
@@ -73,7 +86,7 @@ export const autoBatchEnhancer =
73
86
options . type === 'tick'
74
87
? queueMicrotaskShim
75
88
: options . type === 'raf'
76
- ? requestAnimationFrame
89
+ ? rAF
77
90
: options . type === 'callback'
78
91
? options . queueNotification
79
92
: createQueueWithTimer ( options . timeout )
0 commit comments