Giter VIP home page Giter VIP logo

chain's Introduction

Chain

Swift 5.0 Platforms iOS Xcode 10.2

Method chaining of queued closure (blocks) on GCD (Grand Central Dispatch).

Chain is inspired by Async.

Features

  • Method chain of queued closures (blocks) on GCD.
  • Pass the object to the next queue
  • Less code indentation.

Exsample

Basics

Chain.main {
    // called first
    // called at main thread queue
    print("start")
    return "1"
    }.background { result in
         // called second
         // called at background qos class thread queue
         print(result)  // Optional(1)
         return "2"
    }.userInteractive { result in
         // called third
         // called at userInteractive qos class thread queue
         print(result)  // Optional(2)
         return "3"
    }.userInitiated { result in
         // called fourth
         // called at userInitiated qos class thread queue
         print(result)  // Optional(3)
         return "4"
    }.onDefault { result in
         // called fifth
         // called at default qos class thread queue
         print(result)  // Optional(4)
         return "5"
    }.run(.Main) { result in
         // called last
         // called at main thread queue
         print(result)  // Optional(5)
         print("completion")
}

Custom queue

let customQueue = dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0)

Chain.custom(customQueue) {
    // called first
    // called at customQueue
    return nil
    }.onDefault { result in
         // called second
         // called at default qos class thread queue
         return result
    }.main { result in
         // called third
         // called at main thread queue
         return result
    }.custom(customQueue) { result in
         // called fourth
         // called at customQueue
         return result
    }.run()

After

Chain.main {
    // called first
    // called at main thread queue
    print("start after: \(NSDate().description)")
    return "1"
    }.after(seconds: 5) { result in
        // called second
        // called to 5 seconds after the previous block
        // called at background qos class thread queue
        print(result)  // Optional(1)
        return "after 2: \(NSDate().description)"
    }.userInteractive { result in
        // called third
        // called at userInteractive qos class thread queue
        print(result)  // Optional(2)
        return "after 3: \(NSDate().description)"
    }.after(Queue.Utility, seconds: 5) { result in
        // called fourth
        // called to 5 seconds after the previous block
        // called at utility qos class thread queue
        print(result)  // Optional(3)
        return "after 4: \(NSDate().description)"
    }.run(.Main) { result in
        // called last
        // called at main thread queue
        print(result)  // Optional(4)
        print("after completion: \(NSDate().description)")
}

Wait

Chain.main {
    // called first
    // called at main thread queue
    print("start wait: \(NSDate().description)")
    return "1"
    }.wait(seconds: 5).background { result in
        // called to 5 seconds after the previous block
        // called at background qos class thread queue
        print("wait 2: \(NSDate().description)")
        return result
    }.wait(seconds: 5).main { result in
        // called to 5 seconds after the previous block
        // called at main thread queue
        print("wait 3: \(NSDate().description)")
        return result
    }.run()

Requirements

  • Xcode 10.2+
OS Swift
v1.2.x iOS 8+ 3.0
v1.3.x iOS 10+ 5.0

Installation

CocoaPods

Chain is available through CocoaPods. To install it, simply add the following line to your Podfile:

use_frameworks!

pod "SwChain"

Carthage

To integrate Chain into your Xcode project using Carthage, specify it in your Cartfile:

github "xxxAIRINxxx/Chain"

License

MIT license. See the LICENSE file for more info.

chain's People

Contributors

xxxairinxxx avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

smr spnkr

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.