Skip to content

Instantly share code, notes, and snippets.

@monday8am
Last active October 10, 2018 20:34
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 monday8am/bcd3d13680185bd4d21b701402fbf2f2 to your computer and use it in GitHub Desktop.
Save monday8am/bcd3d13680185bd4d21b701402fbf2f2 to your computer and use it in GitHub Desktop.
Simple action creator in Swift
// 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