Skip to content

Commit

Permalink
Merge pull request #643 from apollographql/change/result-type
Browse files Browse the repository at this point in the history
Switch to built-in `Result` type
  • Loading branch information
designatednerd committed Jul 17, 2019
2 parents 2bcbbff + 98e261f commit 1b0b68d
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 61 deletions.
8 changes: 4 additions & 4 deletions Apollo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
5AC6CA4322AAF7B200B7C94D /* GraphQLHTTPMethod.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5AC6CA4222AAF7B200B7C94D /* GraphQLHTTPMethod.swift */; };
9B95EDC022CAA0B000702BB2 /* GETTransformerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B95EDBF22CAA0AF00702BB2 /* GETTransformerTests.swift */; };
9BA1244A22D8A8EA00BF1D24 /* JSONSerialziation+Sorting.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9BA1244922D8A8EA00BF1D24 /* JSONSerialziation+Sorting.swift */; };
9BA1245E22DE116B00BF1D24 /* Result+Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9BA1245D22DE116B00BF1D24 /* Result+Helpers.swift */; };
9BDE43D122C6655300FD7C7F /* Cancellable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9BDE43D022C6655200FD7C7F /* Cancellable.swift */; };
9BDE43DD22C6705300FD7C7F /* GraphQLHTTPResponseError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9BDE43DC22C6705300FD7C7F /* GraphQLHTTPResponseError.swift */; };
9BDE43DF22C6708600FD7C7F /* GraphQLHTTPRequestError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9BDE43DE22C6708600FD7C7F /* GraphQLHTTPRequestError.swift */; };
Expand Down Expand Up @@ -96,7 +97,6 @@
9FE941D01E62C771007CDD89 /* Promise.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FE941CF1E62C771007CDD89 /* Promise.swift */; };
9FEB050D1DB5732300DA3B44 /* JSONSerializationFormat.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FEB050C1DB5732300DA3B44 /* JSONSerializationFormat.swift */; };
9FEC15B41E681DAD00D461B4 /* Collections.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FEC15B31E681DAD00D461B4 /* Collections.swift */; };
9FEC15B91E6965E300D461B4 /* Result.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FEC15B81E6965E300D461B4 /* Result.swift */; };
9FF33D811E48B98200F608A4 /* HTTPNetworkTransport.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F4DAF2D1E48B84B00EBFF0B /* HTTPNetworkTransport.swift */; };
9FF90A611DDDEB100034C3B6 /* GraphQLResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FF90A5B1DDDEB100034C3B6 /* GraphQLResponse.swift */; };
9FF90A651DDDEB100034C3B6 /* GraphQLExecutor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FF90A5C1DDDEB100034C3B6 /* GraphQLExecutor.swift */; };
Expand Down Expand Up @@ -254,6 +254,7 @@
90690D2522433CAF00FC2E54 /* Apollo-Target-TestSupport.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = "Apollo-Target-TestSupport.xcconfig"; sourceTree = "<group>"; };
9B95EDBF22CAA0AF00702BB2 /* GETTransformerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GETTransformerTests.swift; sourceTree = "<group>"; };
9BA1244922D8A8EA00BF1D24 /* JSONSerialziation+Sorting.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "JSONSerialziation+Sorting.swift"; sourceTree = "<group>"; };
9BA1245D22DE116B00BF1D24 /* Result+Helpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Result+Helpers.swift"; sourceTree = "<group>"; };
9BDE43D022C6655200FD7C7F /* Cancellable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Cancellable.swift; sourceTree = "<group>"; };
9BDE43DC22C6705300FD7C7F /* GraphQLHTTPResponseError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GraphQLHTTPResponseError.swift; sourceTree = "<group>"; };
9BDE43DE22C6708600FD7C7F /* GraphQLHTTPRequestError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GraphQLHTTPRequestError.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -355,7 +356,6 @@
9FE941CF1E62C771007CDD89 /* Promise.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Promise.swift; sourceTree = "<group>"; };
9FEB050C1DB5732300DA3B44 /* JSONSerializationFormat.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONSerializationFormat.swift; sourceTree = "<group>"; };
9FEC15B31E681DAD00D461B4 /* Collections.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Collections.swift; sourceTree = "<group>"; };
9FEC15B81E6965E300D461B4 /* Result.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Result.swift; sourceTree = "<group>"; };
9FF90A5B1DDDEB100034C3B6 /* GraphQLResponse.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GraphQLResponse.swift; sourceTree = "<group>"; };
9FF90A5C1DDDEB100034C3B6 /* GraphQLExecutor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GraphQLExecutor.swift; sourceTree = "<group>"; };
9FF90A6A1DDDEB420034C3B6 /* InputValueEncodingTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InputValueEncodingTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -639,8 +639,8 @@
children = (
9F578D8F1D8D2CB300C0EA36 /* Utilities.swift */,
9FCDFD221E33A0D8007519DC /* AsynchronousOperation.swift */,
9FEC15B81E6965E300D461B4 /* Result.swift */,
9FE941CF1E62C771007CDD89 /* Promise.swift */,
9BA1245D22DE116B00BF1D24 /* Result+Helpers.swift */,
9F19D8431EED568200C57247 /* ResultOrPromise.swift */,
9FEC15B31E681DAD00D461B4 /* Collections.swift */,
9FADC8491E6B0B2300C677E6 /* Locking.swift */,
Expand Down Expand Up @@ -1129,7 +1129,6 @@
9F7BA89922927A3700999B3B /* ResponsePath.swift in Sources */,
9FC9A9BD1E2C271C0023C4D5 /* RecordSet.swift in Sources */,
9BF1A95122CA6E71005292C2 /* GraphQLGETTransformer.swift in Sources */,
9FEC15B91E6965E300D461B4 /* Result.swift in Sources */,
9FADC84F1E6B865E00C677E6 /* DataLoader.swift in Sources */,
9FF90A611DDDEB100034C3B6 /* GraphQLResponse.swift in Sources */,
9F27D4641D40379500715680 /* JSONStandardTypeConversions.swift in Sources */,
Expand All @@ -1139,6 +1138,7 @@
9BDE43DF22C6708600FD7C7F /* GraphQLHTTPRequestError.swift in Sources */,
5AC6CA4322AAF7B200B7C94D /* GraphQLHTTPMethod.swift in Sources */,
9FE941D01E62C771007CDD89 /* Promise.swift in Sources */,
9BA1245E22DE116B00BF1D24 /* Result+Helpers.swift in Sources */,
9FC750631D2A59F600458D91 /* ApolloClient.swift in Sources */,
9F86B6901E65533D00B885FF /* GraphQLResponseGenerator.swift in Sources */,
9FC9A9C21E2D3CAF0023C4D5 /* GraphQLInputValue.swift in Sources */,
Expand Down
14 changes: 7 additions & 7 deletions Sources/Apollo/Promise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public final class Promise<Value> {
private let lock = Mutex()
private var state: State<Value>

private typealias ResultHandler<Value> = (Result<Value>) -> Void
private typealias ResultHandler<Value> = (Result<Value, Error>) -> Void
private var resultHandlers: [ResultHandler<Value>] = []

public init(resolved result: Result<Value>) {
public init(resolved result: Result<Value, Error>) {
state = .resolved(result)
}

Expand Down Expand Up @@ -72,7 +72,7 @@ public final class Promise<Value> {
}
}

public var result: Result<Value>? {
public var result: Result<Value, Error>? {
return lock.withLock {
switch state {
case .pending:
Expand All @@ -96,7 +96,7 @@ public final class Promise<Value> {
public func await() throws -> Value {
let semaphore = DispatchSemaphore(value: 0)

var result: Result<Value>? = nil
var result: Result<Value, Error>? = nil

whenResolved {
result = $0
Expand All @@ -105,7 +105,7 @@ public final class Promise<Value> {

semaphore.wait()

return try result!.valueOrError()
return try result!.get()
}

@discardableResult public func andThen(_ whenFulfilled: @escaping (Value) throws -> Void) -> Promise<Value> {
Expand Down Expand Up @@ -208,7 +208,7 @@ public final class Promise<Value> {
resolve(.failure(error))
}

private func resolve(_ result: Result<Value>) {
private func resolve(_ result: Result<Value, Error>) {
lock.withLock {
guard state.isPending else { return }

Expand Down Expand Up @@ -237,7 +237,7 @@ public final class Promise<Value> {

private enum State<Value> {
case pending
case resolved(Result<Value>)
case resolved(Result<Value, Error>)

var isPending: Bool {
if case .pending = self {
Expand Down
25 changes: 25 additions & 0 deletions Sources/Apollo/Result+Helpers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Foundation

extension Result {

/// Converts the result into an optional value. Returns the value for a `success` case and nil for a `failure` case.
var value: Success? {
switch self {
case .success(let value):
return value
case .failure:
return nil
}
}

/// Converts the result into an optional error. Returns the error for a `failure` case and nil for a `success` case.
/// Mostly useful for testing to make sure appropriate errors are received.
var error: Failure? {
switch self {
case .success:
return nil
case .failure(let error):
return error
}
}
}
44 changes: 0 additions & 44 deletions Sources/Apollo/Result.swift

This file was deleted.

10 changes: 5 additions & 5 deletions Sources/Apollo/ResultOrPromise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import Dispatch

public func whenAll<Value>(_ resultsOrPromises: [ResultOrPromise<Value>], notifyOn queue: DispatchQueue = .global()) -> ResultOrPromise<[Value]> {
onlyResults: do {
var results: [Result<Value>] = []
var results: [Result<Value, Error>] = []
for resultOrPromise in resultsOrPromises {
guard case .result(let result) = resultOrPromise else {
break onlyResults
}
results.append(result)
}
do {
let values = try results.map { try $0.valueOrError() }
let values = try results.map { try $0.get() }
return .result(.success(values))
} catch {
return .result(.failure(error))
Expand All @@ -37,7 +37,7 @@ public func whenAll<Value>(_ resultsOrPromises: [ResultOrPromise<Value>], notify
}

public enum ResultOrPromise<Value> {
case result(Result<Value>)
case result(Result<Value, Error>)
case promise(Promise<Value>)

public init(_ body: () throws -> Value) {
Expand All @@ -49,7 +49,7 @@ public enum ResultOrPromise<Value> {
}
}

public var result: Result<Value>? {
public var result: Result<Value, Error>? {
switch self {
case .result(let result):
return result
Expand All @@ -61,7 +61,7 @@ public enum ResultOrPromise<Value> {
public func await() throws -> Value {
switch self {
case .result(let result):
return try result.valueOrError()
return try result.get()
case .promise(let promise):
return try promise.await()
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/ApolloTestSupport/XCTestCase+Promise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ extension XCTestCase {

waitForExpectations(timeout: 5)

return try promise.result!.valueOrError()
return try promise.result!.get()
}
}

0 comments on commit 1b0b68d

Please sign in to comment.