+
Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let package = Package(
],
dependencies: [
/// Event-driven network application framework for high performance protocol servers & clients, non-blocking.
.package(url: "https://github.com/apple/swift-nio.git", from: "1.12.0"),
.package(url: "https://github.com/apple/swift-nio.git", from: "1.14.1"),
],
targets: [
.target(name: "Async", dependencies: ["NIO"]),
Expand Down
6 changes: 3 additions & 3 deletions Sources/Async/Collection+Future.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
extension Collection where Element: FutureType {
extension Collection {
/// Maps a collection of same-type `Future`s.
///
/// See `Future.map`
public func map<T>(to type: T.Type, on worker: Worker, _ callback: @escaping ([Element.Expectation]) throws -> T) -> Future<T> {
public func map<S, T>(to type: T.Type, on worker: Worker, _ callback: @escaping ([S]) throws -> T) -> Future<T> where Element == Future<S> {
return flatten(on: worker).map(to: T.self, callback)
}

/// Maps a collection of same-type `Future`s.
///
/// See `Future.flatMap`
public func flatMap<T>(to type: T.Type, on worker: Worker, _ callback: @escaping ([Element.Expectation]) throws -> Future<T>) -> Future<T> {
public func flatMap<S, T>(to type: T.Type, on worker: Worker, _ callback: @escaping ([S]) throws -> Future<T>) -> Future<T> where Element == Future<S> {
return flatten(on: worker).flatMap(to: T.self, callback)
}
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/Async/Future+DoCatch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ extension Future {
}
}

extension Collection where Element: FutureType {
extension Collection {
/// Adds a callback for handling this `[Future]`'s result when it becomes available.
///
/// futureStrings.do { strings in
Expand All @@ -58,7 +58,7 @@ extension Collection where Element: FutureType {
/// }
///
/// - warning: Don't forget to use `catch` to handle the error case.
public func `do`(on worker: Worker, _ callback: @escaping ([Element.Expectation]) -> ()) -> Future<[Element.Expectation]> {
public func `do`<T>(on worker: Worker, _ callback: @escaping ([T]) -> ()) -> Future<[T]> where Element == Future<T> {
return self.flatten(on: worker).do(callback)
}

Expand All @@ -72,7 +72,7 @@ extension Collection where Element: FutureType {
///
/// - note: Will *only* be executed if an error occurs. Successful results will not call this handler.
@discardableResult
public func `catch`(on worker: Worker,_ callback: @escaping (Error) -> ()) -> Future<[Element.Expectation]> {
public func `catch`<T>(on worker: Worker,_ callback: @escaping (Error) -> ()) -> Future<[T]> where Element == Future<T> {
return self.flatten(on: worker).catch(callback)
}

Expand All @@ -89,7 +89,7 @@ extension Collection where Element: FutureType {
///
/// - note: Will be executed on both success and failure, but will not receive any input.
@discardableResult
public func always(on worker: Worker,_ callback: @escaping () -> ()) -> Future<[Element.Expectation]> {
public func always<T>(on worker: Worker,_ callback: @escaping () -> ()) -> Future<[T]> where Element == Future<T> {
return self.flatten(on: worker).always(callback)
}
}
Expand Down
49 changes: 4 additions & 45 deletions Sources/Async/Future+Flatten.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,58 +49,17 @@ extension Collection where Element == LazyFuture<Void> {
}
}

extension Collection where Element: FutureType {
extension Collection {
/// Flattens an array of futures into a future with an array of results.
/// - note: the order of the results will match the order of the futures in the input array.
public func flatten(on worker: Worker) -> Future<[Element.Expectation]> {
let eventLoop = worker.eventLoop

// Avoid unnecessary work
guard count > 0 else {
return eventLoop.newSucceededFuture(result: [])
}

let resultPromise: EventLoopPromise<[Element.Expectation]> = eventLoop.newPromise()
var promiseFulfilled = false

let expectedCount = self.count
var fulfilledCount = 0
var results = Array<Element.Expectation?>(repeating: nil, count: expectedCount)
for (index, future) in self.enumerated() {
future.addAwaiter { result in
let work: () -> Void = {
guard !promiseFulfilled else { return }
switch result {
case .success(let result):
results[index] = result
fulfilledCount += 1

if fulfilledCount == expectedCount {
promiseFulfilled = true
// Forcibly unwrapping is okay here, because we know that each result has been filled.
resultPromise.succeed(result: results.map { $0! })
}
case .error(let error):
promiseFulfilled = true
resultPromise.fail(error: error)
}
}

if future.eventLoop === eventLoop {
work()
} else {
eventLoop.execute(work)
}
}
}
return resultPromise.futureResult
public func flatten<T>(on worker: Worker) -> Future<[T]> where Element == Future<T> {
return Future.whenAll(Array(self), eventLoop: worker.eventLoop)
}
}

extension Collection where Element == Future<Void> {
/// Flattens an array of void futures into a single one.
public func flatten(on worker: Worker) -> Future<Void> {
let flatten: Future<[Void]> = self.flatten(on: worker)
return flatten.map(to: Void.self) { _ in return }
return Future.andAll(Array(self), eventLoop: worker.eventLoop)
}
}
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载