- Notifications
You must be signed in to change notification settings - Fork30
Open
Description
I am trying to use await/async in middleware. For example, rewriting the sample from the docs, is this correct?
public final class FavoritesAPIMiddleware: MiddlewareProtocol { public typealias InputActionType = FavoritesAction // It wants to receive only actions related to Favorites public typealias OutputActionType = FavoritesAction // It wants to also dispatch actions related to Favorites public typealias StateType = FavoritesModel // It wants to read the app state that manages favorites private let api: API public init(api: API) { self.api = api } public func handle(action: InputActionType, from dispatcher: ActionSource, state: @escaping GetState<StateType>) -> IO<OutputActionType> { guard case let .toggleFavorite(movieId) = action else { return .pure() } let favoritesList = state() // state before reducer let makeFavorite = !favoritesList.contains(where: { $0.id == movieId }) return IO { [weak self] output in guard let self = self else { return } ///////// START ASYNC CODE Task.init { do { let result = try await self.api.changeFavorite(id: movieId, makeFavorite: makeFavorite) switch result { case let .success(value): output.dispatch(.changedFavorite(movieId, isFavorite: makeFavorite), info: "API.changeFavorite callback") case let .failure(error): output.dispatch(.changedFavoriteHasFailed(movieId, isFavorite: !makeFavorite, error: error), info: "api.changeFavorite callback") } } catch { // .. handle error } } ///////// END ASYNC CODE } }}Metadata
Metadata
Assignees
Labels
No labels