Skip to content

Instantly share code, notes, and snippets.

@O-O-wl
Last active July 9, 2020 02:09
Show Gist options
  • Save O-O-wl/4ebd1e80b2d66798ce30f84ae312e19c to your computer and use it in GitHub Desktop.
Save O-O-wl/4ebd1e80b2d66798ce30f84ae312e19c to your computer and use it in GitHub Desktop.
example reduce
func reduce(_ action: Action, for currentState: State) -> State {
switch action {
// from User
case .typeQuery(let query):
let newState = currentState.mutate { $0.query = query }
sideEffects.accept(.requestAutoCompletions(for: query))
return newState
case .tapSearchButton:
let newState = currentState.mutate { $0.onProgress = true }
sideEffects.accept(.requestSearchResults(for: query))
return newState
// from Side Effect
case .fetchAutoCompletion(let autoCompletions):
let newState = currentState.mutate { $0.autoCompletions = autoCompletions }
return newState
case .fetchSearchResult(let searchResults):
let newState = currentState
.mutate({
$0.searchResults = searchResults
$0.onProgress = false
})
return newState
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment