-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduceThunkReducerInjector.ts
43 lines (41 loc) · 1.44 KB
/
produceThunkReducerInjector.ts
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
// Copyright 2022 MFB Technologies, Inc.
import { ActionReducerMapBuilder } from "@reduxjs/toolkit"
/**
* Call this function with a type parameter that gives it the slice's
* state. It returns a function that can be used to create properly
* typed thunk case reducers using the same builder patter provided by
* the `extraReducers` property of `createSlice`'s argument. The end
* result is a function that can be called inside `createSlice` to inject
* the extra reducers built.
*
* @example
* const createCounterThunkReducerInjector = produceThunkReducerInjector<CounterState>()
*
* export const injectReducers = createCounterThunkReducerInjector( builder => {
* builder.addCase(someAsyncThunk.fulfilled, (state, action) => {
* // case reducer code here
* })
* }
*
* //in slice.ts
* createSlice({
* ...
* extraReducers: build => injectReducers(build)
* })
*
* @example <caption>Curried calling style</caption>
* export const injectReducers = produceThunkReducerInjector<CounterState>()( builder => {
* builder.addCase(someAsyncThunk.fulfilled, (state, action) => {
* // case reducer code here
* })
* }
*/
export const produceThunkReducerInjector = <TSliceState = never>() => {
return <TResult>(
injector: [TSliceState] extends [never]
? never
: (builder: ActionReducerMapBuilder<TSliceState>) => TResult
) =>
(builder: ActionReducerMapBuilder<TSliceState>) =>
injector(builder)
}