Skip to content

Commit

Permalink
Rename FutureValue to FutureResult, add result property to Future
Browse files Browse the repository at this point in the history
  • Loading branch information
davidask committed Sep 26, 2018
1 parent a73b16c commit 50045d3
Show file tree
Hide file tree
Showing 41 changed files with 445 additions and 379 deletions.
20 changes: 10 additions & 10 deletions Futures.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

/* Begin PBXBuildFile section */
B757AE1520B8921E00850F92 /* Futures.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B757AE0B20B8921E00850F92 /* Futures.framework */; };
B757AE7620B8A5BA00850F92 /* FutureValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = B757AE7320B8A5BA00850F92 /* FutureValue.swift */; };
B757AE7720B8A5BA00850F92 /* FutureValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = B757AE7320B8A5BA00850F92 /* FutureValue.swift */; };
B757AE7820B8A5BA00850F92 /* FutureValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = B757AE7320B8A5BA00850F92 /* FutureValue.swift */; };
B757AE7920B8A5BA00850F92 /* FutureValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = B757AE7320B8A5BA00850F92 /* FutureValue.swift */; };
B757AE7620B8A5BA00850F92 /* FutureResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = B757AE7320B8A5BA00850F92 /* FutureResult.swift */; };
B757AE7720B8A5BA00850F92 /* FutureResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = B757AE7320B8A5BA00850F92 /* FutureResult.swift */; };
B757AE7820B8A5BA00850F92 /* FutureResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = B757AE7320B8A5BA00850F92 /* FutureResult.swift */; };
B757AE7920B8A5BA00850F92 /* FutureResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = B757AE7320B8A5BA00850F92 /* FutureResult.swift */; };
B757AE7A20B8A5BA00850F92 /* DispatchQueue+Promise.swift in Sources */ = {isa = PBXBuildFile; fileRef = B757AE7420B8A5BA00850F92 /* DispatchQueue+Promise.swift */; };
B757AE7B20B8A5BA00850F92 /* DispatchQueue+Promise.swift in Sources */ = {isa = PBXBuildFile; fileRef = B757AE7420B8A5BA00850F92 /* DispatchQueue+Promise.swift */; };
B757AE7C20B8A5BA00850F92 /* DispatchQueue+Promise.swift in Sources */ = {isa = PBXBuildFile; fileRef = B757AE7420B8A5BA00850F92 /* DispatchQueue+Promise.swift */; };
Expand Down Expand Up @@ -52,7 +52,7 @@
B757AE2A20B8924600850F92 /* Futures.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Futures.framework; sourceTree = BUILT_PRODUCTS_DIR; };
B757AE3720B8926D00850F92 /* Futures.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Futures.framework; sourceTree = BUILT_PRODUCTS_DIR; };
B757AE4420B8928200850F92 /* Futures.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Futures.framework; sourceTree = BUILT_PRODUCTS_DIR; };
B757AE7320B8A5BA00850F92 /* FutureValue.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FutureValue.swift; sourceTree = "<group>"; };
B757AE7320B8A5BA00850F92 /* FutureResult.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FutureResult.swift; sourceTree = "<group>"; };
B757AE7420B8A5BA00850F92 /* DispatchQueue+Promise.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "DispatchQueue+Promise.swift"; sourceTree = "<group>"; };
B757AE7520B8A5BA00850F92 /* Future.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Future.swift; sourceTree = "<group>"; };
B757AE8420B8A5C700850F92 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -137,7 +137,7 @@
B757AE7220B8A5BA00850F92 /* Future */ = {
isa = PBXGroup;
children = (
B757AE7320B8A5BA00850F92 /* FutureValue.swift */,
B757AE7320B8A5BA00850F92 /* FutureResult.swift */,
B757AE7420B8A5BA00850F92 /* DispatchQueue+Promise.swift */,
B757AE7520B8A5BA00850F92 /* Future.swift */,
B79FF3AC20BC6D1E001A625C /* Promise.swift */,
Expand Down Expand Up @@ -483,7 +483,7 @@
B757AE7E20B8A5BA00850F92 /* Future.swift in Sources */,
B79FF3AD20BC6D1E001A625C /* Promise.swift in Sources */,
B768073820C6C02E00402ECB /* FutureProvider.swift in Sources */,
B757AE7620B8A5BA00850F92 /* FutureValue.swift in Sources */,
B757AE7620B8A5BA00850F92 /* FutureResult.swift in Sources */,
B757AE7A20B8A5BA00850F92 /* DispatchQueue+Promise.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -503,7 +503,7 @@
B79FF3B820BC6E34001A625C /* AnyFuture.swift in Sources */,
B757AE7F20B8A5BA00850F92 /* Future.swift in Sources */,
B79FF3AE20BC6D1E001A625C /* Promise.swift in Sources */,
B757AE7720B8A5BA00850F92 /* FutureValue.swift in Sources */,
B757AE7720B8A5BA00850F92 /* FutureResult.swift in Sources */,
B757AE7B20B8A5BA00850F92 /* DispatchQueue+Promise.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -515,7 +515,7 @@
B79FF3B920BC6E34001A625C /* AnyFuture.swift in Sources */,
B757AE8020B8A5BA00850F92 /* Future.swift in Sources */,
B79FF3AF20BC6D1E001A625C /* Promise.swift in Sources */,
B757AE7820B8A5BA00850F92 /* FutureValue.swift in Sources */,
B757AE7820B8A5BA00850F92 /* FutureResult.swift in Sources */,
B757AE7C20B8A5BA00850F92 /* DispatchQueue+Promise.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -527,7 +527,7 @@
B79FF3BA20BC6E34001A625C /* AnyFuture.swift in Sources */,
B757AE8120B8A5BA00850F92 /* Future.swift in Sources */,
B79FF3B020BC6D1E001A625C /* Promise.swift in Sources */,
B757AE7920B8A5BA00850F92 /* FutureValue.swift in Sources */,
B757AE7920B8A5BA00850F92 /* FutureResult.swift in Sources */,
B757AE7D20B8A5BA00850F92 /* DispatchQueue+Promise.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
36 changes: 23 additions & 13 deletions Sources/Futures/Future.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Dispatch

private enum FutureState<Value> {
private enum FutureState<Result> {
case pending
case finished(FutureValue<Value>)
case finished(FutureResult<Result>)

fileprivate func canTransition(to newState: FutureState<Value>) -> Bool {
fileprivate func canTransition(to newState: FutureState<Result>) -> Bool {

switch (self, newState) {
case (.pending, .finished):
Expand Down Expand Up @@ -39,13 +39,13 @@ public protocol AnyFutureObserver: class {
/// - `Future<T>.whenRejected()`
public final class FutureObserver<T>: AnyFutureObserver {

private let callback: (FutureValue<T>) -> Void
private let callback: (FutureResult<T>) -> Void

private let queue: DispatchQueue

private weak var future: Future<T>?

fileprivate init(_ callback: @escaping (FutureValue<T>) -> Void, future: Future<T>, queue: DispatchQueue) {
fileprivate init(_ callback: @escaping (FutureResult<T>) -> Void, future: Future<T>, queue: DispatchQueue) {
self.callback = callback
self.future = future
self.queue = queue
Expand All @@ -57,7 +57,7 @@ public final class FutureObserver<T>: AnyFutureObserver {
future?.removeObserver(self)
}

fileprivate func invoke(_ value: FutureValue<T>) {
fileprivate func invoke(_ value: FutureResult<T>) {
queue.async {
self.callback(value)
}
Expand Down Expand Up @@ -136,8 +136,8 @@ public final class Future<T>: AnyFuture {

/// Creates a resolved future
///
/// - Parameter resolved: `FutureValue<T>`
public init(resolved: FutureValue<T>) {
/// - Parameter resolved: `FutureResult<T>`
public init(resolved: FutureResult<T>) {
state = .finished(resolved)
}

Expand Down Expand Up @@ -174,7 +174,7 @@ public final class Future<T>: AnyFuture {
}
}

fileprivate func setValue(_ value: FutureValue<T>) {
fileprivate func setValue(_ value: FutureResult<T>) {
stateQueue.sync(flags: .barrier) {

guard case .pending = state else {
Expand Down Expand Up @@ -223,6 +223,16 @@ public final class Future<T>: AnyFuture {
return true
}
}

/// Indicates the result of the future.
/// Returns `nil` if the future is not resolved yet.
public var result: FutureResult<T>? {
guard case .finished(let result) = state else {
return nil
}

return result
}
}

extension Future: Equatable {
Expand Down Expand Up @@ -530,12 +540,12 @@ public extension Future {
///
/// - Parameters:
/// - queue: Dispatch queue to observe on.
/// - callback: Callback invoked with the resolved `FutureValue<T>` of this future
/// - callback: Callback invoked with the resolved `FutureResult<T>` of this future
/// - Returns: A `FutureObserver<T>`, which can be used to remove the observer from this future.
@discardableResult
func whenResolved(
on queue: DispatchQueue = .futures,
callback: @escaping (FutureValue<T>) -> Void) -> FutureObserver<T> {
callback: @escaping (FutureResult<T>) -> Void) -> FutureObserver<T> {
let observer = FutureObserver<T>(callback, future: self, queue: queue)

self.addObserver(observer)
Expand Down Expand Up @@ -563,8 +573,8 @@ public extension Promise {

/// Resolves the promise, setting either a value or an error to this promise's `Future`
///
/// - Parameter result: `FutureValue<T>` to resolve with
func resolve(_ result: FutureValue<T>) {
/// - Parameter result: `FutureResult<T>` to resolve with
func resolve(_ result: FutureResult<T>) {
future.setValue(result)
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import Dispatch

/// Value of a future, being either rejected with an error or fulfilled with a value.
/// Result of a future, being either rejected with an error or fulfilled with a value.
///
/// - fulfilled: A fulfilled future, with a value.
/// - rejected: A rejected future, with an error.
public enum FutureValue<T> {
/// - fulfilled: A fulfilled future result, with a value.
/// - rejected: A rejected future result, with an error.
public enum FutureResult<T> {

/// The value is fulfilled
case fulfilled(T)

/// The value is rejected
case rejected(Error)

/// Creates a new `FutureValue<T>`, capturing the return value, or throw error of a function.
/// Creates a new `FutureResult<T>`, capturing the return value, or throw error of a function.
///
/// - Parameter capturing: Function to invoke, resulting in an error or a value.
public init(_ capturing: () throws -> T) {
Expand All @@ -23,14 +23,14 @@ public enum FutureValue<T> {
}
}

/// Creates a new fulfilled `FutureValue<T>`.
/// Creates a new fulfilled `FutureResult<T>`.
///
/// - Parameter value: A fulfilled value.
public init(_ value: T) {
self = .fulfilled(value)
}

/// Creates a new rejected `FutureValue<T>`.
/// Creates a new rejected `FutureResult<T>`.
///
/// - Parameter error: A rejection error
public init(_ error: Error) {
Expand Down Expand Up @@ -85,7 +85,7 @@ public enum FutureValue<T> {
}
}

/// The description of this `FutureValue<T>`
/// The description of this `FutureResult<T>`
public var description: String {
switch self {
case .fulfilled(let value):
Expand All @@ -96,8 +96,8 @@ public enum FutureValue<T> {
}
}

public extension FutureValue where T == Void {
static var success: FutureValue {
public extension FutureResult where T == Void {
static var success: FutureResult {
return .fulfilled(())
}
}
8 changes: 4 additions & 4 deletions Sources/Futures/Promise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ public func promise<T>(on queue: DispatchQueue = .futures, _ body: @escaping ()
/// - Parameters:
/// - type: Type of the future value.
/// - queue: Dispatch queue to execute the callback on.
/// - body: A function with a completion function as its parameter, taking a `FutureValue<T>`, which will be
/// - body: A function with a completion function as its parameter, taking a `FutureResult<T>`, which will be
/// used to resolve the future returned by this method.
/// - value: `FutureValue<T>` to resolve the future with.
/// - value: `FutureResult<T>` to resolve the future with.
/// - Returns: A future that will receive the eventual value.
public func promise<T>(
_ type: T.Type,
on queue: DispatchQueue = .futures,
_ body: @escaping (@escaping (_ value: FutureValue<T>) -> Void) throws -> Void) -> Future<T> {
_ body: @escaping (@escaping (_ value: FutureResult<T>) -> Void) throws -> Void) -> Future<T> {

let promise = Promise<T>()

queue.async {
do {
let completion = { (value: FutureValue<T>) in
let completion = { (value: FutureResult<T>) in
promise.resolve(value)
}

Expand Down
8 changes: 4 additions & 4 deletions docs/Classes.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
<a class="nav-group-name-link" href="Enums.html">Enumerations</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a class="nav-group-task-link" href="Enums/FutureValue.html">FutureValue</a>
<a class="nav-group-task-link" href="Enums/FutureResult.html">FutureResult</a>
</li>
</ul>
</li>
Expand All @@ -81,10 +81,10 @@
<a class="nav-group-name-link" href="Functions.html">Functions</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a class="nav-group-task-link" href="Functions.html#/s:7Futures7promiseAA6FutureCyxGxm_So13DispatchQueueC2onyyAA0C5ValueOyxGcKctlF">promise(_:on:_:)</a>
<a class="nav-group-task-link" href="Functions.html#/s:7Futures7promise_2on_AA6FutureCyxGxm_So17OS_dispatch_queueCyyAA0D6ResultOyxGcKctlF">promise(_:on:_:)</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Functions.html#/s:7Futures7promiseAA6FutureCyxGSo13DispatchQueueC2on_xyKctlF">promise(on:_:)</a>
<a class="nav-group-task-link" href="Functions.html#/s:7Futures7promise2on_AA6FutureCyxGSo17OS_dispatch_queueC_xyKctlF">promise(on:_:)</a>
</li>
</ul>
</li>
Expand Down Expand Up @@ -252,7 +252,7 @@ <h4>Declaration</h4>
</article>
</div>
<section class="footer">
<p>&copy; 2018 <a class="link" href="" target="_blank" rel="external">Formbound</a>. All rights reserved. (Last updated: 2018-09-01)</p>
<p>&copy; 2018 <a class="link" href="" target="_blank" rel="external">Formbound</a>. All rights reserved. (Last updated: 2018-09-26)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.9.3</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</body>
Expand Down
Loading

0 comments on commit 50045d3

Please sign in to comment.