Skip to content

Instantly share code, notes, and snippets.

View t-unit's full-sized avatar

Tobias Ottenweller t-unit

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