Skip to content

Commit d13d220

Browse files
committed
adding when(fulfilled:) and when(resolved:) tuple overloads and an associated type R representing the result type in Thenable which improves when(resolved:) return type
1 parent 4d8d128 commit d13d220

17 files changed

+604
-218
lines changed

PromiseKit.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
0C42F31B1FCF86320051309C /* HangTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C42F3191FCF86240051309C /* HangTests.swift */; };
1313
0CC3AF2B1FCF84F7000E98C9 /* hang.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CC3AF2A1FCF84F7000E98C9 /* hang.swift */; };
1414
49A5584D1DC5185900E4D01B /* ResolverTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49A5584B1DC5172F00E4D01B /* ResolverTests.swift */; };
15+
6220BC2723114E580032BE32 /* TestError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6220BC2623114E580032BE32 /* TestError.swift */; };
1516
630A8056203CEF6800D25F23 /* AnyPromiseTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 630A8051203CEF6800D25F23 /* AnyPromiseTests.m */; settings = {COMPILER_FLAGS = "-fobjc-arc-exceptions"; }; };
1617
630A8057203CEF6800D25F23 /* PMKManifoldTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 630A8052203CEF6800D25F23 /* PMKManifoldTests.m */; };
1718
630A8058203CEF6800D25F23 /* JoinTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 630A8053203CEF6800D25F23 /* JoinTests.m */; };
@@ -144,6 +145,7 @@
144145
0C42F3191FCF86240051309C /* HangTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HangTests.swift; sourceTree = "<group>"; };
145146
0CC3AF2A1FCF84F7000E98C9 /* hang.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = hang.swift; path = Sources/hang.swift; sourceTree = "<group>"; };
146147
49A5584B1DC5172F00E4D01B /* ResolverTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ResolverTests.swift; sourceTree = "<group>"; };
148+
6220BC2623114E580032BE32 /* TestError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestError.swift; sourceTree = "<group>"; };
147149
630019221D596292003B4E30 /* PMKCoreTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = PMKCoreTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
148150
630A8051203CEF6800D25F23 /* AnyPromiseTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AnyPromiseTests.m; sourceTree = "<group>"; };
149151
630A8052203CEF6800D25F23 /* PMKManifoldTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PMKManifoldTests.m; sourceTree = "<group>"; };
@@ -352,6 +354,7 @@
352354
635D640D1D59635300BC0AF5 /* ZalgoTests.swift */,
353355
639BF755203DF02C00FA577B /* Utilities.swift */,
354356
085B96B121A6358900E5E22F /* LoggingTests.swift */,
357+
6220BC2623114E580032BE32 /* TestError.swift */,
355358
);
356359
name = Core;
357360
path = Tests/CorePromise;
@@ -757,6 +760,7 @@
757760
636A291F1F1C16FF001229C2 /* Box.swift in Sources */,
758761
63B0AC821D595E6300FA21D9 /* AnyPromise.m in Sources */,
759762
636A29231F1C17A6001229C2 /* Guarantee.swift in Sources */,
763+
6220BC2723114E580032BE32 /* TestError.swift in Sources */,
760764
636A291A1F1C156B001229C2 /* Promise.swift in Sources */,
761765
63B0AC8B1D595E6300FA21D9 /* join.m in Sources */,
762766
63B0AC891D595E6300FA21D9 /* hang.m in Sources */,

Sources/Guarantee.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ public final class Guarantee<T>: Thenable {
4545
}
4646

