17
17
#if compiler(>=5.5)
18
18
19
19
/// Writer for server-streaming RPC handlers to provide responses.
20
- ///
21
- /// NOTE: This will be replaced by a pausible writer that is currently being worked on in parallel.
22
20
@available ( macOS 12 , iOS 15 , tvOS 15 , watchOS 8 , * )
23
21
public struct GRPCAsyncResponseStreamWriter < Response> {
22
+ @usableFromInline
23
+ internal typealias Delegate = AsyncResponseStreamWriterDelegate < Response >
24
+
25
+ @usableFromInline
26
+ internal let _asyncWriter : AsyncWriter < Delegate >
27
+
28
+ @inlinable
29
+ internal init ( wrapping asyncWriter: AsyncWriter < Delegate > ) {
30
+ self . _asyncWriter = asyncWriter
31
+ }
32
+
33
+ @inlinable
34
+ public func send(
35
+ _ response: Response ,
36
+ compression: Compression = . deferToCallDefault
37
+ ) async throws {
38
+ try await _asyncWriter. write ( ( response, compression) )
39
+ }
40
+ }
41
+
42
+ @available ( macOS 12 , iOS 15 , tvOS 15 , watchOS 8 , * )
43
+ @usableFromInline
44
+ internal final class AsyncResponseStreamWriterDelegate < Response> : AsyncWriterDelegate {
24
45
@usableFromInline
25
46
internal let _context : GRPCAsyncServerCallContext
26
47
27
48
@usableFromInline
28
- internal let _send : ( Response , MessageMetadata ) async throws -> Void
49
+ internal let _send : ( Response , MessageMetadata ) -> Void
29
50
30
51
@usableFromInline
31
52
internal let _compressionEnabledOnServer : Bool
32
53
33
- // Create a new AsyncResponseStreamWriter .
54
+ // Create a new AsyncResponseStreamWriterDelegate .
34
55
//
35
56
// - Important: the `send` closure must be thread-safe.
36
57
@inlinable
37
58
internal init (
38
59
context: GRPCAsyncServerCallContext ,
39
60
compressionIsEnabled: Bool ,
40
- send: @escaping ( Response , MessageMetadata ) async throws -> Void
61
+ send: @escaping ( Response , MessageMetadata ) -> Void
41
62
) {
42
63
self . _context = context
43
64
self . _compressionEnabledOnServer = compressionIsEnabled
@@ -53,12 +74,25 @@ public struct GRPCAsyncResponseStreamWriter<Response> {
53
74
}
54
75
55
76
@inlinable
56
- public func send(
77
+ internal func send(
57
78
_ response: Response ,
58
79
compression: Compression = . deferToCallDefault
59
- ) async throws {
80
+ ) {
60
81
let compress = self . shouldCompress ( compression)
61
- try await self . _send ( response, . init( compress: compress, flush: true ) )
82
+ self . _send ( response, . init( compress: compress, flush: true ) )
83
+ }
84
+
85
+ // MARK: - AsyncWriterDelegate conformance.
86
+
87
+ @inlinable
88
+ internal func write( _ response: ( Response , Compression ) ) {
89
+ self . send ( response. 0 , compression: response. 1 )
90
+ }
91
+
92
+ @inlinable
93
+ internal func writeEnd( _ end: Void ) {
94
+ // meh.
95
+ // TODO: is this where will move the state on to completed somehow?
62
96
}
63
97
}
64
98
0 commit comments