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
func transform(newsReducer: (NewsState, NewsEvent) -> NewsState) -> Reducer |
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
func transform( | |
newsReducer: @escaping (NewsState, NewsEvent) -> NewsState, | |
stateKeyPath: WritableKeyPath<AppState, NewsState> | |
) -> Reducer { | |
return { appState, appEvent in | |
var appState = appState | |
appState[keyPath: stateKeyPath] = newsReducer(appState[keyPath: stateKeyPath], appEvent) | |
return appState | |
} | |
} |
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
enum AppEvent { | |
case newUserWasLoaded(User) | |
case newsEvents(NewsEvent) | |
var newsEvent: NewsEvent? { | |
guard case .newsEvents(let event) = self else { return nil } | |
return event | |
} | |
} |
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
func transform( | |
newsReducer: @escaping (NewsState, NewsEvent) -> NewsState, | |
stateKeyPath: WritableKeyPath<AppState, NewsState> | |
) -> Reducer { | |
return { appState, appEvent in | |
guard let newsEvent = appEvent.newsEvent else { return appState } | |
var appState = appState | |
appState[keyPath: stateKeyPath] = newsReducer(appState[keyPath: stateKeyPath], newsEvent) | |
return appState | |
} |
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
func transform( | |
newsReducer: @escaping (NewsState, NewsEvent) -> NewsState, | |
stateKeyPath: WritableKeyPath<AppState, NewsState>, | |
toNewsEvent: @escaping (AppEvent) -> NewsEvent? | |
) -> Reducer { | |
return { appState, appEvent in | |
guard let newsEvent = toNewsEvent(appEvent) else { return appState } | |
var appState = appState | |
appState[keyPath: stateKeyPath] = newsReducer(appState[keyPath: stateKeyPath], newsEvent) | |
return appState |
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
func combine<State, Event>(_ reducers: (State, Event) -> State...) -> (State, Event) -> State { | |
return { state, event in | |
reducers.reduce(state) { state, reducer in | |
reducer(state, event) | |
} | |
} | |
} | |
func transform<GlobalState, GlobalEvent, LocalState, LocalEvent>( | |
localReducer: @escaping (LocalState, LocalEvent) -> LocalState, | |
stateKeyPath: WritableKeyPath<GlobalState, LocalState?>, |
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
let appReducer: Reducer = { state, event in | |
var state = state | |
switch event { | |
case .newUserWasLoaded(let user): | |
state.user = user | |
default: | |
return state | |
} | |
return state | |
} |
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 NewsStore { | |
@Published private(set) var state: NewsState | |
private let reducer: (NewsState, NewsEvent) -> NewsState | |
init( | |
initialState: NewsState, | |
reducer: @escaping (NewsState, NewsEvent) -> NewsState | |
) { | |
self.state = initialState | |
self.reducer = reducer | |
} |
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 ViewController: UIViewController { | |
var cancellables: Set<AnyCancellable> = [] | |
} | |
enum InputEvent { | |
case newUserWasLoaded(User) | |
var user: User { | |
switch self { | |
case .newUserWasLoaded(let user): | |
return user | |
} |
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 ViewController: UIViewController { | |
var cancellables: Set<AnyCancellable> = [] | |
} | |
enum InputEvent { | |
case newUserWasLoaded(User) | |
var user: User { | |
switch self { | |
case .newUserWasLoaded(let user): | |
return user | |
} |