Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 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,10 @@ 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.
typeof window !== 'undefined' && window.requestAnimationFrame
? window.requestAnimationFrame
: createQueueWithTimer(10)
: options.type === 'callback'
? options.queueNotification
: createQueueWithTimer(options.timeout)
Expand Down
81 changes: 81 additions & 0 deletions packages/toolkit/src/tests/autoBatchEnhancer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,84 @@ describe.each(cases)('autoBatchEnhancer: %j', (autoBatchOptions) => {
expect(subscriptionNotifications).toBe(3)
})
})

describe.each(cases)(
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two of these tests were failing before the fix was applied

'autoBatchEnhancer with fake timers: %j',
(autoBatchOptions) => {
beforeAll(() => {
vitest.useFakeTimers({
toFake: ['setTimeout', 'queueMicrotask', 'requestAnimationFrame'],
})
})
afterAll(() => {
vitest.useRealTimers()
})
beforeEach(() => {
subscriptionNotifications = 0
store = makeStore(autoBatchOptions)

store.subscribe(() => {
subscriptionNotifications++
})
})
test('Does not alter normal subscription notification behavior', () => {
store.dispatch(decrementUnbatched())
expect(subscriptionNotifications).toBe(1)
store.dispatch(decrementUnbatched())
expect(subscriptionNotifications).toBe(2)
store.dispatch(decrementUnbatched())
expect(subscriptionNotifications).toBe(3)
store.dispatch(decrementUnbatched())

vitest.runAllTimers()

expect(subscriptionNotifications).toBe(4)
})

test('Only notifies once if several batched actions are dispatched in a row', () => {
store.dispatch(incrementBatched())
expect(subscriptionNotifications).toBe(0)
store.dispatch(incrementBatched())
expect(subscriptionNotifications).toBe(0)
store.dispatch(incrementBatched())
expect(subscriptionNotifications).toBe(0)
store.dispatch(incrementBatched())

vitest.runAllTimers()

expect(subscriptionNotifications).toBe(1)
})

test('Notifies immediately if a non-batched action is dispatched', () => {
store.dispatch(incrementBatched())
expect(subscriptionNotifications).toBe(0)
store.dispatch(incrementBatched())
expect(subscriptionNotifications).toBe(0)
store.dispatch(decrementUnbatched())
expect(subscriptionNotifications).toBe(1)
store.dispatch(incrementBatched())

vitest.runAllTimers()

expect(subscriptionNotifications).toBe(2)
})

test('Does not notify at end of tick if last action was normal priority', () => {
store.dispatch(incrementBatched())
expect(subscriptionNotifications).toBe(0)
store.dispatch(incrementBatched())
expect(subscriptionNotifications).toBe(0)
store.dispatch(decrementUnbatched())
expect(subscriptionNotifications).toBe(1)
store.dispatch(incrementBatched())
store.dispatch(decrementUnbatched())
expect(subscriptionNotifications).toBe(2)
store.dispatch(decrementUnbatched())
expect(subscriptionNotifications).toBe(3)

vitest.runAllTimers()

expect(subscriptionNotifications).toBe(3)
})
},
)