GithubHelp home page GithubHelp logo

theblixguy / taskbuilder Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 11 KB

Experimental DSL for creating async tasks using Swift 5.1 Function Builders

Swift 100.00%
swift functionbuilder async operations operationqueue swift5-1

taskbuilder's Introduction

TaskBuilder

TaskBuilder is an experimental DSL for performing async tasks (using OperationQueue), powered by 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()) { task in
      Action { completion(task.imageData) }
    }

    Action { print("All done!") }
  }
}

let queue = OperationQueue()
let task = 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, called Task. A Task has two requirements - a perform() method and completionBlock property (which is simply () -> Void).

TaskBuilder also provides a default implementation of Task on Operation so you can easily start using your existing operation classes.

Built-in tasks

Do

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

Action

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

Passthrough

A Passthrough is a special kind of task, that takes a Task 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 another Do inside a Passthrough or an Action 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!

taskbuilder's People

Contributors

theblixguy avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.