Coder Social home page Coder Social logo

berzniz / sequencer.swift Goto Github PK

View Code? Open in Web Editor NEW
19.0 3.0 4.0 159 KB

Sequencer is an iOS library for asynchronous flow control written in Swift. A direct port of the Obj-C Sequencer: https://github.com/berzniz/Sequencer

License: MIT License

Swift 100.00%

sequencer.swift's Introduction

Sequencer.swift

Sequencer is an iOS library for asynchronous flow control written in Swift.

Sequencer turns complicated nested blocks logic into a clean, straightforward, and readable code.

This is a direct port of the Obj-C Sequencer.

Jumping straight to code

Let's output some strings to the Log Console:

let sequencer = Sequencer()
sequencer.enqueueStep() { result, next in
    println("* 1st Step")
    next(24)
}
sequencer.enqueueStep() { result, next in
    println("* 2nd Step")
    println("Got \(result) from previous step, let's double it")
    next(result as Int * 2)
}
sequencer.enqueueStep() { result, next in
    println("* 3nd Step - Async example")
    println("Got \(result) from previous step")
    println("This step is going to do some async work...")
    let popTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, 2 * 1000);
    dispatch_after(popTime, dispatch_get_main_queue(), {
        println("finished the async work.");
        next(result);
    });
}
sequencer.enqueueStep() {
    println("* 4th Step - A short one")
    $1($0)
}
sequencer.enqueueStep() { result, next in
    println("* Final Step")
    println("Got \(result) from previous step")
}
sequencer.run()

What does the above code do?

  1. A Sequencer was created.
  2. Five steps were enqueued to the Sequencer. The third step is async, but all the rest are plain sync code.
  3. Each step finishes by calling next() with a result object. This result is sent to the next step.
  4. We run the sequencer.

Note: Break the steps by just removing the call to next(nil). Everything will be cleaned-up auto-magically.

License

Sequencer is available under the MIT license:

Copyright (c) 2013 Tal Bereznitskey

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Contact

Find me on github: Tal Bereznitskey

Follow me on Twitter: @ketacode

sequencer.swift's People

Contributors

berzniz avatar sferrini avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  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.