|
4 | 4 |
|
5 | 5 | #if canImport(Darwin) |
6 | 6 | public import Foundation |
7 | | -import Network |
| 7 | +public import Network |
8 | 8 |
|
9 | 9 | #if canImport(OpenGraphCxx_Private) |
10 | 10 | public import OpenGraphCxx_Private.DebugServer |
11 | 11 | #endif |
12 | 12 |
|
| 13 | +public struct ConnectionUpdates: AsyncSequence { |
| 14 | + public typealias Element = NWConnection.State |
| 15 | + |
| 16 | + private let stream: AsyncStream<NWConnection.State> |
| 17 | + |
| 18 | + fileprivate init(stream: AsyncStream<NWConnection.State>) { |
| 19 | + self.stream = stream |
| 20 | + } |
| 21 | + |
| 22 | + public func makeAsyncIterator() -> AsyncStream<NWConnection.State>.AsyncIterator { |
| 23 | + stream.makeAsyncIterator() |
| 24 | + } |
| 25 | +} |
| 26 | + |
13 | 27 | @_spi(Debug) |
14 | 28 | public final class DebugClient { |
15 | 29 | private var connection: NWConnection? |
16 | | - private let queue = DispatchQueue(label: "opengraph.debugserver.client.queue") |
| 30 | + private let queue = DispatchQueue(label: "org.openswiftuiproject.opengraph.debugclient") |
17 | 31 |
|
18 | | - public func connect(to url: URL) async throws { |
| 32 | + public func connect(to url: URL) -> ConnectionUpdates { |
19 | 33 | guard let host = url.host, let port = url.port else { |
20 | | - throw ClientError.invalidURL |
| 34 | + return ConnectionUpdates(stream: AsyncStream { continuation in |
| 35 | + continuation.yield(.failed(NWError.posix(.EINVAL))) |
| 36 | + continuation.finish() |
| 37 | + }) |
21 | 38 | } |
22 | | - |
23 | 39 | let nwHost = NWEndpoint.Host(host) |
24 | 40 | let nwPort = NWEndpoint.Port(integerLiteral: UInt16(port)) |
25 | | - |
26 | 41 | connection = NWConnection(host: nwHost, port: nwPort, using: .tcp) |
27 | | - |
28 | | - return try await withCheckedThrowingContinuation { continuation in |
| 42 | + let stream = AsyncStream<NWConnection.State> { continuation in |
29 | 43 | connection?.stateUpdateHandler = { state in |
30 | | - switch state { |
31 | | - case .ready: |
32 | | - continuation.resume() |
33 | | - case let .failed(error): |
34 | | - continuation.resume(throwing: error) |
35 | | - case .cancelled: |
36 | | - continuation.resume(throwing: ClientError.connectionCancelled) |
37 | | - default: |
38 | | - break |
| 44 | + continuation.yield(state) |
| 45 | + if case .cancelled = state { |
| 46 | + continuation.finish() |
39 | 47 | } |
40 | 48 | } |
41 | 49 | connection?.start(queue: queue) |
42 | 50 | } |
| 51 | + return ConnectionUpdates(stream: stream) |
43 | 52 | } |
44 | 53 |
|
45 | 54 | public func sendMessage(token: UInt32, data: Data) async throws { |
|
0 commit comments