Last active
October 10, 2018 20:34
-
-
Save monday8am/bcd3d13680185bd4d21b701402fbf2f2 to your computer and use it in GitHub Desktop.
Simple action creator in Swift
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
// Action creator definition | |
func fetchAppsFromStore(state: State, store: Store<State>) -> Action? { | |
let request = AppStoreRequests.apps(showDevelopment: state.settingsState.isDevelopmentMode) | |
let task = JsonRequestTask<[AppId]>(dispatcher: dispatcher) | |
// Async task call | |
task.perform(request) | |
.subscribe(onNext: { result in | |
// Asycn result dispatched inside an action | |
store.dispatch(SetStoreApps(apps: .finished(apps))) | |
}, onError: { error in | |
// Asycn error dispatched inside an action | |
store.dispatch(SetStoreApps(apps: .failed(reason: error))) | |
}) | |
.disposed(by: disposer) | |
// Return an action that will be dispatched inmediatelly | |
return SetStoreApps(apps: .loading) | |
} | |
// Action creator call | |
store.dispatch(fetchAppsFromStore) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment