Swiftpack.co - Package - OlegKetrar/TaskKit

TaskKit

Carthage compatible

Features

  • Carthage support
  • Swift PM support
  • CocoaPods support
  • Result<T>
  • Action<Out>
  • LazyAction<In, Out> input can provided lazily
  • onSuccess/onFailure/onAny/always
  • map convert output
  • mapInput convert input
  • then/earlier compose sequence with another action
  • with(input) converts LazyAction to Action
  • ignoredOutput() ignores output
  • recover after error by providing recoveryValue / recoveryClosure
  • async / await / await(timeout:)
  • DispatchQueue.asyncValue
  • resolveOnQueue / execute on queue, completion on queue
  • zip / either / union compose actions
  • ☐ conditions onlyIf(_ closure:)
  • catch & Non-fallible Action
  • Optional<T>.unwrap() -> Action<T>

Usage

struct SomeError: Swift.Error {
  ...
}

// send Request and returns JSON
let downloadSmth = LazyAction<Request, JSON> { request, finish in
   request.send { response in
     if let json = response.json {
       finish(.success(json))
     } else {
       finish(.failure(SomeError()))
     }
   }
}

// parse User from JSON
let parseUser = LazyAction<JSON, User> { json, completion in
   DispatchQueue.global().async {

      // do heavy parsing on bg queue
      let parseResult = Result<User> { try User(json) }

      // it is recommended to always call completion on main queue
      DispatchQueue.main.async { completion(parseResult) }
   }
}

downloadSmth
   .onAny { print("request finished") }
   .then(parseUser)
   .onSuccess { print("user successfully parsed") }
   .onFailure { print("request failed or parsing failed") }
   .recover { error in
      if error is NetworkError {
         return someCachedUser // action failed and we recover by providing recover value
      } else {
         throw error // can't recover, so move error
      }
   }.always {
      // stop preloader
   }.map { user in
      // proccess user
      print("user email is \(user.email)")
   }.execute(with: FetchUserRequest())

Requirements

  • Swift 4+
  • xCode 9+
  • iOS 8.0+

Installation

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

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

github "OlegKetrar/TaskKit"

Run carthage update to build the framework and drag the built TaskKit.framework into your Xcode project.

Swift Package Manager

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler. It is in early development, but TaskKit does support its use on supported platforms.

Once you have your Swift package set up, adding TaskKit as a dependency is as easy as adding it to the dependencies value of your Package.swift.

dependencies: [
   .Package(url: "https://github.com/OlegKetrar/TaskKit")
]

License

TaskKit is released under the MIT license. See LICENSE for details.

Github

link
Stars: 2

Dependencies

Used By

Total: 0

Releases

Version 0.5.1 - 2017-11-24 13:24:18

  • added await(timeout:)
  • revert initializers, deprecate make and makeLazy
  • now async can be used on LazyAction

refinement, implement`async/await` - 2017-09-25 19:27:09

  • added finish(withValue:), finish(withError:), finish(with:)
  • removed Action implementation, Action now is just a typealias to LazyAction
  • added sync constructor for synchronous operations
  • added async constructor for execution synchronous code on background queues
  • added await() throws on Action for waiting action execution.
  • added execute(onSuccess:) same as onSuccess { .. }.execute()
  • replace initializers with static methods LazyAction.makeLazy and Action.make
  • added convenience then(_ work:) for creation actions

Version 0.4 - 2017-09-20 20:09:32

adding recover, fixing then

- 2017-09-19 20:36:27