Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement when function #57

Open
wants to merge 2 commits into
base: swift/2.0
Choose a base branch
from
Open

Conversation

bannzai
Copy link

@bannzai bannzai commented Feb 1, 2016

I implement "when" method.
It corresponds to multiple value type.

Usage

  • success pattern
        let task1 = Task<Float, String, NSError?> { fulfill, reject in fulfill("Success") }
        let task2 = Task<Float, Int, NSError?> { fulfill, reject in fulfill(1) }
        let task3 = Task<Float, Double, NSError?> { fulfill, reject in fulfill(1.1) }

        Task<Float, (String, Int, Double), NSError>.when((task1, task2, task3)).success { (string, int, double) -> Void in
            print(string) // -> Success
            print(int) // -> 1
            print(double) // -> 1.1
        }.failure { (error, isCancelled) -> Void in
            fatalError()
        }

*failure pattern

        let task1 = Task<Float, String, String> { fulfill, reject in fulfill("Success") }
        let task2 = Task<Float, Int, String> { fulfill, reject in fulfill(1) }
        let task3 = Task<Float, Double, String> { fulfill, reject in  reject("Rejected") }

        Task<Float, (String, Int, Double), String>.when((task1, task2, task3)).success { (string, int, double) -> Void in
            fatalError()
        }.failure { (error, isCancelled) -> Void in
            print(error) // -> Optional("Rejected")
        }

@inamiy
Copy link
Member

inamiy commented Feb 1, 2016

Hi, thank you for pull request :)

Currently, there is already Task.all() class method to achieve similar functionality.
For type-strict version, you can find scratch code in

public func zip<Progress2, Value2, Error2>(task2: Task<Progress2, Value2, Error2>) -> Task<(Progress?, Progress2?), (Value, Value2), (Error?, Error2?)>
{
return Task<(Progress?, Progress2?), (Value, Value2), (Error?, Error2?)> { machine, progress, fulfill, _reject, configure in
let t1 = self.convert(progress: _toAny, value: _toAny, error: _toAny)
let t2 = task2.convert(progress: _toAny, value: _toAny, error: _toAny)
let zipTask = Task<Any, Any, Any>.all([t1, t2])
configure.pause = { zipTask.pause() }
configure.resume = { zipTask.resume() }
configure.cancel = { zipTask.cancel() }
t1.progress { _, p in
if t2.value == nil && t2.errorInfo == nil {
progress((p as? Progress, t2.progress as? Progress2))
}
}
t2.progress { _, p in
if t1.value == nil && t1.errorInfo == nil {
progress((t1.progress as? Progress, p as? Progress2))
}
}
zipTask.success { (valueTuple: [Any]) -> Void in
fulfill((valueTuple[0] as! Value, valueTuple[1] as! Value2))
}.failure { _, isCancelled -> Void in
_reject((error: (t1.errorInfo?.error as? Error, t2.errorInfo?.error as? Error2), isCancelled: isCancelled))
}
}.name("\(self.name)-zip(\(task2.name))")
}
(not merged yet) which was first discussed in #44 .

I personally think we can just keep using variadic Task.all() for API simplicity.
What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants