Latest 2.0.0
Homepage https://github.com/JARMourato/StepFlow
License MIT
Platforms ios 8.0, osx 10.10, tvos 9.0, watchos 2.0, requires ARC
Authors

Step Flow

StepFlow Logo

Swift 3.0
Platforms

Podspec
License

Build Status
codecov.io
codebeat badge

The purpose of StepFlow is to provide a simple way to layout a stream of macro-steps to execute and then run them. There are plenty good open source libraries that can be used to create Queues, or perform small tasks (either serially or concurrently), etc. So, in no way is this meant to replace them. Maybe in one step you want o use library X to download all images from some url and in another you want to use library Y to Queue image processing.

Note:

StepFlow requires swift 3.0 from version 2.0.0 onwards. If needed, use version 1.1.0 for swift 2.3 and version 1.0.0 for swift 2.2

Installation

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

pod 'StepFlow'

Usage

Basic setup

Just create the steps and use Flow to run them:


let stepOne = Step { stepFlow, previousResult in
  // previousResult in the first step is nil
  // code to be executed on Step 1
  stepFlow.finish(result: stepOneResult)
}

let stepTwo = Step { stepFlow, previousResult in
  // previousResult in the second step is the result of step 1
  // The error can be anything as long as it conforms to the ErrorType protocol
  // code to be executed on Step 2
  stepFlow.finish(error: someError)
}

Flow(steps: stepOne,stepTwo).start()

In the step closure, don’t forget to finish the step either with some result or some error. Or else, the flow will halt there.

Handle Closures

StepFlow provides closures you can use to be run when the flow finishes, ends with error or is cancelled. All handle closures are run on the main thread.

Finish

Flow(steps: stepOne,stepTwo).onFinish{ finishState in
  // code to be run after all steps finish
}.start()
Error

If you finish the step with error, the flow will end.


Flow(steps: stepOne,stepTwo).onError{ error in
  // code to be executed if a step failed. The error will be sent here
}.onFinish{ finishState in
  // If one step is finished with error. This block will not be executed.
  // Unless, no onError closure is provided, in which case the Flow will 
  // default to this closure with finishState = .Failed(let error)
}.start()
Cancel

If the flow is canceled.


let f = Flow(steps: stepOne,stepTwo).onCancel{ 
  // code to be executed if the flow is canceled.
}.onFinish{ finishState in
  // If the flow is canceled. This block will not be executed.
  // Unless, no onCancel closure is provided, in which case the Flow will 
  // default to this closure with finishState = .Canceled
}

f.start()
f.cancel()

Async Steps

It is not the purpose of StepFlow to provide a way of run code asynchronously. As stated above, there are plenty of other good options, well documented, which you can use alongside with StepFlow. Nonetheless, you can run a step in a background thread by setting it on when creating a step.


let stepAsync = Step(onBackgroundThread : true) { stepFlow, previousResult in
  // previousResult in the first step is nil
  // code to be executed on a background thread
  stepFlow.finish(result: stepOneResult)
}

By default all steps are run on the calling thread.

License

Step Flow is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "StepFlow",
    "version": "2.0.0",
    "license": "MIT",
    "summary": "StepFlow is way to create a stream of macro steps to be executed.",
    "homepage": "https://github.com/JARMourato/StepFlow",
    "authors": {
        "Jou00e3o Mourato": "[email protected]"
    },
    "source": {
        "git": "https://github.com/JARMourato/StepFlow.git",
        "tag": "2.0.0"
    },
    "requires_arc": true,
    "platforms": {
        "ios": "8.0",
        "osx": "10.10",
        "tvos": "9.0",
        "watchos": "2.0"
    },
    "source_files": "Sources/StepFlow/*.swift",
    "module_name": "StepFlow"
}

Pin It on Pinterest

Share This