Simplify receive implementation for transports (#78)
This commit is contained in:
parent
046fa3ee96
commit
29aa74d137
3 changed files with 7 additions and 32 deletions
|
|
@ -148,14 +148,7 @@ public actor HTTPClientTransport: Actor, Transport {
|
||||||
|
|
||||||
/// Receives data in an async sequence
|
/// Receives data in an async sequence
|
||||||
public func receive() -> AsyncThrowingStream<Data, Swift.Error> {
|
public func receive() -> AsyncThrowingStream<Data, Swift.Error> {
|
||||||
return AsyncThrowingStream { continuation in
|
return messageStream
|
||||||
Task {
|
|
||||||
for try await message in messageStream {
|
|
||||||
continuation.yield(message)
|
|
||||||
}
|
|
||||||
continuation.finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - SSE
|
// MARK: - SSE
|
||||||
|
|
|
||||||
|
|
@ -159,18 +159,7 @@ import struct Foundation.Data
|
||||||
}
|
}
|
||||||
|
|
||||||
public func receive() -> AsyncThrowingStream<Data, Swift.Error> {
|
public func receive() -> AsyncThrowingStream<Data, Swift.Error> {
|
||||||
return AsyncThrowingStream { continuation in
|
return messageStream
|
||||||
Task {
|
|
||||||
do {
|
|
||||||
for try await message in messageStream {
|
|
||||||
continuation.yield(message)
|
|
||||||
}
|
|
||||||
continuation.finish()
|
|
||||||
} catch {
|
|
||||||
continuation.finish(throwing: error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private func receiveLoop() async {
|
private func receiveLoop() async {
|
||||||
|
|
|
||||||
|
|
@ -29,8 +29,8 @@ import struct Foundation.Data
|
||||||
public nonisolated let logger: Logger
|
public nonisolated let logger: Logger
|
||||||
|
|
||||||
private var isConnected = false
|
private var isConnected = false
|
||||||
private let messageStream: AsyncStream<Data>
|
private let messageStream: AsyncThrowingStream<Data, Swift.Error>
|
||||||
private let messageContinuation: AsyncStream<Data>.Continuation
|
private let messageContinuation: AsyncThrowingStream<Data, Swift.Error>.Continuation
|
||||||
|
|
||||||
public init(
|
public init(
|
||||||
input: FileDescriptor = FileDescriptor.standardInput,
|
input: FileDescriptor = FileDescriptor.standardInput,
|
||||||
|
|
@ -46,8 +46,8 @@ import struct Foundation.Data
|
||||||
factory: { _ in SwiftLogNoOpLogHandler() })
|
factory: { _ in SwiftLogNoOpLogHandler() })
|
||||||
|
|
||||||
// Create message stream
|
// Create message stream
|
||||||
var continuation: AsyncStream<Data>.Continuation!
|
var continuation: AsyncThrowingStream<Data, Swift.Error>.Continuation!
|
||||||
self.messageStream = AsyncStream { continuation = $0 }
|
self.messageStream = AsyncThrowingStream { continuation = $0 }
|
||||||
self.messageContinuation = continuation
|
self.messageContinuation = continuation
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -177,14 +177,7 @@ import struct Foundation.Data
|
||||||
/// or batches containing multiple requests/notifications encoded as JSON arrays.
|
/// or batches containing multiple requests/notifications encoded as JSON arrays.
|
||||||
/// Each message is guaranteed to be a complete JSON object or array.
|
/// Each message is guaranteed to be a complete JSON object or array.
|
||||||
public func receive() -> AsyncThrowingStream<Data, Swift.Error> {
|
public func receive() -> AsyncThrowingStream<Data, Swift.Error> {
|
||||||
return AsyncThrowingStream { continuation in
|
return messageStream
|
||||||
Task {
|
|
||||||
for await message in messageStream {
|
|
||||||
continuation.yield(message)
|
|
||||||
}
|
|
||||||
continuation.finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue