Skip to content

Implement when function - #57

Open
bannzai wants to merge 2 commits into
ReactKit:swift/2.0from
bannzai:develop/when
Open

Implement when function#57
bannzai wants to merge 2 commits into
ReactKit:swift/2.0from
bannzai:develop/when

Conversation

@bannzai

Copy link
Copy Markdown

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

Usage

  • success pattern
lettask1=Task<Float,String,NSError?>{ fulfill, reject infulfill("Success")}lettask2=Task<Float,Int,NSError?>{ fulfill, reject infulfill(1)}lettask3=Task<Float,Double,NSError?>{ fulfill, reject infulfill(1.1)}Task<Float,(String,Int,Double),NSError>.when((task1, task2, task3)).success{(string, int, double)->Voidinprint(string) // -> Success
print(int) // -> 1
print(double) // -> 1.1
}.failure{(error, isCancelled)->VoidinfatalError()}

*failure pattern

lettask1=Task<Float,String,String>{ fulfill, reject infulfill("Success")}lettask2=Task<Float,Int,String>{ fulfill, reject infulfill(1)}lettask3=Task<Float,Double,String>{ fulfill, reject inreject("Rejected")}Task<Float,(String,Int,Double),String>.when((task1, task2, task3)).success{(string, int, double)->VoidinfatalError()}.failure{(error, isCancelled)->Voidinprint(error) // -> Optional("Rejected")
}

@inamiy

Copy link
Copy Markdown
Member

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

publicfunc zip<Progress2, Value2, Error2>(task2:Task<Progress2,Value2,Error2>)->Task<(Progress?,Progress2?),(Value,Value2),(Error?,Error2?)>
{
returnTask<(Progress?,Progress2?),(Value,Value2),(Error?,Error2?)>{ machine, progress, fulfill, _reject, configure in
lett1=self.convert(progress: _toAny, value: _toAny, error: _toAny)
lett2= task2.convert(progress: _toAny, value: _toAny, error: _toAny)
letzipTask=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])->Voidin
fulfill((valueTuple[0]as!Value,valueTuple[1]as!Value2))
}.failure{ _, isCancelled ->Voidin
_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 freeto 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.

2 participants

@bannzai@inamiy