diff --git a/Sources/Deferred/ExistentialFuture.swift b/Sources/Deferred/ExistentialFuture.swift index 7ad7d9d2..07df98ad 100644 --- a/Sources/Deferred/ExistentialFuture.swift +++ b/Sources/Deferred/ExistentialFuture.swift @@ -3,7 +3,7 @@ // Deferred // // Created by Zachary Waldowski on 8/29/15. -// Copyright © 2014-2016 Big Nerd Ranch. Licensed under MIT. +// Copyright © 2014-2018 Big Nerd Ranch. Licensed under MIT. // import Dispatch @@ -134,11 +134,15 @@ public struct Future: FutureProtocol { self.box = Always(value: value) } - /// Create a future that will never get fulfilled. - public init() { + private init(never: ()) { self.box = Never() } + /// Create a future that will never get fulfilled. + public static var never: Future { + return Future(never: ()) + } + /// Create a future having the same underlying future as `other`. public init(_ other: Future) { self.box = other.box diff --git a/Sources/Task/ExistentialTask.swift b/Sources/Task/ExistentialTask.swift index 4a4e965c..5f298315 100644 --- a/Sources/Task/ExistentialTask.swift +++ b/Sources/Task/ExistentialTask.swift @@ -42,9 +42,8 @@ public final class Task: NSObject { self.progress = .taskRoot(for: progress) } - /// Create a task that will never complete. - public override init() { - self.future = Future() + private init(never: ()) { + self.future = .never self.progress = .indefinite() } @@ -81,12 +80,16 @@ public final class Task: NSObject { self.cancellation = cancellation ?? {} } - /// Create a task that will never complete. - public override init() { - self.future = Future() + private init(never: ()) { + self.future = .never self.cancellation = {} } #endif + + /// Create a task that will never complete. + public static var never: Task { + return Task(never: ()) + } } extension Task: FutureProtocol { diff --git a/Tests/DeferredTests/ExistentialFutureTests.swift b/Tests/DeferredTests/ExistentialFutureTests.swift index 253dea76..6b2496fe 100644 --- a/Tests/DeferredTests/ExistentialFutureTests.swift +++ b/Tests/DeferredTests/ExistentialFutureTests.swift @@ -41,7 +41,7 @@ class ExistentialFutureTests: XCTestCase { } func testPeekWhenUnfilled() { - anyFuture = Future() + anyFuture = Future.never let peek = anyFuture.peek() XCTAssertNil(peek) } @@ -103,7 +103,7 @@ class ExistentialFutureTests: XCTestCase { } func testDebugDescriptionUnfilled() { - let future = Future() + let future = Future.never XCTAssertEqual("\(future)", "Future(not filled)") } @@ -118,7 +118,7 @@ class ExistentialFutureTests: XCTestCase { } func testReflectionUnfilled() { - let future = Future() + let future = Future.never let magicMirror = Mirror(reflecting: future) XCTAssertEqual(magicMirror.displayStyle, .optional) diff --git a/Tests/TaskTests/TaskTests.swift b/Tests/TaskTests/TaskTests.swift index aa766df6..ac0a0e81 100755 --- a/Tests/TaskTests/TaskTests.swift +++ b/Tests/TaskTests/TaskTests.swift @@ -117,7 +117,7 @@ class TaskTests: CustomExecutorTestCase { func testThatAndThenForwardsCancellationToSubsequentTask() { let expect = expectation(description: "flatMapped task is cancelled") let task = makeAnyFinishedTask().andThen(upon: executor) { _ -> Task in - Task(future: Future()) { expect.fulfill() } + Task(future: .never) { expect.fulfill() } } task.cancel() @@ -227,7 +227,7 @@ class TaskTests: CustomExecutorTestCase { } func testThatTaskCreatedUnfilledIsIndeterminate() { - let task = Task() + let task = Task.never XCTAssert(task.progress.isIndeterminate) }