GitXplorerGitXplorer
M

Wyrd

public
33 stars
3 forks
0 issues

Commits

List of commits on branch master.
Unverified
f53403cda9b3e47779629ba068103540cdd2c816

shortened examples code in README.md

MMaxDesiatov committed 10 years ago
Unverified
ff8f6ce2684ea01b7f462c8412a236da9feaa671

added mention of Helpers.swift to README.md

MMaxDesiatov committed 10 years ago
Unverified
f2a4fb36403756717d24cad2925477280358f2d3

=~ and =! operators cleanup

MMaxDesiatov committed 10 years ago
Unverified
c0224bd2f6e9971699f1b92e086aee85d85574a2

introduced =| and =|| operators

MMaxDesiatov committed 10 years ago
Unverified
259d730425bddc87be0df4c667cbd1c6ba89389b

README.md updated (success renamed to =~)

MMaxDesiatov committed 10 years ago
Unverified
6edc7edfe2a7e870b400eca6d13c9921417913d0

fixes for Xcode 6 beta 4

MMaxDesiatov committed 10 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.