yonekawa / SwiftFlux

A type-safe Flux implementation for Swift
MIT License
237 stars 19 forks source link

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

Open yonekawa opened 8 years ago

yonekawa commented 8 years ago

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?

mortyccp commented 8 years ago

I think the idea of middleware of Redux.js would be useful.