Skip to content

Instantly share code, notes, and snippets.

@pixelrevision
Created December 19, 2018 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pixelrevision/29a5963d476e6fb7dcd1b31d438be130 to your computer and use it in GitHub Desktop.
Save pixelrevision/29a5963d476e6fb7dcd1b31d438be130 to your computer and use it in GitHub Desktop.
Protocol for common ReSwift middleware usecase
import ReSwift
public protocol MiddlewareProcessor {
typealias GetStateFunction = () -> Any?
func preprocess(dispatch: DispatchFunction, action: Action, getState: GetStateFunction)
func postprocess(dispatch: DispatchFunction, action: Action, getState: GetStateFunction)
}
public extension MiddlewareProcessor {
func preprocess(dispatch: DispatchFunction, action: Action, getState: GetStateFunction) {}
func postprocess(dispatch: DispatchFunction, action: Action, getState: GetStateFunction) {}
func middleware() -> Middleware<Any> {
return { dispatch, getState in
return { next in
return { action in
self.preprocess(dispatch: dispatch, action: action, getState: getState)
next(action)
self.postprocess(dispatch: dispatch, action: action, getState: getState)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment