Skip to content

Commit 026221a

Browse files
authored
Merge pull request #2887 from reduxjs/bugfix/1.9-polyfills
2 parents 69ee99a + cd7c208 commit 026221a

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

packages/toolkit/src/autoBatchEnhancer.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ export const prepareAutoBatched =
1414
let promise: Promise<any>
1515
const 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-
3232
const 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)

packages/toolkit/src/query/core/buildMiddleware/batchActions.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ import { createSlice, PayloadAction, AnyAction } from '@reduxjs/toolkit'
1212
let promise: Promise<any>
1313
const queueMicrotaskShim =
1414
typeof queueMicrotask === 'function'
15-
? queueMicrotask.bind(typeof window !== 'undefined' ? window : global)
15+
? queueMicrotask.bind(
16+
typeof window !== 'undefined'
17+
? window
18+
: typeof global !== 'undefined'
19+
? global
20+
: globalThis
21+
)
1622
: // reuse resolved promise, and allocate it lazily
1723
(cb: () => void) =>
1824
(promise || (promise = Promise.resolve())).then(cb).catch((err: any) =>

0 commit comments

Comments
 (0)