TaskBuilder is an experimental DSL for performing async tasks (using OperationQueue), poweredby the new function builders feature in Swift 5.1.
func createImageTask(_ completion:@escaping(Data)->Void)->Task{Do{Action{print("Starting...")}LoadBigImageTask()Action{print("Load big image task finished!")}Passthrough(task:VeryBigImageTask()){ taskinAction{completion(task.imageData)}}Action{print("All done!")}}}letqueue=OperationQueue()lettask=createImageTask{print($0)}queue.addTask(task)TaskBuilder allows you to define tasks sequentially, rather than executing them at any order. This simply means that order in which you define the tasks is the order in which they are executed.
TaskBuilder comes with a very simple protocol, calledTask. ATask has two requirements - aperform() method andcompletionBlock property (which is simply() -> Void).
TaskBuilder also provides a default implementation ofTask onOperation so you can easily start using your existing operation classes.
ADo task is a top level task that encapsulates child tasks. Your task must always start with aDo at the top level.
AnAction task is used to perform any kind of simple action (that does not return). For example - callingprint(...), calling a completion handler, etc.
APassthrough is a special kind of task, that takes aTask as input and then provides that same task in a completion block once that task has finished executing. This allows you to access the properties and methods on that task once its completed.
For example - this can be useful when you want to pass the downloaded data to a completion handler.
This is an experimental package and is very basic in terms of functionality. For example - you cannot just create any kind of ordering you want, like putting anotherDo inside aPassthrough or anAction inside another.
This is simply a way for me to experiment with this new feature. I made this in a few hours, so don't be harsh 😄 I will be improving this with time, so stay tuned.
PRs are welcome if you want to contribute something!