Skip to content

Commit

Permalink
Small changes to make adding all, any, and race easier.
Browse files Browse the repository at this point in the history
  • Loading branch information
jkolb committed Nov 16, 2016
1 parent 13fcd9c commit 548dc5a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
20 changes: 12 additions & 8 deletions Sources/Deferred.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,28 @@
*/

final class Deferred<Value> {
fileprivate let pendingPromise: AnyObject?
fileprivate let pending: Any?
var onFulfilled: [(Value) -> Void]
var onRejected: [(Error) -> Void]

convenience init() {
self.init(pendingPromise: nil, onFulfilled: [], onRejected: [])
self.init(pending: nil, onFulfilled: [], onRejected: [])
}

convenience init<P>(pendingOn: Promise<P>) {
self.init(pendingPromise: pendingOn, onFulfilled: [], onRejected: [])
convenience init(pending: Any) {
self.init(pending: pending, onFulfilled: [], onRejected: [])
}

convenience init<P>(pendingOn: Promise<P>, onFulfilled: [(Value) -> Void], onRejected: [(Error) -> Void]) {
self.init(pendingPromise: pendingOn, onFulfilled: onFulfilled, onRejected: onRejected)
convenience init<P>(pendingPromise: Promise<P>?) {
self.init(pending: pendingPromise, onFulfilled: [], onRejected: [])
}

fileprivate init(pendingPromise: AnyObject?, onFulfilled: [(Value) -> Void], onRejected: [(Error) -> Void]) {
self.pendingPromise = pendingPromise
convenience init<P>(pendingPromise: Promise<P>, onFulfilled: [(Value) -> Void], onRejected: [(Error) -> Void]) {
self.init(pending: pendingPromise, onFulfilled: onFulfilled, onRejected: onRejected)
}

fileprivate init(pending: Any?, onFulfilled: [(Value) -> Void], onRejected: [(Error) -> Void]) {
self.pending = pending
self.onFulfilled = onFulfilled
self.onRejected = onRejected
}
Expand Down
17 changes: 12 additions & 5 deletions Sources/Promise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final class Promise<Value> : Thenable {
}

public func then<ResultingValue>(on dispatcher: Dispatcher, onFulfilled: @escaping (Value) throws -> Result<ResultingValue>, onRejected: @escaping (Error) throws -> Result<ResultingValue>) -> Promise<ResultingValue> {
return Promise<ResultingValue>(pendingOn: self) { (resolve, reject) in
return Promise<ResultingValue>(pendingPromise: self) { (resolve, reject) in
self.onResolve(
fulfill: { (value) in
dispatcher.dispatch {
Expand All @@ -65,10 +65,17 @@ public final class Promise<Value> : Thenable {
)
}
}

init(pending: Any, _ resolver: (_ fulfill: @escaping (Value) -> Void, _ reject: @escaping (Error) -> Void) -> Void) {
self.lock = Lock()
self.state = .pending(Deferred(pending: pending))

resolver(weakifyFulfill(), weakifyReject())
}

fileprivate init<PendingValue>(pendingOn: Promise<PendingValue>, _ resolver: (_ resolve: @escaping (Result<Value>) -> Void, _ reject: @escaping (Error) -> Void) -> Void) {
fileprivate init<PendingValue>(pendingPromise: Promise<PendingValue>, _ resolver: (_ resolve: @escaping (Result<Value>) -> Void, _ reject: @escaping (Error) -> Void) -> Void) {
self.lock = Lock()
self.state = .pending(Deferred(pendingOn: pendingOn))
self.state = .pending(Deferred(pendingPromise: pendingPromise))

resolver(weakifyResolve(), weakifyReject())
}
Expand Down Expand Up @@ -130,7 +137,7 @@ public final class Promise<Value> : Thenable {

switch state {
case .pending(let deferred):
state = .pending(Deferred(pendingOn: promise, onFulfilled: deferred.onFulfilled, onRejected: deferred.onRejected))
state = .pending(Deferred(pendingPromise: promise, onFulfilled: deferred.onFulfilled, onRejected: deferred.onRejected))
lock.unlock()
promise.onResolve(fulfill: weakifyFulfill(), reject: weakifyReject())

Expand All @@ -157,7 +164,7 @@ public final class Promise<Value> : Thenable {
}
}

fileprivate func onResolve(fulfill: @escaping (Value) -> Void, reject: @escaping (Error) -> Void) {
func onResolve(fulfill: @escaping (Value) -> Void, reject: @escaping (Error) -> Void) {
lock.lock()

switch state {
Expand Down
2 changes: 1 addition & 1 deletion Sources/PromiseMaker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/

public final class PromiseMaker {
public static func makeUsing<Context, Value, InitialValue>(dispatcher: Dispatcher = GCDDispatcher.main, context: Context, builder: (((Context) -> Promise<InitialValue>) -> PromiseMakerHelper<Context, InitialValue>) -> PromiseMakerHelper<Context, Value>) -> Promise<Value> {
public static func makeUsing<Context : AnyObject, Value, InitialValue>(dispatcher: Dispatcher = GCDDispatcher.main, context: Context, builder: (((Context) -> Promise<InitialValue>) -> PromiseMakerHelper<Context, InitialValue>) -> PromiseMakerHelper<Context, Value>) -> Promise<Value> {
return builder { (initialBuilder) in
return PromiseMakerHelper<Context, InitialValue>(dispatcher: dispatcher, context: context, promise: initialBuilder(context))
}.promise
Expand Down

0 comments on commit 548dc5a

Please sign in to comment.