Skip to content

Commit 9813d3c

Browse files
committed
Fix formatting
1 parent eaa0ffc commit 9813d3c

File tree

1 file changed

+64
-63
lines changed

1 file changed

+64
-63
lines changed

docs/usage/WritingCustomMiddleware.md

+64-63
Original file line numberDiff line numberDiff line change
@@ -39,74 +39,75 @@ You might still want to use custom middleware in one of two cases:
3939
### Create side effects for actions
4040

4141
This is the most common middleware. Here's what it looks like for [rtk listener middleware](https://github.com/reduxjs/redux-toolkit/blob/0678c2e195a70c34cd26bddbfd29043bc36d1362/packages/toolkit/src/listenerMiddleware/index.ts#L427):
42+
4243
```ts
4344
const middleware: ListenerMiddleware<S, D, ExtraArgument> =
44-
(api) => (next) => (action) => {
45-
if (addListener.match(action)) {
46-
return startListening(action.payload)
47-
}
48-
49-
if (clearAllListeners.match(action)) {
50-
clearListenerMiddleware()
51-
return
52-
}
45+
api => next => action => {
46+
if (addListener.match(action)) {
47+
return startListening(action.payload)
48+
}
5349

54-
if (removeListener.match(action)) {
55-
return stopListening(action.payload)
56-
}
50+
if (clearAllListeners.match(action)) {
51+
clearListenerMiddleware()
52+
return
53+
}
5754

58-
// Need to get this state _before_ the reducer processes the action
59-
let originalState: S | typeof INTERNAL_NIL_TOKEN = api.getState()
55+
if (removeListener.match(action)) {
56+
return stopListening(action.payload)
57+
}
6058

61-
// `getOriginalState` can only be called synchronously.
62-
// @see https://github.com/reduxjs/redux-toolkit/discussions/1648#discussioncomment-1932820
63-
const getOriginalState = (): S => {
64-
if (originalState === INTERNAL_NIL_TOKEN) {
65-
throw new Error(
66-
`${alm}: getOriginalState can only be called synchronously`
67-
)
68-
}
59+
// Need to get this state _before_ the reducer processes the action
60+
let originalState: S | typeof INTERNAL_NIL_TOKEN = api.getState()
6961

70-
return originalState as S
62+
// `getOriginalState` can only be called synchronously.
63+
// @see https://github.com/reduxjs/redux-toolkit/discussions/1648#discussioncomment-1932820
64+
const getOriginalState = (): S => {
65+
if (originalState === INTERNAL_NIL_TOKEN) {
66+
throw new Error(
67+
`${alm}: getOriginalState can only be called synchronously`
68+
)
7169
}
7270

73-
let result: unknown
71+
return originalState as S
72+
}
7473

75-
try {
76-
// Actually forward the action to the reducer before we handle listeners
77-
result = next(action)
74+
let result: unknown
7875

79-
if (listenerMap.size > 0) {
80-
let currentState = api.getState()
81-
// Work around ESBuild+TS transpilation issue
82-
const listenerEntries = Array.from(listenerMap.values())
83-
for (let entry of listenerEntries) {
84-
let runListener = false
76+
try {
77+
// Actually forward the action to the reducer before we handle listeners
78+
result = next(action)
8579

86-
try {
87-
runListener = entry.predicate(action, currentState, originalState)
88-
} catch (predicateError) {
89-
runListener = false
80+
if (listenerMap.size > 0) {
81+
let currentState = api.getState()
82+
// Work around ESBuild+TS transpilation issue
83+
const listenerEntries = Array.from(listenerMap.values())
84+
for (let entry of listenerEntries) {
85+
let runListener = false
9086

91-
safelyNotifyError(onError, predicateError, {
92-
raisedBy: 'predicate',
93-
})
94-
}
87+
try {
88+
runListener = entry.predicate(action, currentState, originalState)
89+
} catch (predicateError) {
90+
runListener = false
9591

96-
if (!runListener) {
97-
continue
98-
}
92+
safelyNotifyError(onError, predicateError, {
93+
raisedBy: 'predicate'
94+
})
95+
}
9996

100-
notifyListener(entry, action, api, getOriginalState)
97+
if (!runListener) {
98+
continue
10199
}
100+
101+
notifyListener(entry, action, api, getOriginalState)
102102
}
103-
} finally {
104-
// Remove `originalState` store from this scope.
105-
originalState = INTERNAL_NIL_TOKEN
106103
}
107-
108-
return result
104+
} finally {
105+
// Remove `originalState` store from this scope.
106+
originalState = INTERNAL_NIL_TOKEN
109107
}
108+
109+
return result
110+
}
110111
```
111112

112113
In the first part, it listens to `addListener`, `clearAllListeners` and `removeListener` actions to change which listeners should be invoked later on.
@@ -120,20 +121,20 @@ It is common to have side effects after dispatching th eaction, because this all
120121
While these patterns are less common, most of them (except for cancelling actions) are used by [redux thunk middleware](https://github.com/reduxjs/redux-thunk/blob/587a85b1d908e8b7cf2297bec6e15807d3b7dc62/src/index.ts#L22):
121122

122123
```ts
123-
const middleware: ThunkMiddleware<State, BasicAction, ExtraThunkArg> =
124-
({ dispatch, getState }) =>
125-
next =>
126-
action => {
127-
// The thunk middleware looks for any functions that were passed to `store.dispatch`.
128-
// If this "action" is really a function, call it and return the result.
129-
if (typeof action === 'function') {
130-
// Inject the store's `dispatch` and `getState` methods, as well as any "extra arg"
131-
return action(dispatch, getState, extraArgument)
132-
}
133-
134-
// Otherwise, pass the action down the middleware chain as usual
135-
return next(action)
124+
const middleware: ThunkMiddleware<State, BasicAction, ExtraThunkArg> =
125+
({ dispatch, getState }) =>
126+
next =>
127+
action => {
128+
// The thunk middleware looks for any functions that were passed to `store.dispatch`.
129+
// If this "action" is really a function, call it and return the result.
130+
if (typeof action === 'function') {
131+
// Inject the store's `dispatch` and `getState` methods, as well as any "extra arg"
132+
return action(dispatch, getState, extraArgument)
136133
}
134+
135+
// Otherwise, pass the action down the middleware chain as usual
136+
return next(action)
137+
}
137138
```
138139

139140
Usually, `dispatch` can only handle JSON actions. This middleware adds the ability to also handle actions in the form of functions. It also changes the return type of the dispatch function itself by passing the return value of the function-action to be the return value of the dispatch function.

0 commit comments

Comments
 (0)