Created
February 21, 2017 15:53
-
-
Save dimazen/d0c83bdc7c3618ab8bc0b40663370351 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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