Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Experimental DSL for creating async tasks using Swift 5.1 Function Builders

NotificationsYou must be signed in to change notification settings

theblixguy/TaskBuilder

Repository files navigation

TaskBuilder is an experimental DSL for performing async tasks (using OperationQueue), poweredby the new function builders feature in Swift 5.1.

Example

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)

Basics

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.

Built-in tasks

Do

ADo task is a top level task that encapsulates child tasks. Your task must always start with aDo at the top level.

Action

AnAction task is used to perform any kind of simple action (that does not return). For example - callingprint(...), calling a completion handler, etc.

Passthrough

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.

Limitations

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!

About

Experimental DSL for creating async tasks using Swift 5.1 Function Builders

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp