Skip to content

Commit dbe7f06

Browse files
authored
Merge pull request #4701 from ensconced/master
Fix issue #4693
2 parents 5c66933 + 3907062 commit dbe7f06

File tree

2 files changed

+85
-8
lines changed

2 files changed

+85
-8
lines changed

packages/toolkit/src/autoBatchEnhancer.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@ const createQueueWithTimer = (timeout: number) => {
1515
}
1616
}
1717

18-
// requestAnimationFrame won't exist in SSR environments.
19-
// Fall back to a vague approximation just to keep from erroring.
20-
const rAF =
21-
typeof window !== 'undefined' && window.requestAnimationFrame
22-
? window.requestAnimationFrame
23-
: createQueueWithTimer(10)
24-
2518
export type AutoBatchOptions =
2619
| { type: 'tick' }
2720
| { type: 'timer'; timeout: number }
@@ -66,7 +59,10 @@ export const autoBatchEnhancer =
6659
options.type === 'tick'
6760
? queueMicrotask
6861
: options.type === 'raf'
69-
? rAF
62+
? // requestAnimationFrame won't exist in SSR environments. Fall back to a vague approximation just to keep from erroring.
63+
typeof window !== 'undefined' && window.requestAnimationFrame
64+
? window.requestAnimationFrame
65+
: createQueueWithTimer(10)
7066
: options.type === 'callback'
7167
? options.queueNotification
7268
: createQueueWithTimer(options.timeout)

packages/toolkit/src/tests/autoBatchEnhancer.test.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,84 @@ describe.each(cases)('autoBatchEnhancer: %j', (autoBatchOptions) => {
125125
expect(subscriptionNotifications).toBe(3)
126126
})
127127
})
128+
129+
describe.each(cases)(
130+
'autoBatchEnhancer with fake timers: %j',
131+
(autoBatchOptions) => {
132+
beforeAll(() => {
133+
vitest.useFakeTimers({
134+
toFake: ['setTimeout', 'queueMicrotask', 'requestAnimationFrame'],
135+
})
136+
})
137+
afterAll(() => {
138+
vitest.useRealTimers()
139+
})
140+
beforeEach(() => {
141+
subscriptionNotifications = 0
142+
store = makeStore(autoBatchOptions)
143+
144+
store.subscribe(() => {
145+
subscriptionNotifications++
146+
})
147+
})
148+
test('Does not alter normal subscription notification behavior', () => {
149+
store.dispatch(decrementUnbatched())
150+
expect(subscriptionNotifications).toBe(1)
151+
store.dispatch(decrementUnbatched())
152+
expect(subscriptionNotifications).toBe(2)
153+
store.dispatch(decrementUnbatched())
154+
expect(subscriptionNotifications).toBe(3)
155+
store.dispatch(decrementUnbatched())
156+
157+
vitest.runAllTimers()
158+
159+
expect(subscriptionNotifications).toBe(4)
160+
})
161+
162+
test('Only notifies once if several batched actions are dispatched in a row', () => {
163+
store.dispatch(incrementBatched())
164+
expect(subscriptionNotifications).toBe(0)
165+
store.dispatch(incrementBatched())
166+
expect(subscriptionNotifications).toBe(0)
167+
store.dispatch(incrementBatched())
168+
expect(subscriptionNotifications).toBe(0)
169+
store.dispatch(incrementBatched())
170+
171+
vitest.runAllTimers()
172+
173+
expect(subscriptionNotifications).toBe(1)
174+
})
175+
176+
test('Notifies immediately if a non-batched action is dispatched', () => {
177+
store.dispatch(incrementBatched())
178+
expect(subscriptionNotifications).toBe(0)
179+
store.dispatch(incrementBatched())
180+
expect(subscriptionNotifications).toBe(0)
181+
store.dispatch(decrementUnbatched())
182+
expect(subscriptionNotifications).toBe(1)
183+
store.dispatch(incrementBatched())
184+
185+
vitest.runAllTimers()
186+
187+
expect(subscriptionNotifications).toBe(2)
188+
})
189+
190+
test('Does not notify at end of tick if last action was normal priority', () => {
191+
store.dispatch(incrementBatched())
192+
expect(subscriptionNotifications).toBe(0)
193+
store.dispatch(incrementBatched())
194+
expect(subscriptionNotifications).toBe(0)
195+
store.dispatch(decrementUnbatched())
196+
expect(subscriptionNotifications).toBe(1)
197+
store.dispatch(incrementBatched())
198+
store.dispatch(decrementUnbatched())
199+
expect(subscriptionNotifications).toBe(2)
200+
store.dispatch(decrementUnbatched())
201+
expect(subscriptionNotifications).toBe(3)
202+
203+
vitest.runAllTimers()
204+
205+
expect(subscriptionNotifications).toBe(3)
206+
})
207+
},
208+
)

0 commit comments

Comments
 (0)