Skip to content

Commit

Permalink
Flip middleware execution order
Browse files Browse the repository at this point in the history
  • Loading branch information
LPGhatguy committed May 18, 2018
1 parent df5fbdb commit 93b6c54
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Thunks are no longer enabled by default, use `Rodux.thunkMiddleware` to add them back.
* Added `Rodux.loggerMiddleware` as a simple debugger
* The middleware API changed in [#29](https://github.com/Roblox/rodux/pull/29) in a backwards-incompatible way!
* Middleware now run left-to-right instead of right-to-left!
* Errors thrown in `changed` event now have correct stack traces ([#27](https://github.com/Roblox/rodux/pull/27))

## Public Release (December 13, 2017)
Expand Down
10 changes: 9 additions & 1 deletion docs/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ local reducer = createReducer(initialState, {
```

## Middleware
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`.
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`.

!!! warn
The middleware API changed in [#29](https://github.com/Roblox/rodux/pull/29) -- middleware written against the old API will not work!
Expand All @@ -180,6 +180,14 @@ end

Rodux also ships with several middleware that address common use-cases.

To apply middleware, pass a list of middleware as the third argument to `Store.new`:

```lua
local store = Store.new(reducer, initialState, { simpleLogger })
```

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.

### Rodux.loggerMiddleware
A middleware that logs actions and the new state that results from them.

Expand Down
3 changes: 2 additions & 1 deletion lib/Store.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ function Store.new(reducer, initialState, middlewares)
return unboundDispatch(self, ...)
end

for _, middleware in ipairs(middlewares) do
for i = #middlewares, 1, -1 do
local middleware = middlewares[i]
dispatch = middleware(dispatch, self)
end

Expand Down
38 changes: 38 additions & 0 deletions lib/Store.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,44 @@ return function()
store:destruct()
end)

it("should execute middleware left-to-right", function()
local events = {}

local function reducer(state)
return state
end

local function middlewareA(nextDispatch, store)
table.insert(events, "instantiate a")
return function(action)
table.insert(events, "execute a")
return nextDispatch(action)
end
end

local function middlewareB(nextDispatch, store)
table.insert(events, "instantiate b")
return function(action)
table.insert(events, "execute b")
return nextDispatch(action)
end
end

local store = Store.new(reducer, 5, { middlewareA, middlewareB })

expect(#events).to.equal(2)
expect(events[1]).to.equal("instantiate b")
expect(events[2]).to.equal("instantiate a")

store:dispatch({
type = "test",
})

expect(#events).to.equal(4)
expect(events[3]).to.equal("execute a")
expect(events[4]).to.equal("execute b")
end)

it("should send an initial action with a 'type' field", function()
local lastAction
local callCount = 0
Expand Down

0 comments on commit 93b6c54

Please sign in to comment.