Skip to content

Commit 2cccf48

Browse files
neurosnapAndarist
andauthored
Update saga middleware options to reflect implementation (#2372)
We already allowed `channel` to be passed into `createSagaMiddleware`, this change simply updates our types to match our implementation. I also added a recipe for batching redux actions since it is a very common paradigm and something I've had to re-write multiple times from memory. Co-authored-by: Mateusz Burzyński <[email protected]>
1 parent daf805c commit 2cccf48

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed

.changeset/eighty-lions-sleep.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@redux-saga/core': patch
3+
'redux-saga': patch
4+
---
5+
6+
Added a `channel` property to the `SagaMiddlewareOptions` to reflect its runtime support.

docs/API.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ Creates a Redux middleware and connects the Sagas to the Redux Store
1919

2020
- `onError: (error: Error, { sagaStack: string })` - if provided, the middleware will call it with uncaught errors from Sagas. useful for sending uncaught exceptions to error tracking services.
2121
- `effectMiddlewares` : Function [] - allows you to intercept any effect, resolve it on your own and pass to the next middleware. See [this section](advanced/Testing.md#effectmiddlewares) for a detailed example
22-
22+
- `channel`: If provided, the middleware will use this channel instead of the default `stdChannel()` for
23+
* `take` and `put` effects.
2324

2425
#### Example
2526

docs/Recipes.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,46 @@ function* main() {
214214
}
215215
}
216216
```
217+
218+
## Batching actions
219+
220+
`redux` does not support the ability to dispatch multiple actions and only call
221+
the reducer once. This has performance implications and the ergonomics of
222+
needing to dispatch multiple actions sequentially aren't great.
223+
224+
Instead we look to a third-party library, [redux-batched-actions](https://github.com/tshelburne/redux-batched-actions). This is a
225+
simple reducer and action that allows end-developers to dispatch multiple
226+
actions and only have your reducer be called once.
227+
228+
If you have a codebase that needs to dispatch many actions at the same time, we
229+
recommend using this recipe.
230+
231+
```javascript
232+
import { configureStore } from '@reduxjs/toolkit';
233+
import createSagaMiddleware, { stdChannel } from 'redux-saga';
234+
import { enableBatching, BATCH } from 'redux-batched-actions';
235+
236+
// your root reducer
237+
import { rootReducer } from './reducer';
238+
// your root saga
239+
import { rootSaga } from './saga';
240+
241+
const channel = stdChannel();
242+
const rawPut = channel.put;
243+
channel.put = (action: ActionWithPayload<any>) => {
244+
if (action.type === BATCH) {
245+
action.payload.forEach(rawPut);
246+
return;
247+
}
248+
rawPut(action);
249+
};
250+
const sagaMiddleware = createSagaMiddleware({ channel });
251+
252+
const reducer = enableBatching(rootReducer);
253+
// https://redux-toolkit.js.org/api/configureStore
254+
const store = configureStore({
255+
reducer: rootReducer,
256+
middleware: [sagaMiddleware],
257+
});
258+
sagaMiddleware.run(rootSaga);
259+
```

packages/core/types/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ export interface SagaMiddlewareOptions<C extends object = {}> {
125125
* next middleware.
126126
*/
127127
effectMiddlewares?: EffectMiddleware[]
128+
/**
129+
* If provided, the middleware will use this channel instead of the default `stdChannel` for
130+
* take and put effects.
131+
*/
132+
channel?: MulticastChannel<Action>;
128133
}
129134

130135
export interface SagaMiddleware<C extends object = {}> extends Middleware {

packages/core/types/ts3.6/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ export interface SagaMiddlewareOptions<C extends object = {}> {
124124
* next middleware.
125125
*/
126126
effectMiddlewares?: EffectMiddleware[]
127+
/**
128+
* If provided, the middleware will use this channel instead of the default `stdChannel` for
129+
* take and put effects.
130+
*/
131+
channel?: MulticastChannel<Action>;
127132
}
128133

129134
export interface SagaMiddleware<C extends object = {}> extends Middleware {

0 commit comments

Comments
 (0)