4747
/// - See: `Thenable.result`
48-
public var result: Result<T>? {
48+
public var result: T? {
4949
switch box.inspect() {
5050
case .pending:
5151
return nil
5252
case .resolved(let value):
53-
return .fulfilled(value)
53+
return value
5454
}
5555
}
5656

@@ -125,7 +125,6 @@ public extension Guarantee {
125125
any part of your chain may use. Like the main thread for example.
126126
*/
127127
func wait() -> T {
128-
129128
if Thread.isMainThread {
130129
conf.logHandler(.waitOnMainThread)
131130
}

Sources/Resolver.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public enum Result<T> {
8888
}
8989

9090
public extension PromiseKit.Result {
91+
9192
var isFulfilled: Bool {
9293
switch self {
9394
case .fulfilled:
@@ -96,4 +97,22 @@ public extension PromiseKit.Result {
9697
return false
9798
}
9899
}
100+
101+
var value: T? {
102+
switch self {
103+
case let .fulfilled(value):
104+
return value
105+
case .rejected:
106+
return nil
107+
}
108+
}
109+
110+
var error: Error? {
111+
switch self {
112+
case let .rejected(error):
113+
return error
114+
case .fulfilled:
115+
return nil
116+
}
117+
}
99118
}

Sources/Thenable.swift

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,21 @@ import Dispatch
44
public protocol Thenable: class {
55
/// The type of the wrapped value
66
associatedtype T
7+
/// The type of the result
8+
associatedtype R
79

810
/// `pipe` is immediately executed when this `Thenable` is resolved
911
func pipe(to: @escaping(Result<T>) -> Void)
1012

1113
/// The resolved result or nil if pending.
12-
var result: Result<T>? { get }
14+
var result: R? { get }
15+
16+
/// The resolved result's value or nil if pending.
17+
var value: T? { get }
1318
}
1419

1520
public extension Thenable {
21+
1622
/**
1723
The provided closure executes when this promise is fulfilled.
1824

@@ -217,60 +223,57 @@ public extension Thenable {
217223
}
218224

219225
public extension Thenable {
226+
220227
/**
221-
- Returns: The error with which this promise was rejected; `nil` if this promise is not rejected.
222-
*/
223-
var error: Error? {
224-
switch result {
225-
case .none:
226-
return nil
227-
case .some(.fulfilled):
228-
return nil
229-
case .some(.rejected(let error)):
230-
return error
231-
}
232-
}
233-
234-
/**
235-
- Returns: `true` if the promise has not yet resolved.
228+
- Returns: `true` if this thenable has not yet resolved.
236229
*/
237230
var isPending: Bool {
238231
return result == nil
239232
}
240233

241234
/**
242-
- Returns: `true` if the promise has resolved.
235+
- Returns: `true` if this thenable has resolved.
243236
*/
244237
var isResolved: Bool {
245238
return !isPending
246239
}
240+
}
241+
242+
public extension Thenable where R == T {
243+
244+
var value: T? {
245+
return result
246+
}
247+
}
247248

249+
public extension Thenable where R == Result<Self.T> {
250+
248251
/**
249-
- Returns: `true` if the promise was fulfilled.
252+
- Returns: `true` if this thenable was fulfilled.
250253
*/
251254
var isFulfilled: Bool {
252255
return value != nil
253256
}
254-
257+
258+
/**
259+
- Returns: The value with which this thenable was fulfilled or `nil` if this thenable is pending or rejected.
260+
*/
261+
var value: T? {
262+
return result?.value
263+
}
264+
255265
/**
256-
- Returns: `true` if the promise was rejected.
266+
- Returns: `true` if this thenable was rejected.
257267
*/
258268
var isRejected: Bool {
259269
return error != nil
260270
}
261-
271+
262272
/**
263-
- Returns: The value with which this promise was fulfilled or `nil` if this promise is pending or rejected.
273+
- Returns: The error with which this thenable was rejected; `nil` if this thenable is not rejected or pending.
264274
*/
265-
var value: T? {
266-
switch result {
267-
case .none:
268-
return nil
269-
case .some(.fulfilled(let value)):
270-
return value
271-
case .some(.rejected):
272-
return nil
273-
}
275+
var error: Error? {
276+
return result?.error
274277
}
275278
}
276279

0 commit comments

Comments
 (0)