You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/api-reference.md
+17-6Lines changed: 17 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -153,30 +153,41 @@ local reducer = createReducer(initialState, {
153
153
```
154
154
155
155
## Middleware
156
-
Rodux provides an API that allows changing the way that actions are dispatched called *middleware*. To attach middlewares to a store, pass a list of middleware as the third argument to `Store.new`.
156
+
Rodux provides an API that allows changing the way that actions are dispatched called *middleware*. To attach middleware to a store, pass a list of middleware as the third argument to `Store.new`.
157
+
158
+
!!! warn
159
+
The middleware API changed in [#29](https://github.com/Roblox/rodux/pull/29) -- middleware written against the old API will not work!
157
160
158
161
A single middleware is just a function with the following signature:
159
162
160
163
```
161
-
(next) -> (store, action) -> result
164
+
(nextDispatch, store) -> (action) -> result
162
165
```
163
166
164
-
That is, middleware is a function that accepts the next middleware to apply and returns a new function. That function takes the `Store` and the current action and can dispatch more actions, log to output, or do network requests!
167
+
A middleware is a function that accepts the next dispatch function in the *middleware chain*, as well as the store the middleware is being used with, and returns a new function. That function is called whenever an action is dispatched and can dispatch more actions, log to output, or perform any side effects!
165
168
166
169
A simple version of Rodux's `loggerMiddleware` is as easy as:
167
170
168
171
```lua
169
-
localfunctionsimpleLogger(next)
170
-
returnfunction(store, action)
172
+
localfunctionsimpleLogger(nextDispatch, store)
173
+
returnfunction(action)
171
174
print("Dispatched action of type", action.type)
172
175
173
-
returnnext(store, action)
176
+
returnnextDispatch(action)
174
177
end
175
178
end
176
179
```
177
180
178
181
Rodux also ships with several middleware that address common use-cases.
179
182
183
+
To apply middleware, pass a list of middleware as the third argument to `Store.new`:
Middleware runs from left to right when an action is dispatched. That means that if a middleware does not call `nextDispatch` when handling an action, any middleware after it will not run.
190
+
180
191
### Rodux.loggerMiddleware
181
192
A middleware that logs actions and the new state that results from them.
0 commit comments