File size: 6,246 Bytes
c2b7eb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { configureStore } from '../configureStore'
import { createSlice } from '../createSlice'
import type { AutoBatchOptions } from '../autoBatchEnhancer'
import { autoBatchEnhancer, prepareAutoBatched } from '../autoBatchEnhancer'
import { delay } from '../utils'
import { debounce } from 'lodash'

interface CounterState {
  value: number
}

const counterSlice = createSlice({
  name: 'counter',
  initialState: { value: 0 } as CounterState,
  reducers: {
    incrementBatched: {
      // Batched, low-priority
      reducer(state) {
        state.value += 1
      },
      prepare: prepareAutoBatched<void>(),
    },
    // Not batched, normal priority
    decrementUnbatched(state) {
      state.value -= 1
    },
  },
})
const { incrementBatched, decrementUnbatched } = counterSlice.actions

const makeStore = (autoBatchOptions?: AutoBatchOptions) => {
  return configureStore({
    reducer: counterSlice.reducer,
    enhancers: (getDefaultEnhancers) =>
      getDefaultEnhancers({
        autoBatch: autoBatchOptions,
      }),
  })
}

let store: ReturnType<typeof makeStore>

let subscriptionNotifications = 0

const cases: AutoBatchOptions[] = [
  { type: 'tick' },
  { type: 'raf' },
  { type: 'timer', timeout: 0 },
  { type: 'timer', timeout: 10 },
  { type: 'timer', timeout: 20 },
  {
    type: 'callback',
    queueNotification: debounce((notify: () => void) => {
      notify()
    }, 5),
  },
]

describe.each(cases)('autoBatchEnhancer: %j', (autoBatchOptions) => {
  beforeEach(() => {
    subscriptionNotifications = 0
    store = makeStore(autoBatchOptions)

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

    await delay(25)

    expect(subscriptionNotifications).toBe(4)
  })

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

    await delay(25)

    expect(subscriptionNotifications).toBe(1)
  })

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

    await delay(25)

    expect(subscriptionNotifications).toBe(2)
  })

  test('Does not notify at end of tick if last action was normal priority', async () => {
    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)

    await delay(25)

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

describe.each(cases)(
  '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)
    })
  },
)