ReSwiftThunk
ReSwiftThunk copied to clipboard
A thunk middleware for ReSwift.
ReSwiftThunk
A thunk middleware for ReSwift.
⚠️ ReSwift now has a first-party version of thunk middelware. I recommend using the new version as it will be more actively maintained.
Installation
Cocoapods
pod 'ReSwiftThunk', '~> 1.0'
Carthage
github "mikecole20/ReSwiftThunk" ~> 1.0
Usage
Include the ThunkMiddleware when you create your ReSwift store.
let mainStore = Store<AppState>(
reducer: AppReducer(),
state: nil,
middleware: [ThunkMiddleware()]
)
Extend ThunkAction to define your asynchronous actions. Usually, they will dispatch regular Actions inside. I like to use functions to create the ThunkAction.
struct ActionResetPassword: Action {
}
struct ActionResetPasswordSuccess: Action {
}
struct ActionResetPasswordFailure: Action {
var error: Error
}
public func resetPassword(email: String) -> ThunkAction {
return ThunkAction(
action: { (dispatch, getState) in
_ = dispatch(ActionRestPassword())
let result = self.bellhopsSessionManager.request(Router.resetPassword(email: email)).validate().responseJSON()
return result.then { resp -> Any in
_ = dispatch(ActionResetPasswordSuccess())
return resp
}.recover { error -> Any in
_ = dispatch(ActionResetPasswordFailure(error: error))
throw error
}
}
)
}
Note that in this example, I am using PromiseKit so that the action returns a promise. This makes side effects in your views easy.
Then dispatch the ThunkAction.
let action = resetPassword(email: "[email protected]")
let result = mainStore.dispatch(action)
if let promise = result as? Promise<Any> {
promise.then { resp -> Void in
print("success")
}.catch { error in
print("failure")
}
}
mainStore.dispatch
Once again, this example is using promises. You can return anything you want though, because ReSwift only requires a return of
Any.