Initializes a new AdapterSocket with an underlying socket and conversion functions. This initializer sets up a AdapterSocket that wraps another Socket, providing conversion between the underlying socket's request/response types and the AdapterSocket's request/response types. - Parameters: - underl
(socket: T, receive: @escaping (T.Request) -> Request?, send: @escaping (Response) -> T.Response)
| 74 | /// - to: A closure that converts from this connection's response type to the underlying connection's response type. |
| 75 | /// This closure may throw an error if the conversion fails. |
| 76 | init<T: Socket>(socket: T, |
| 77 | receive: @escaping (T.Request) -> Request?, |
| 78 | send: @escaping (Response) -> T.Response) async throws { |
| 79 | (responseStream, responseContinuation) = AsyncStream.makeStream(of: Response.self) |
| 80 | underlyingSocket = socket |
| 81 | sendResponse = { [socket] (response: Response) async throws -> Void in |
| 82 | try await socket.send(value: send(response)) |
| 83 | } |
| 84 | |
| 85 | let sequence = try await socket.receive().compactMap { receive($0) } |
| 86 | task = Task {[weak self] in |
| 87 | for try await request in sequence { |
| 88 | if Task.isCancelled { |
| 89 | return |
| 90 | } |
| 91 | |
| 92 | guard let self else { |
| 93 | continue |
| 94 | } |
| 95 | |
| 96 | await self.inform(request: request) |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | deinit { |
| 102 | task?.cancel() |