-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cherry-pick thunk extra-arg change onto 3.x patch branch (#94)
* Add extra argument to thunk middleware (#69) This feature is equivalent to what is available in newer versions of Redux: https://github.com/reduxjs/redux-thunk#injecting-a-custom-argument It allows arbitrary parameters to injected into thunks via a table passed in at Rodux initialization time (useful for passing in services or anything else we might want to mock for tests). It can replace other custom middlewares we've written to achieve the same thing (e.g. PaginatedFetchThunk.middleware, AvatarEditorServiceThunk.middleware, RNAdapterThunk.middleware) * Make it 3.2 * oops
- Loading branch information
1 parent
b59cc9d
commit 7ee2311
Showing
8 changed files
with
116 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters