Skip to content

Commit 4e119df

Browse files
committed
Create Tuple class to replace MiddlewareArray and EnhancerArray
1 parent 99545b0 commit 4e119df

20 files changed

+200
-179
lines changed

docs/api/actionCreatorMiddleware.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export default function (state = {}, action: any) {
4747
import {
4848
configureStore,
4949
createActionCreatorInvariantMiddleware,
50-
MiddlewareArray,
50+
Tuple,
5151
} from '@reduxjs/toolkit'
5252
import reducer from './reducer'
5353

@@ -63,6 +63,6 @@ const actionCreatorMiddleware = createActionCreatorInvariantMiddleware({
6363

6464
const store = configureStore({
6565
reducer,
66-
middleware: new MiddlewareArray(actionCreatorMiddleware),
66+
middleware: new Tuple(actionCreatorMiddleware),
6767
})
6868
```

docs/api/configureStore.mdx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,15 @@ and should return a middleware array.
9494
For more details on how the `middleware` parameter works and the list of middleware that are added by default, see the
9595
[`getDefaultMiddleware` docs page](./getDefaultMiddleware.mdx).
9696

97-
:::note MiddlewareArray
98-
Typescript users are required to use a `MiddlewareArray` instance (if not using a `getDefaultMiddleware` result, which is already a `MiddlewareArray`), for better inference.
97+
:::note Tuple
98+
Typescript users are required to use a `Tuple` instance (if not using a `getDefaultMiddleware` result, which is already a `Tuple`), for better inference.
9999

100100
```ts no-transpile
101-
import { configureStore, MiddlewareArray } from '@reduxjs/toolkit'
101+
import { configureStore, Tuple } from '@reduxjs/toolkit'
102102

103103
configureStore({
104104
reducer: rootReducer,
105-
middleware: new MiddlewareArray(additionalMiddleware, logger),
105+
middleware: new Tuple(additionalMiddleware, logger),
106106
})
107107
```
108108
@@ -138,7 +138,7 @@ If defined as an array, these will be passed to [the Redux `compose` function](h
138138
139139
This should _not_ include `applyMiddleware()` or the Redux DevTools Extension `composeWithDevTools`, as those are already handled by `configureStore`.
140140
141-
Example: `enhancers: new EnhancerArray(offline)` will result in a final setup of `[applyMiddleware, offline, devToolsExtension]`.
141+
Example: `enhancers: new Tuple(offline)` will result in a final setup of `[applyMiddleware, offline, devToolsExtension]`.
142142
143143
If defined as a callback function, it will be called with the existing array of enhancers _without_ the DevTools Extension (currently `[applyMiddleware]`),
144144
and should return a new array of enhancers. This is primarily useful for cases where a store enhancer needs to be added
@@ -147,15 +147,15 @@ in front of `applyMiddleware`, such as `redux-first-router` or `redux-offline`.
147147
Example: `enhancers: (defaultEnhancers) => defaultEnhancers.prepend(offline)` will result in a final setup
148148
of `[offline, applyMiddleware, devToolsExtension]`.
149149
150-
:::note EnhancerArray
151-
Typescript users are required to use a `EnhancerArray` instance (if not using a `getDefaultEnhancer` result, which is already a `EnhancerArray`), for better inference.
150+
:::note Tuple
151+
Typescript users are required to use a `Tuple` instance (if not using a `getDefaultEnhancer` result, which is already a `Tuple`), for better inference.
152152
153153
```ts no-transpile
154-
import { configureStore, EnhancerArray } from '@reduxjs/toolkit'
154+
import { configureStore, Tuple } from '@reduxjs/toolkit'
155155

156156
configureStore({
157157
reducer: rootReducer,
158-
enhancers: new EnhancerArray(offline),
158+
enhancers: new Tuple(offline),
159159
})
160160
```
161161

docs/api/getDefaultMiddleware.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ If you want to customize the list of middleware, you can supply an array of midd
2828
```js
2929
const store = configureStore({
3030
reducer: rootReducer,
31-
middleware: new MiddlewareArray(thunk, logger),
31+
middleware: new Tuple(thunk, logger),
3232
})
3333

3434
// Store specifically has the thunk and logger middleware applied
@@ -55,7 +55,7 @@ const store = configureStore({
5555
// Store has all of the default middleware added, _plus_ the logger middleware
5656
```
5757

58-
It is preferable to use the chainable `.concat(...)` and `.prepend(...)` methods of the returned `MiddlewareArray` instead of the array spread operator, as the latter can lose valuable type information under some circumstances.
58+
It is preferable to use the chainable `.concat(...)` and `.prepend(...)` methods of the returned `Tuple` instead of the array spread operator, as the latter can lose valuable type information under some circumstances.
5959

6060
## Included Default Middleware
6161

docs/api/immutabilityMiddleware.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export default exampleSlice.reducer
7474
import {
7575
configureStore,
7676
createImmutableStateInvariantMiddleware,
77-
MiddlewareArray,
77+
Tuple,
7878
} from '@reduxjs/toolkit'
7979

8080
import exampleSliceReducer from './exampleSlice'
@@ -86,7 +86,7 @@ const immutableInvariantMiddleware = createImmutableStateInvariantMiddleware({
8686
const store = configureStore({
8787
reducer: exampleSliceReducer,
8888
// Note that this will replace all default middleware
89-
middleware: new MiddlewareArray(immutableInvariantMiddleware),
89+
middleware: new Tuple(immutableInvariantMiddleware),
9090
})
9191
```
9292

docs/api/serializabilityMiddleware.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ import {
9393
configureStore,
9494
createSerializableStateInvariantMiddleware,
9595
isPlain,
96-
MiddlewareArray,
96+
Tuple,
9797
} from '@reduxjs/toolkit'
9898
import reducer from './reducer'
9999

@@ -111,7 +111,7 @@ const serializableMiddleware = createSerializableStateInvariantMiddleware({
111111

112112
const store = configureStore({
113113
reducer,
114-
middleware: new MiddlewareArray(serializableMiddleware),
114+
middleware: new Tuple(serializableMiddleware),
115115
})
116116
```
117117

docs/usage/usage-with-typescript.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export default store
9999

100100
The type of the `dispatch` function type will be directly inferred from the `middleware` option. So if you add _correctly typed_ middlewares, `dispatch` should already be correctly typed.
101101

102-
As TypeScript often widens array types when combining arrays using the spread operator, we suggest using the `.concat(...)` and `.prepend(...)` methods of the `MiddlewareArray` returned by `getDefaultMiddleware()`.
102+
As TypeScript often widens array types when combining arrays using the spread operator, we suggest using the `.concat(...)` and `.prepend(...)` methods of the `Tuple` returned by `getDefaultMiddleware()`.
103103

104104
```ts
105105
import { configureStore } from '@reduxjs/toolkit'
@@ -134,18 +134,18 @@ export type AppDispatch = typeof store.dispatch
134134
export default store
135135
```
136136

137-
#### Using `MiddlewareArray` without `getDefaultMiddleware`
137+
#### Using `Tuple` without `getDefaultMiddleware`
138138

139-
If you want to skip the usage of `getDefaultMiddleware` altogether, you are requred to use `MiddlewareArray` for type-safe creation of your `middleware` array. This class extends the default JavaScript `Array` type, only with modified typings for `.concat(...)` and the additional `.prepend(...)` method.
139+
If you want to skip the usage of `getDefaultMiddleware` altogether, you are requred to use `Tuple` for type-safe creation of your `middleware` array. This class extends the default JavaScript `Array` type, only with modified typings for `.concat(...)` and the additional `.prepend(...)` method.
140140

141141
For example:
142142

143143
```ts
144-
import { configureStore, MiddlewareArray } from '@reduxjs/toolkit'
144+
import { configureStore, Tuple } from '@reduxjs/toolkit'
145145

146146
configureStore({
147147
reducer: rootReducer,
148-
middleware: new MiddlewareArray(additionalMiddleware, logger),
148+
middleware: new Tuple(additionalMiddleware, logger),
149149
})
150150
```
151151

packages/toolkit/src/configureStore.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import type {
2323
ExtractStoreExtensions,
2424
ExtractStateExtensions,
2525
} from './tsHelpers'
26-
import type { EnhancerArray, MiddlewareArray } from './utils'
26+
import type { Tuple } from './utils'
2727
import type { GetDefaultEnhancers } from './getDefaultEnhancers'
2828
import { buildGetDefaultEnhancers } from './getDefaultEnhancers'
2929

@@ -37,8 +37,8 @@ const IS_PRODUCTION = process.env.NODE_ENV === 'production'
3737
export interface ConfigureStoreOptions<
3838
S = any,
3939
A extends Action = AnyAction,
40-
M extends MiddlewareArray<Middlewares<S>> = MiddlewareArray<Middlewares<S>>,
41-
E extends EnhancerArray<Enhancers> = EnhancerArray<Enhancers>,
40+
M extends Tuple<Middlewares<S>> = Tuple<Middlewares<S>>,
41+
E extends Tuple<Enhancers> = Tuple<Enhancers>,
4242
P = S
4343
> {
4444
/**
@@ -48,8 +48,8 @@ export interface ConfigureStoreOptions<
4848
reducer: Reducer<S, A, P> | ReducersMapObject<S, A, P>
4949

5050
/**
51-
* An array of Redux middleware to install. If not supplied, defaults to
52-
* the set of middleware returned by `getDefaultMiddleware()`.
51+
* An array of Redux middleware to install, or a callback receiving `getDefaultMiddleware` and returning a Tuple of middleware.
52+
* If not supplied, defaults to the set of middleware returned by `getDefaultMiddleware()`.
5353
*
5454
* @example `middleware: (gDM) => gDM().concat(logger, apiMiddleware, yourCustomMiddleware)`
5555
* @see https://redux-toolkit.js.org/api/getDefaultMiddleware#intended-usage
@@ -78,8 +78,8 @@ export interface ConfigureStoreOptions<
7878
* The store enhancers to apply. See Redux's `createStore()`.
7979
* All enhancers will be included before the DevTools Extension enhancer.
8080
* If you need to customize the order of enhancers, supply a callback
81-
* function that will receive a `getDefaultEnhancers` function that returns an EnhancerArray,
82-
* and should return a new array (such as `getDefaultEnhancers().concat(offline)`).
81+
* function that will receive a `getDefaultEnhancers` function that returns a Tuple,
82+
* and should return a Tuple of enhancers (such as `getDefaultEnhancers().concat(offline)`).
8383
* If you only need to add middleware, you can use the `middleware` parameter instead.
8484
*/
8585
enhancers?: ((getDefaultEnhancers: GetDefaultEnhancers<M>) => E) | E
@@ -112,10 +112,8 @@ export type EnhancedStore<
112112
export function configureStore<
113113
S = any,
114114
A extends Action = AnyAction,
115-
M extends MiddlewareArray<Middlewares<S>> = MiddlewareArray<
116-
[ThunkMiddlewareFor<S>]
117-
>,
118-
E extends EnhancerArray<Enhancers> = EnhancerArray<
115+
M extends Tuple<Middlewares<S>> = Tuple<[ThunkMiddlewareFor<S>]>,
116+
E extends Tuple<Enhancers> = Tuple<
119117
[StoreEnhancer<{ dispatch: ExtractDispatchExtensions<M> }>, StoreEnhancer]
120118
>,
121119
P = S

packages/toolkit/src/getDefaultEnhancers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { StoreEnhancer } from 'redux'
22
import type { AutoBatchOptions } from './autoBatchEnhancer'
33
import { autoBatchEnhancer } from './autoBatchEnhancer'
4-
import { EnhancerArray } from './utils'
4+
import { Tuple } from './utils'
55
import type { Middlewares } from './configureStore'
66
import type { ExtractDispatchExtensions } from './tsHelpers'
77

@@ -11,15 +11,15 @@ type GetDefaultEnhancersOptions = {
1111

1212
export type GetDefaultEnhancers<M extends Middlewares<any>> = (
1313
options?: GetDefaultEnhancersOptions
14-
) => EnhancerArray<[StoreEnhancer<{ dispatch: ExtractDispatchExtensions<M> }>]>
14+
) => Tuple<[StoreEnhancer<{ dispatch: ExtractDispatchExtensions<M> }>]>
1515

1616
export const buildGetDefaultEnhancers = <M extends Middlewares<any>>(
1717
middlewareEnhancer: StoreEnhancer<{ dispatch: ExtractDispatchExtensions<M> }>
1818
): GetDefaultEnhancers<M> =>
1919
function getDefaultEnhancers(options) {
2020
const { autoBatch = true } = options ?? {}
2121

22-
let enhancerArray = new EnhancerArray<StoreEnhancer[]>(middlewareEnhancer)
22+
let enhancerArray = new Tuple<StoreEnhancer[]>(middlewareEnhancer)
2323
if (autoBatch) {
2424
enhancerArray.push(
2525
autoBatchEnhancer(typeof autoBatch === 'object' ? autoBatch : undefined)

packages/toolkit/src/getDefaultMiddleware.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { createImmutableStateInvariantMiddleware } from './immutableStateInvaria
1111
import type { SerializableStateInvariantMiddlewareOptions } from './serializableStateInvariantMiddleware'
1212
import { createSerializableStateInvariantMiddleware } from './serializableStateInvariantMiddleware'
1313
import type { ExcludeFromTuple } from './tsHelpers'
14-
import { MiddlewareArray } from './utils'
14+
import { Tuple } from './utils'
1515

1616
function isBoolean(x: any): x is boolean {
1717
return typeof x === 'boolean'
@@ -48,7 +48,7 @@ export type GetDefaultMiddleware<S = any> = <
4848
}
4949
>(
5050
options?: O
51-
) => MiddlewareArray<ExcludeFromTuple<[ThunkMiddlewareFor<S, O>], never>>
51+
) => Tuple<ExcludeFromTuple<[ThunkMiddlewareFor<S, O>], never>>
5252

5353
export const buildGetDefaultMiddleware = <S = any>(): GetDefaultMiddleware<S> =>
5454
function getDefaultMiddleware(options) {
@@ -59,7 +59,7 @@ export const buildGetDefaultMiddleware = <S = any>(): GetDefaultMiddleware<S> =>
5959
actionCreatorCheck = true,
6060
} = options ?? {}
6161

62-
let middlewareArray = new MiddlewareArray<Middleware[]>()
62+
let middlewareArray = new Tuple<Middleware[]>()
6363

6464
if (thunk) {
6565
if (isBoolean(thunk)) {

packages/toolkit/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export type {
101101
// types
102102
ActionReducerMapBuilder,
103103
} from './mapBuilders'
104-
export { MiddlewareArray, EnhancerArray } from './utils'
104+
export { Tuple } from './utils'
105105

106106
export { createEntityAdapter } from './entities/create_adapter'
107107
export type {

0 commit comments

Comments
 (0)