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 placesService = PlacesService( | |
locale: .current, | |
apiKey: "<key here>", | |
fetcher: NetworkFetcher(session: .shared, decoder: JSONDecoder()) | |
) | |
let appStore = AppStore( | |
reducer: appReducer, | |
state: nil, | |
middleware: [ |
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 fetchPlaces(service: PlacesServing) -> SimpleMiddleware<AppState> { | |
return { (action, context) in | |
guard | |
let placesAction = action as? PlacesAction, | |
case .fetch = placesAction | |
else { | |
return action | |
} |
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 create(Dependency) -> Middleware |
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 fetchPlaces(action: Action, context: MiddlewareContext<AppState>) -> Action? { | |
// create places service | |
// perform network request | |
// dispatch set places action on success | |
} |
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 PlacesAction: Action { | |
case fetch | |
case set(PlacesSearchResult) | |
} |
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 logMiddleware(action: Action, context: MiddlewareContext<AppState>) -> Action? { | |
print(action) | |
return action | |
} | |
let actualMiddleware = createMiddleware(logMiddleware) |
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 logMiddleware: Middleware<StateType> = { dispatch, getState in | |
return { next in | |
return { action in | |
print(action) | |
next(action) | |
} | |
} | |
} |
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 typealias DispatchFunction = (Action) -> Void | |
public typealias Middleware<State> = (@escaping DispatchFunction, @escaping () -> State?) | |
-> (@escaping DispatchFunction) -> DispatchFunction |
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 fetchPlaces(state: AppState, store: AppStore) -> Action? { | |
let service = PlacesService([…]) | |
let fakeCoordinates = […] | |
let radius = […] | |
service.search(coordinates: fakeCoordinates, radius: radius) { result in | |
guard let places = result.value else { | |
return | |
} |
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
struct FetchPlacesAction: Action { } | |
class ViewController: UIViewController { | |
override func viewDidAppear(_ animated: Bool) { | |
super.viewDidAppear(animated) | |
appStore.dispatch(FetchPlacesAction()) | |
} | |
} |