Skip to content

Commit

Permalink
Progress now returns its own promise \o/
Browse files Browse the repository at this point in the history
  • Loading branch information
s4cha committed Jul 28, 2016
1 parent 2c6ca26 commit d63042e
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 20 deletions.
60 changes: 48 additions & 12 deletions Source/Promise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public class Promise<T> {
private var successBlocks = [SuccessBlock]()
private typealias FailBlock = (ErrorType) -> Void
private var failBlocks = [FailBlock]()
private var progressBlock:(Float) -> Void = { t in }
private typealias ProgressBlock = (Float) -> Void
private var progressBlocks = [ProgressBlock]()
private var finallyBlock:() -> Void = { t in }
private var promiseCallBack:PromiseCallBack!
private var promiseProgressCallBack:PromiseProgressCallBack?
Expand Down Expand Up @@ -78,7 +79,9 @@ public class Promise<T> {
})
self.failBlocks.append(reject)
}
self.progressBlock = progress
self.progressBlocks.append({ p in
progress(p)
})
}
p.start()
passAlongFirstPromiseStartFunctionAndStateTo(p)
Expand Down Expand Up @@ -133,8 +136,8 @@ public class Promise<T> {
public func registerOnError(block:(ErrorType) -> Void) -> Promise<Void>{
let p = Promise<Void> { resolve, reject, progress in
switch self.state {
case .Fulfilled:()
reject(NSError(domain: "", code: 123, userInfo: nil))
case .Fulfilled:
reject(NSError(domain: "", code: 123, userInfo: nil))
// No error so do nothing.
case .Rejected:
// Already failed so call error block
Expand All @@ -150,7 +153,9 @@ public class Promise<T> {
resolve()
})
}
self.progressBlock = progress
self.progressBlocks.append({ p in
progress(p)
})
}
p.start()
passAlongFirstPromiseStartFunctionAndStateTo(p)
Expand All @@ -168,7 +173,7 @@ public class Promise<T> {
public func registerFinally<X>(block:() -> X) -> Promise<X>{
let p = Promise<X>{ resolve, reject, progress in
switch self.state {
case .Fulfilled:()
case .Fulfilled:
resolve(block())
case .Rejected:
resolve(block())
Expand All @@ -180,20 +185,47 @@ public class Promise<T> {
resolve(block())
})
}
self.progressBlock = progress
self.progressBlocks.append({ p in
progress(p)
})
}
p.start()
passAlongFirstPromiseStartFunctionAndStateTo(p)
return p
}

//MARK: - Progress

public func progress(block:(Float) -> Void) -> Self {
progressBlock = block
return self

public func progress(block:(Float) -> Void) -> Promise<Void> {
tryStartInitialPromise()
startPromiseIfNeeded()
return registerProgress(block)
}

public func registerProgress(block:(Float) -> Void) -> Promise<Void>{
let p = Promise<Void> { resolve, reject, progress in
switch self.state {
case .Fulfilled:
resolve()
case .Rejected:
reject(self.error!)
case .Pending:()
self.failBlocks.append(reject)
self.successBlocks.append({ _ in
resolve()
})
}
self.progressBlocks.append({ p in
block(p)
progress(p)
})
}
p.start()
passAlongFirstPromiseStartFunctionAndStateTo(p)
return p
}


//MARK: - Helpers

private func passAlongFirstPromiseStartFunctionAndStateTo<X>(p:Promise<X>) {
Expand Down Expand Up @@ -245,6 +277,10 @@ public class Promise<T> {

private func progressPromise(p:Float) {
progress = p
progressBlock(progress!)
for pb in progressBlocks {
if let progress = progress {
pb(progress)
}
}
}
}
40 changes: 32 additions & 8 deletions thenTests/thenTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -321,21 +321,34 @@ class thenTests: XCTestCase {
}

func testProgress() {

let progressExpectation = expectationWithDescription("thenExpectation")
let thenExpectation = expectationWithDescription("thenExpectation")

upload().registerThen {
print("Done")
let thenExpectation = expectationWithDescription("thenExpectation")
upload().progress { p in
print("PROGRESS \(p)")
XCTAssertEqual(p, 0.8)
progressExpectation.fulfill()
}.then {
print("Done")
thenExpectation.fulfill()
}.progress { p in
print("PROGRESS \(p)")
}.onError { e in
print("ERROR")
}

waitForExpectationsWithTimeout(3, handler: nil)
}

func testProgressFails() {
let progressExpectation = expectationWithDescription("thenExpectation")
let errorExpectation = expectationWithDescription("errorExpectation")
failingUpload().progress { p in
XCTAssertEqual(p, 0.8)
progressExpectation.fulfill()
}.then {
XCTFail()
}.onError { e in
errorExpectation.fulfill()
}

waitForExpectationsWithTimeout(3, handler: nil)
}

Expand Down Expand Up @@ -486,6 +499,17 @@ func upload() -> Promise<Void> {
}
}

func failingUpload() -> Promise<Void> {
return Promise<Void> { resolve, reject, progress in
wait {
progress(0.8)
wait {
reject(NSError(domain: "", code: 1223, userInfo: nil))
}
}
}
}

func promiseA() -> Promise<Int> {
return Promise { resolve, reject in
XCTAssertTrue(globalCount == 0)
Expand Down

0 comments on commit d63042e

Please sign in to comment.