GitXplorerGitXplorer
M

Wyrd

public
33 stars
3 forks
0 issues

Commits

List of commits on branch master.
Unverified
9e0b1c925d44b9bc783207a507885857d1b20574

parallel composition operators stub

MMaxDesiatov committed 11 years ago
Unverified
b943edfe5169f1e3cae65e325472989cbea460f7

implemented =! and =~ operators

MMaxDesiatov committed 11 years ago
Unverified
0b27a47a8f3d1c54b1b0551d8ae0396a5e72a03a

refined README.md

MMaxDesiatov committed 11 years ago
Unverified
afafad803048c0291142bd9ef07481b99876b3c0

fixed a typo in README.md

MMaxDesiatov committed 11 years ago
Unverified
a5aa5d0d6150c015be7c55c8657a62d58c5374b6

added syntax highlighting to README.md

MMaxDesiatov committed 11 years ago
Unverified
80cc57d58fd06a39f62f78e0b708293581170a47

Added README.md, fixed a few typos

MMaxDesiatov committed 11 years ago

README

The README file for this repository.

Wyrd

Wyrd is a library for asynchronous programming in Swift. It aims to be concise and simple. Wyrd is inspired by Promises/A+. Both Swift and Cocoa Touch doesn't provide any helpers for asynchronous programming besides standard functions taking success/failure callbacks. Wyrd tries to alleviate this with fairly simple API and a few helpers of its own.

How to Install

At the moment the most convenient way is to add Wyrd repository as a git submodule to your main repository:

git add submodule add https://github.com/explicitcall/Wyrd.git wyrd

Then add Wyrd.swift and Helpers.swift (add the latter if you will use helpers and most likely you will) files to your project.

CocoaPods package will be added as soon as CocoaPods will support source code in Swift.

How to Use

Essentially, Wyrd instance is a promise, which can be chained with other promises using closures and chaining operators. Wyrd library provides a few wrapper extensions for standard asynchronous Cocoa Touch functions. These extended functions return a promise instead of taking a success/error callback, giving you much clearer code and saving you from a Pyramid of Doom.

Obligatory example (getURLData and FullResponse typealias are provided to you by Wyrd):

let s = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
let u1 = NSURL(string: "https://api/endpoint1")
let u2 = NSURL(string: "https://api/endpoint2")
s.getURLData(u1) => { (full: FullResponse) -> Wyrd<FullResponse> in
  let (data, response) = full
  println("data1 length is \(data.length)")
  return s.getURLData(u2)
} =~ { (full: FullResponse) in
  let (data, response) = full
  println("data2 length is \(data.length)")
}

This code issues two API calls in serial, the second will fail if the first fails.

Wrapping typical asynchronous Cocoa Touch code is fairly easy, just define a method/function which will return a Wyrd instance, which you will be able to chain. You will need to fulfil or reject the promise in the raw callbacks code to indicate when the promise will be able to chain further:

typealias FullResponse = (NSData!, NSURLResponse!)

extension NSURLSession {
  func getURLData(url: NSURL) -> Wyrd<FullResponse> {
    let result = Wyrd<FullResponse>()

    let task = dataTaskWithURL(url) { data, response, error in
      if let e = error {
        result.reject(e)
      } else {
        result.fulfil(data, response)
      }
    }

    task.resume()

    return result
  }
}

More examples and wrapper functions are coming soon.