Skip to content

[Proposal][Any Idea]Invoke action without explicit dispatch call #18

@yonekawa

Description

@yonekawa

Currently, Action needs to call dispatcher.dispatch for async func.
For example.

func invoke(dispatcher: Dispatcher) {
     dispatcher.dispatch(self, result: Result(value: result))
}

It has some problem.
There is possibilities which developer forgets call dispatch.
And I want to hide dispatcher instance from action.

This is my ideal.

func invoke() -> Payload {
    let payload = anyFunc()
    return payload
}

This approach is more robust because compiler warns about missing return value and its type.
However, it is difficult because async func can't return value immediately.

My idea is this.

struct ActionA: Action {
    typealias Payload = Todo
    func invoke() throws -> Payload {
        return Todo()
    }
}

struct ActionB: AsyncAction {
    typealias Payload = Todo
    func invoke() -> Promise<Payload> {
        return Promise { fulfill, reject in
            asyncCall() { (todo) in
                fulfill(todo)
            }
        }
    }
}

So, divide action to normal and async.
Action can return payload immediately.
AsyncAction action uses Promise like as PromiseKit

Any idea?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions