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 State { | |
case initial | |
case loading | |
case loaded(data: [String]) | |
} | |
enum Event { | |
case dataLoaded(data: [String]) | |
case loadData | |
} | |
extension 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
public func scope<LocalState, LocalEvent: ComponentableEvent>( | |
state toLocalState: @escaping (State) -> LocalState, | |
event toGlobalEvent: @escaping (LocalEvent) -> Event, | |
feedBacks: [SideEffect<LocalState, LocalEvent>] | |
) -> Store<LocalState, LocalEvent> where LocalEvent.State == LocalState { | |
let localStore = Store<LocalState, LocalEvent>( | |
initial: toLocalState(state), | |
feedBacks: feedBacks, | |
reducer: { localState, localEvent in | |
var state = self.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
public func scope<LocalState, LocalEvent: ComponentableEvent>( | |
state toLocalState: @escaping (State) -> LocalState, | |
event toGlobalEvent: @escaping (LocalEvent) -> Event, | |
feedBacks: [SideEffect<LocalState, LocalEvent>] | |
) -> Store<LocalState, LocalEvent> where LocalEvent.State == LocalState { | |
let localStore = Store<LocalState, LocalEvent>( | |
initial: toLocalState(state), | |
feedBacks: feedBacks, | |
reducer: { localState, localEvent in | |
var state = self.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 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 | |
} |
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
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
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
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 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 | |
} |