Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherry-pick thunk extra-arg change onto 3.x patch branch #94

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased Changes

## 3.2.0 (2023-11-17)
* Add makeThunkMiddleware to inject custom argument ([#94](https://github.com/Roblox/rodux/pull/94)).

## 3.1.0 (2023-08-22)
* Add support for devtools ([#84](https://github.com/Roblox/rodux/pull/84))

Expand Down
27 changes: 26 additions & 1 deletion docs/advanced/thunks.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,29 @@ store:dispatch(function(store)
end)
```

Thunks are a simple way to introduce more complex processing of `action` objects, but you may want to consider creating custom [`middleware`](middleware.md) for complex features instead of relying on thunks alone.
Thunks are a simple way to introduce more complex processing of `action` objects, but you may want to consider creating custom [`middleware`](middleware.md) for complex features instead of relying on thunks alone.

It is also possible to inject a custom argument into the thunk middleware. This is useful for cases like using an API service layer that could be swapped out for a mock service in tests. This is accomplished by using the `Rodux.makeThunkMiddleware` API instead:

```lua
local myThunkMiddleware = Rodux.makeThunkMiddleware(myCustomArg)
local store = Rodux.Store.new(reducer, initialState, {
myThunkMiddleware,
})

store:dispatch(function(store, myCustomArg)
print("Hello from a thunk with extra argument:", myCustomArg)
store:dispatch({
type = "thunkAction"
})
end)
```

If multiple values need to be passed in, they can be combined into a single table:

```lua
local myThunkMiddleware = Rodux.makeThunkMiddleware({
[RoactNetworking] = networking,
AvatarEditorService = AvatarEditorService,
})
```
20 changes: 20 additions & 0 deletions docs/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,23 @@ store:dispatch(function(store)
})
end)
```

### Rodux.makeThunkMiddleware (unreleased)
```
Rodux.makeThunkMiddleware(extraArgument) -> thunkMiddleware
```

A function that creates a thunk middleware that injects a custom argument when invoking thunks (in addition to the store itself). This is useful for cases like using an API service layer that could be swapped out for a mock service in tests.

```lua
local myThunkMiddleware = Rodux.makeThunkMiddleware(myCustomArg)
local store = Store.new(reducer, initialState, { myThunkMiddleware })

store:dispatch(function(store, myCustomArg)
print("Hello from a thunk with extra argument:", myCustomArg)

store:dispatch({
type = "thunkAction"
})
end)
```
2 changes: 1 addition & 1 deletion rotriever.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ name = "Rodux"
authors = ["Roblox"]
license = "Apache-2.0"
content_root = "src"
version = "3.1.0"
version = "3.2.0"
2 changes: 2 additions & 0 deletions src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ local combineReducers = require(script.combineReducers)
local makeActionCreator = require(script.makeActionCreator)
local loggerMiddleware = require(script.loggerMiddleware)
local thunkMiddleware = require(script.thunkMiddleware)
local makeThunkMiddleware = require(script.makeThunkMiddleware)

return {
Store = Store,
Expand All @@ -12,4 +13,5 @@ return {
makeActionCreator = makeActionCreator,
loggerMiddleware = loggerMiddleware.middleware,
thunkMiddleware = thunkMiddleware,
makeThunkMiddleware = makeThunkMiddleware,
}
40 changes: 40 additions & 0 deletions src/makeThunkMiddleware.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
--[[
A middleware that allows for functions to be dispatched with an extra
argument for convenience. Functions will receive two arguments:
the store itself and the extra argument provided initially to makeThunkMiddleware.

This middleware consumes the function; middleware further down the chain
will not receive it.
]]
local function tracebackReporter(message)
return debug.traceback(message)
end

local function makeThunkMiddleware(extraArgument)
local function thunkMiddleware(nextDispatch, store)
return function(action)
if typeof(action) == "function" then
local ok, result = xpcall(function()
return action(store, extraArgument)
end, tracebackReporter)

if not ok then
-- report the error and move on so it's non-fatal app
store._errorReporter.reportReducerError(store:getState(), action, {
message = "Caught error in thunk",
thrownValue = result,
})
return nil
end

return result
end

return nextDispatch(action)
end
end

return thunkMiddleware
end

return makeThunkMiddleware
29 changes: 2 additions & 27 deletions src/thunkMiddleware.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,6 @@
This middleware consumes the function; middleware further down the chain
will not receive it.
]]
local function tracebackReporter(message)
return debug.traceback(message)
end
local makeThunkMiddleware = require(script.Parent.makeThunkMiddleware)

local function thunkMiddleware(nextDispatch, store)
return function(action)
if typeof(action) == "function" then
local ok, result = xpcall(function()
return action(store)
end, tracebackReporter)

if not ok then
-- report the error and move on so it's non-fatal app
store._errorReporter.reportReducerError(store:getState(), action, {
message = "Caught error in thunk",
thrownValue = result,
})
return nil
end

return result
end

return nextDispatch(action)
end
end

return thunkMiddleware
return makeThunkMiddleware(nil) -- no extra argument
22 changes: 22 additions & 0 deletions src/thunkMiddleware.spec.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
return function()
local Store = require(script.Parent.Store)
local thunkMiddleware = require(script.Parent.thunkMiddleware)
local makeThunkMiddleware = require(script.Parent.makeThunkMiddleware)

it("should dispatch thunks", function()
local function reducer(state, action)
Expand Down Expand Up @@ -117,4 +118,25 @@ return function()
store:dispatch(safeThunk)
expect(ranSafeThunk).to.equal(true)
end)

it("should send extra argument to thunks when provided", function()
local function reducer(state, action)
return state
end

local myExtraArg = { What = "MyExtraArg" }
local store = Store.new(reducer, {}, { makeThunkMiddleware(myExtraArg) })
local thunkCount = 0
local extraArgParam = nil

local function thunk(_store, extraArg)
thunkCount = thunkCount + 1
extraArgParam = extraArg
end

store:dispatch(thunk)

expect(thunkCount).to.equal(1)
expect(extraArgParam).to.equal(myExtraArg)
end)
end
Loading