Skip to content

Commit

Permalink
Rename Promise.init(result:) to Promise.init(with:)
Browse files Browse the repository at this point in the history
Also rename `Promise.init(on:result:after:)` to
`Promise.init(on:with:after:)`.

Fixes #40.
  • Loading branch information
lilyball committed Apr 27, 2019
1 parent 02c3740 commit f89626b
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 16 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,12 @@ Unless you explicitly state otherwise, any contribution intentionally submitted
- Add convenience functions for working with `Swift.Result` ([#39][]).
- Mark all the deprecated functions as unavailable instead. This restores the ability to write code like `promise.then({ foo?($0) })` without it incorrectly
resolving to the deprecated form of `map(_:)` ([#35][]).
- Rename `Promise.init(result:)` and `Promise.init(on:result:after:)` to `Promise.init(with:)` and `Promise.init(on:with:after:)` ([#40][]).

[#35]: https://github.com/lilyball/Tomorrowland/issues/35 "Move deprecations to unavailable"
[#37]: https://github.com/lilyball/Tomorrowland/issues/37 "DelayedPromise should conform to Equatable"
[#39]: https://github.com/lilyball/Tomorrowland/issues/39 "Add some minimal support for Result"
[#40]: https://github.com/lilyball/Tomorrowland/issues/40 "Rename Promise.init(result:) to Promise.init(with:)"

### v0.5.1

Expand Down
16 changes: 16 additions & 0 deletions Sources/Deprecations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@
// except according to those terms.
//

import typealias Foundation.TimeInterval

public extension Promise {
@available(*, deprecated, renamed: "init(with:)")
init(result: PromiseResult<Value,Error>) {
self.init(with: result)
}

@available(*, deprecated, renamed: "init(on:with:after:)")
init(on context: PromiseContext = .auto, result: PromiseResult<Value,Error>, after delay: TimeInterval) {
self.init(on: context, with: result, after: delay)
}
}

// MARK: - Great API Rename

public extension Promise {
@available(*, unavailable, renamed: "map(on:token:_:)")
func then<U>(on context: PromiseContext = .auto, token: PromiseInvalidationToken? = nil, _ onSuccess: @escaping (Value) -> U) -> Promise<U,Error> {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Promise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ public struct Promise<Value,Error> {
}

/// Returns a `Promise` that is already resolved with the given result.
public init(result: PromiseResult<Value,Error>) {
public init(with result: PromiseResult<Value,Error>) {
_seal = PromiseSeal(result: result)
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
// Add support for Swift.Result where it makes sense
extension Promise where Error: Swift.Error {
/// Returns a `Promise` that is already resolved with the given result.
public init(result: Result<Value,Error>) {
self.init(result: PromiseResult(result))
public init(with result: Result<Value,Error>) {
self.init(with: PromiseResult(result))
}
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ extension Promise {
/// - Parameter value: The value the promise will be fulfilled with.
/// - Parameter delay: The number of seconds to delay the promise by.
public init(on context: PromiseContext = .auto, fulfilled value: Value, after delay: TimeInterval) {
self.init(on: context, result: .value(value), after: delay)
self.init(on: context, with: .value(value), after: delay)
}

/// Returns a `Promise` that rejects with the given error after a delay.
Expand All @@ -52,7 +52,7 @@ extension Promise {
/// - Parameter error: The error the promise will be rejected with.
/// - Parameter delay: The number of seconds to delay the promise by.
public init(on context: PromiseContext = .auto, rejected error: Error, after delay: TimeInterval) {
self.init(on: context, result: .error(error), after: delay)
self.init(on: context, with: .error(error), after: delay)
}

/// Returns a `Promise` that resolves with the given result after a delay.
Expand All @@ -68,7 +68,7 @@ extension Promise {
/// delay has elapsed.
/// - Parameter result: The result the promise will be resolved with.
/// - Parameter delay: The number of seconds to delay the promise by.
public init(on context: PromiseContext = .auto, result: PromiseResult<Value,Error>, after delay: TimeInterval) {
public init(on context: PromiseContext = .auto, with result: PromiseResult<Value,Error>, after delay: TimeInterval) {
_seal = PromiseSeal()
let resolver = Resolver(box: _box)
let timer: DispatchSourceTimer
Expand Down
10 changes: 5 additions & 5 deletions Tests/PromiseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ final class PromiseTests: XCTestCase {
}

func testAlreadyFulfilledWithResult() {
let promise = Promise<Int,String>(result: .value(42))
let promise = Promise<Int,String>(with: .value(42))
XCTAssertEqual(promise.result, .value(42))
var invoked = false
_ = promise.then(on: .immediate) { (x) in
Expand All @@ -164,7 +164,7 @@ final class PromiseTests: XCTestCase {
}

func testAlreadyRejectedWithResult() {
let promise = Promise<Int,String>(result: .error("foo"))
let promise = Promise<Int,String>(with: .error("foo"))
XCTAssertEqual(promise.result, .error("foo"))
var invoked = false
_ = promise.catch(on: .immediate) { (x) in
Expand All @@ -174,7 +174,7 @@ final class PromiseTests: XCTestCase {
}

func testAlreadyCancelledWithResult() {
let promise = Promise<Int,String>(result: .cancelled)
let promise = Promise<Int,String>(with: .cancelled)
XCTAssertEqual(promise.result, .cancelled)
var invoked = false
_ = promise.onCancel(on: .immediate) {
Expand All @@ -186,7 +186,7 @@ final class PromiseTests: XCTestCase {
#if compiler(>=5)
func testAlreadyFulfilledWithSwiftResult() {
enum MyError: Error, Equatable { case foo }
let promise = Promise<Int,MyError>(result: .success(42))
let promise = Promise<Int,MyError>(with: .success(42))
XCTAssertEqual(promise.result, .value(42))
var invoked = false
_ = promise.then(on: .immediate) { (x) in
Expand All @@ -197,7 +197,7 @@ final class PromiseTests: XCTestCase {

func testAlreadyRejectedWithSwiftResult() {
enum MyError: Error, Equatable { case foo }
let promise = Promise<Int,MyError>(result: .failure(MyError.foo))
let promise = Promise<Int,MyError>(with: .failure(MyError.foo))
XCTAssertEqual(promise.result, .error(MyError.foo))
var invoked = false
_ = promise.catch(on: .immediate) { (x) in
Expand Down
10 changes: 5 additions & 5 deletions Tests/UtilityTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ final class UtilityTests: XCTestCase {
func testInitResultAfter() {
// NB: We're going to delay by a very short value, 50ms, so the tests are still speedy
let deadline = DispatchTime.now() + DispatchTimeInterval.milliseconds(50)
let promise = Promise<Int,String>(on: .userInteractive, result: .cancelled, after: 0.05)
let promise = Promise<Int,String>(on: .userInteractive, with: .cancelled, after: 0.05)
let expectation = XCTestExpectation(description: "promise")
var invoked: DispatchTime?
promise.always(on: .immediate) { (result) in
Expand All @@ -684,7 +684,7 @@ final class UtilityTests: XCTestCase {
}

func testInitResultAfterCancelledCancelsImmediately() {
let promise = Promise<Int,String>(on: .utility, result: .value(42), after: 1)
let promise = Promise<Int,String>(on: .utility, with: .value(42), after: 1)
XCTAssertNil(promise.result)
promise.requestCancel()
// Cancellation of the promise is synchronous
Expand All @@ -705,7 +705,7 @@ final class UtilityTests: XCTestCase {
expectation.fulfill()
}
}
let promise = Promise<Spy,String>(on: .utility, result: .value(Spy(expectation: expectation)), after: 1)
let promise = Promise<Spy,String>(on: .utility, with: .value(Spy(expectation: expectation)), after: 1)
promise.requestCancel()
wait(for: [expectation], timeout: 0.5)
}
Expand All @@ -714,7 +714,7 @@ final class UtilityTests: XCTestCase {
// NB: We're going to delay by a very short value, 50ms, so the tests are still speedy
let queue = OperationQueue()
let deadline = DispatchTime.now() + DispatchTimeInterval.milliseconds(50)
let promise = Promise<Int,String>(on: .operationQueue(queue), result: .value(42), after: 0.05)
let promise = Promise<Int,String>(on: .operationQueue(queue), with: .value(42), after: 0.05)
let expectation = XCTestExpectation(description: "promise")
var invoked: DispatchTime?
promise.always(on: .immediate) { (result) in
Expand All @@ -736,7 +736,7 @@ final class UtilityTests: XCTestCase {
// immediately, and thus it will have priority over later operations on the same queue.
let queue = OperationQueue()
queue.maxConcurrentOperationCount = 1
let promise = Promise<Int,String>(on: .operationQueue(queue), result: .value(42), after: 0.05)
let promise = Promise<Int,String>(on: .operationQueue(queue), with: .value(42), after: 0.05)
let expectation = XCTestExpectation(onSuccess: promise, expectedValue: 42)
queue.addOperation {
// block the queue for 50ms
Expand Down

0 comments on commit f89626b

Please sign in to comment.