Skip to content

Commit

Permalink
Merge pull request #19 from vadymmarkov/feature/as-void
Browse files Browse the repository at this point in the history
Feature: asVoid
  • Loading branch information
vadymmarkov authored May 28, 2017
2 parents 54bd410 + c8c2622 commit 47995bb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 25 deletions.
33 changes: 24 additions & 9 deletions Playground.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import Foundation
import XCPlayground
import PlaygroundSupport
import When

enum Error: Swift.Error {
Expand All @@ -14,7 +15,7 @@ Promise({
return "String"
}).always({ result in
print("Always")
print(result.value)
print(result.value ?? "")
}).done({ value in
print(value)
}).fail({ error in
Expand Down Expand Up @@ -91,21 +92,35 @@ promise4

promise4.resolve("String")

// MARK: - Recover

let promise5 = Promise<String>()
promise5
.then({ value -> String in
throw Error.notFound
})
.recover({ error -> String in
return "String"
})
.done({ value in
print(value)
})

// MARK: - When

let promise5 = Promise<Int>()
let promise6 = Promise<String>()
let promise7 = Promise<Int>()
let promise6 = Promise<Int>()
let promise7 = Promise<String>()
let promise8 = Promise<Int>()

when(promise5, promise6, promise7)
when(promise6, promise7, promise8)
.done({ value1, value2, value3 in
print(value1)
print(value2)
print(value3)
})

promise5.resolve(1)
promise6.resolve("String")
promise7.resolve(3)
promise6.resolve(1)
promise7.resolve("String")
promise8.resolve(3)

XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
PlaygroundPage.current.needsIndefiniteExecution = true
10 changes: 5 additions & 5 deletions Sources/When/Functions.swift
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import Foundation

let mainQueue = DispatchQueue.main
let backgroundQueue = DispatchQueue.global(qos: DispatchQoS.QoSClass.background)
let instantQueue = DispatchQueue(label: "When.InstantQueue", attributes: [])
let barrierQueue = DispatchQueue(label: "When.BarrierQueue", attributes: DispatchQueue.Attributes.concurrent)

public func when<T, U>(_ p1: Promise<T>, _ p2: Promise<U>) -> Promise<(T, U)> {
return when([p1.asVoid(), p2.asVoid()]).then(on: instantQueue) { _ in
return when([p1.asVoid(on: instantQueue), p2.asVoid(on: instantQueue)]).then(on: instantQueue) { _ in
(p1.state.result!.value!, p2.state.result!.value!)
}
}

public func when<T, U, V>(_ p1: Promise<T>, _ p2: Promise<U>, _ p3: Promise<V>) -> Promise<(T, U, V)> {
return when([p1.asVoid(), p2.asVoid(), p3.asVoid()]).then(on: instantQueue) { _ in
(p1.state.result!.value!, p2.state.result!.value!, p3.state.result!.value!)
}
return when([p1.asVoid(on: instantQueue), p2.asVoid(on: instantQueue), p3.asVoid(on: instantQueue)])
.then(on: instantQueue, ({ _ in
(p1.state.result!.value!, p2.state.result!.value!, p3.state.result!.value!)
}))
}

public func when<T>(_ promises: [Promise<T>]) -> Promise<[T]> {
Expand Down
24 changes: 13 additions & 11 deletions Sources/When/Promise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ open class Promise<T> {
// MARK: - Initialization

/// Create a promise that resolves using a synchronous closure.
public init(queue: DispatchQueue = mainQueue, _ body: @escaping (Void) throws -> T) {
public init(queue: DispatchQueue = .main, _ body: @escaping (Void) throws -> T) {
state = .pending
self.queue = queue

Expand All @@ -31,7 +31,7 @@ open class Promise<T> {
}

/// Create a promise that resolves using an asynchronous closure that can either resolve or reject.
public init(queue: DispatchQueue = mainQueue,
public init(queue: DispatchQueue = .main,
_ body: @escaping (_ resolve: (T) -> Void, _ reject: (Error) -> Void) -> Void) {
state = .pending
self.queue = queue
Expand All @@ -42,7 +42,7 @@ open class Promise<T> {
}

/// Create a promise that resolves using an asynchronous closure that can only resolve.
public init(queue: DispatchQueue = mainQueue, _ body: @escaping (@escaping (T) -> Void) -> Void) {
public init(queue: DispatchQueue = .main, _ body: @escaping (@escaping (T) -> Void) -> Void) {
state = .pending
self.queue = queue

Expand All @@ -52,7 +52,7 @@ open class Promise<T> {
}

/// Create a promise with a given state.
public init(queue: DispatchQueue = mainQueue, state: State<T> = .pending) {
public init(queue: DispatchQueue = .main, state: State<T> = .pending) {
self.queue = queue
self.state = state
}
Expand Down Expand Up @@ -168,13 +168,13 @@ open class Promise<T> {
// MARK: - Then

extension Promise {
public func then<U>(on queue: DispatchQueue = mainQueue, _ body: @escaping (T) throws -> U) -> Promise<U> {
public func then<U>(on queue: DispatchQueue = .main, _ body: @escaping (T) throws -> U) -> Promise<U> {
let promise = Promise<U>(queue: queue)
addObserver(on: queue, promise: promise, body)
return promise
}

public func then<U>(on queue: DispatchQueue = mainQueue, _ body: @escaping (T) throws -> Promise<U>) -> Promise<U> {
public func then<U>(on queue: DispatchQueue = .main, _ body: @escaping (T) throws -> Promise<U>) -> Promise<U> {
let promise = Promise<U>(queue: queue)
addObserver(on: queue, promise: promise) { value -> U? in
let nextPromise = try body(value)
Expand Down Expand Up @@ -214,8 +214,11 @@ extension Promise {
update(state: state)
}

func asVoid() -> Promise<Void> {
return then(on: instantQueue) { _ in return }
/**
Returns a promise with Void as a result type.
*/
public func asVoid(on queue: DispatchQueue = .main) -> Promise<Void> {
return then(on: queue) { _ in return }
}
}

Expand All @@ -225,8 +228,7 @@ extension Promise {
/**
Helps to recover from certain errors. Continues the chain if a given closure does not throw.
*/
public func recover(on queue: DispatchQueue = mainQueue,
_ body: @escaping (Error) throws -> T) -> Promise<T> {
public func recover(on queue: DispatchQueue = .main, _ body: @escaping (Error) throws -> T) -> Promise<T> {
let promise = Promise<T>(queue: queue)
addRecoverObserver(on: queue, promise: promise, body)
return promise
Expand All @@ -235,7 +237,7 @@ extension Promise {
/**
Helps to recover from certain errors. Continues the chain if a given closure does not throw.
*/
public func recover(on queue: DispatchQueue = mainQueue,
public func recover(on queue: DispatchQueue = .main,
_ body: @escaping (Error) throws -> Promise<T>) -> Promise<T> {
let promise = Promise<T>(queue: queue)
addRecoverObserver(on: queue, promise: promise) { error -> T? in
Expand Down

0 comments on commit 47995bb

Please sign in to comment.