Latest 0.0.4
Homepage https://github.com/mgadda/typed-operation
License Apache License Version 2.0
Platforms ios 8.0, osx 10.9, requires ARC
Authors

Typed Operation

Overview

TypedOperation provides type-safe chainable, nestable composition of NSOperations.

Build Status

Examples

Chaining

func doComputation() -> Int

let result: TypedOperation<Int> = TypedOperation {
  doComputation()
}

result.map { someValue in
  someValue * 10
}

try result.awaitResult() // 100

Nesting

TypedOperation<Int>(constant: 10).flatMap { someInt in
  TypedOperation(constant: someInt * 2)
}.awaitResult() // -> 20 :: Int

Error Handling

enum Error: ErrorType {
  case SomeError
}

let handled = TypedOperation<Int>(error: Error.SomeError).handle { error in
  return 10
}
handled.awaitResult() // => 10

Recover using another TypedOperation:


let rescued = TypedOperation<Int>(error: Error.SomeError).rescue { error in
  return TypedOperation<Int>(constant: 10)
}

rescued.awaitResult() // => 10

For side-effects

TypedOperation(constant: 10).onSuccess { result in
  print("Result was (result)")
}.onFailure { error in
  print("Error was (error)")
}

Joining concurrent operations

let joinedOperation = TypedOperation(constant: 10).join(TypedOperation(constant: 20))
try joinedOperation.awaitResult() // => Tuple2(10, 20)

Wrapping NSURLSessionTasks

Many existing libraries offer asynchronous methods which accept callback functions.
Using AsynchOperationAdapter, you can wrap those existing asynchonrous operations
to produce TypedOperations.

protocol SomeService {
  func doAsyncThing(callback: (ResultType?, NSError?) -> ())
}

let service: SomeService = /* ... */

AsyncOperationAdapter<ResultType> { service.doAsyncThing(callback: $0) }

Or using NSURLSessions:

let url: NSURL! = NSURL(string: "http://api.example.com")
let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())

let op = AsyncOperationAdapter<NSData> { completionHandler in
  let task = session.dataTaskWithURL(url) { (maybeData, maybeResponse, maybeError) in
    completionHandler(maybeData, maybeError)
  }
  task.resume()
}
op.onSuccess { (response: NSData) in
  print(response)
}

Installation

TypedOperation is available as a cocoapod. In your Podfile, add:

pod 'TypedOperation'

Then do the normal thing and run:

$ pod install

If all goes well, open the generated (or updated) xcworkspace and
add the following to any file where you’d like to use a TypedOperation:

import TypedOperation

Latest podspec

{
    "name": "TypedOperation",
    "version": "0.0.4",
    "summary": "TypedOperation implements type-safe chainable, nestable composition of NSOperations.",
    "homepage": "https://github.com/mgadda/typed-operation",
    "license": {
        "type": "Apache License Version 2.0",
        "file": "LICENSE.txt"
    },
    "authors": {
        "Matt Gadda": "[email protected]"
    },
    "social_media_url": "http://twitter.com/mgadda",
    "platforms": {
        "ios": "8.0",
        "osx": "10.9"
    },
    "source": {
        "git": "https://github.com/mgadda/typed-operation.git",
        "tag": "0.0.4"
    },
    "source_files": "Sources",
    "requires_arc": true,
    "xcconfig": {
        "OS_OBJECT_USE_OBJC": 1
    }
}

Pin It on Pinterest

Share This