Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Using await/async in middleware #131

Open
@armoona

Description

@armoona

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp