Skip to content

Commit

Permalink
George review
Browse files Browse the repository at this point in the history
  • Loading branch information
FranzBusch committed Feb 19, 2024
1 parent e0a87fc commit 5d20621
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 61 deletions.
2 changes: 1 addition & 1 deletion Sources/NIOCore/EventLoop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ extension EventLoop {
/// - returns: An `EventLoopFuture` identical to the `EventLoopFuture` returned from `task`.
@inlinable
@preconcurrency
public func flatSubmit<T: Sendable>(_ task: @escaping @Sendable () -> EventLoopFuture<T>) -> EventLoopFuture<T> { // TODO: This should take a closure that returns fresh
public func flatSubmit<T: Sendable>(_ task: @escaping @Sendable () -> EventLoopFuture<T>) -> EventLoopFuture<T> {
self.submit(task).flatMap { $0 }
}

Expand Down
73 changes: 13 additions & 60 deletions Sources/NIOCore/EventLoopFuture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,17 +184,6 @@ public struct EventLoopPromise<Value> {
self._resolve(value: .success(value))
}

/// Deliver a successful result to the associated `EventLoopFuture<Value>` object.
///
/// - Note: The call to this method must happen on the same event loop as this promise was created from.
///
/// - parameters:
/// - eventLoopBoundValue: The successful result of the operation.
@inlinable
public func succeed(eventLoopBoundValue: Value) {
self._resolve(eventLoopBoundResult: .success(eventLoopBoundValue))
}

/// Deliver an error to the associated `EventLoopFuture<Value>` object.
///
/// - parameters:
Expand Down Expand Up @@ -247,27 +236,6 @@ public struct EventLoopPromise<Value> {
self._resolve(value: result)
}

/// Complete the promise with the passed in `Result<Value, Error>`.
///
/// This method is equivalent to invoking:
/// ```
/// switch result {
/// case .success(let value):
/// promise.succeed(value)
/// case .failure(let error):
/// promise.fail(error)
/// }
/// ```
///
/// - Note: The call to this method must happen on the same event loop as this promise was created from.
///
/// - parameters:
/// - result: The result which will be used to succeed or fail this promise.
@inlinable
public func completeWith(eventLoopBoundResult: Result<Value, Error>) {
self._resolve(eventLoopBoundResult: eventLoopBoundResult)
}

/// Fire the associated `EventLoopFuture` on the appropriate event loop.
///
/// This method provides the primary difference between the `EventLoopPromise` and most
Expand All @@ -287,23 +255,6 @@ public struct EventLoopPromise<Value> {
}
}

/// Fire the associated `EventLoopFuture` on the appropriate event loop.
///
/// This method provides the primary difference between the `EventLoopPromise` and most
/// other `Promise` implementations: specifically, all callbacks fire on the `EventLoop`
/// that was used to create the promise.
///
/// - Note: The call to this method must happen on the same event loop as this promise was created from.
///
/// - parameters:
/// - value: The value to fire the future with.
@inlinable
internal func _resolve(eventLoopBoundResult: Result<Value, Error>) {
self.futureResult.eventLoop.assertInEventLoop()

self._setValue(value: eventLoopBoundResult)._run()
}

/// Set the future result and get the associated callbacks.
///
/// - parameters:
Expand Down Expand Up @@ -1435,17 +1386,17 @@ extension EventLoopFuture {
processResult(index, .success(()))
case .failure(let error):
processResult(index, .failure(error))
}
if case .failure = result {
return // Once the promise is failed, future results do not need to be processed.
return
}
} else {
// We have to map to `Void` here to avoid sharing the potentially non-Sendable
// value across event loops.
future
.map { _ in () }
.hop(to: eventLoop)
.whenComplete { result in processResult(index, result) }
future.whenComplete { result in
let voidResult = result.map { _ in }
future.eventLoop.execute {
processResult(index, voidResult)
}
}
}
}
}
Expand Down Expand Up @@ -1661,10 +1612,12 @@ extension EventLoopFuture {
} else {
// We have to map to `Void` here to avoid sharing the potentially non-Sendable
// value across event loops.
future
.map { _ in () }
.hop(to: eventLoop)
.whenComplete { result in processResult(index, result) }
future.whenComplete { result in
let voidResult = result.map { _ in }
future.eventLoop.execute {
processResult(index, voidResult)
}
}
}
}
}
Expand Down

0 comments on commit 5d20621

Please sign in to comment.