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

Add extra argument to thunk middleware #69

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
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