Skip to content

Instantly share code, notes, and snippets.

@dimazen
Created February 21, 2017 15:53
Show Gist options
  • Save dimazen/d0c83bdc7c3618ab8bc0b40663370351 to your computer and use it in GitHub Desktop.
Save dimazen/d0c83bdc7c3618ab8bc0b40663370351 to your computer and use it in GitHub Desktop.
class NetworkMidddleware {
let container: Container
init(container: Container) {
self.container = container
}
func dispatch(_ dispatch: DispatchFunction?, getState: @escaping GetState) -> (@escaping DispatchFunction) -> DispatchFunction {
return { next in
return { action in
let retValue = next(action)
self._dispatch(dispatch, action: action, getState: getState)
return retValue
}
}
}
private func _dispatch(_ dispatch: DispatchFunction?, action: Action, getState: GetState) {
switch action {
case UserAction.getUser:
_ = dispatch?(UserAction.fetchStarted)
let network: Network = container.resolve()!
network.getUser { result in
_ = dispatch?(UserAction.fetchCompleted(result))
}
default:
break
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment