@@ -39,89 +39,102 @@ public actor Client<InitPayload: Equatable & Codable> {
3939 /// Listen and react to the provided async sequence of server messages. This function will block until the stream is completed.
4040 /// - Parameter incoming: The server message sequence that the client should react to.
4141 public func listen< A: AsyncSequence & Sendable > ( to incoming: A ) async throws
42- where A. Element == String {
42+ where A. Element == Data {
4343 for try await message in incoming {
44- // Detect and ignore error responses.
45- if message. starts ( with: " 44 " ) {
46- // TODO: Determine what to do with returned error messages
44+ try await respond ( to: message)
45+ }
46+ }
47+
48+ /// Listen and react to the provided async sequence of server messages. This function will block until the stream is completed.
49+ /// - Parameter incoming: The server message sequence that the client should react to.
50+ @available ( * , deprecated, message: " Use `Data` sequence instead. " )
51+ public func listen< A: AsyncSequence & Sendable > ( to incoming: A ) async throws
52+ where A. Element == String {
53+ for try await stringMessage in incoming {
54+ guard let message = stringMessage. data ( using: . utf8) else {
55+ try await self . error ( . invalidEncoding( ) )
4756 return
4857 }
58+ try await respond ( to: message)
59+ }
60+ }
61+
62+ private func respond( to message: Data ) async throws {
63+ let response : Response
64+ do {
65+ response = try decoder. decode ( Response . self, from: message)
66+ } catch {
67+ try await self . error ( . noType( ) )
68+ return
69+ }
4970
50- guard let json = message. data ( using: . utf8) else {
51- try await error ( . invalidEncoding( ) )
71+ switch response. type {
72+ case . connectionAck:
73+ guard
74+ let connectionAckResponse = try ? decoder. decode (
75+ ConnectionAckResponse . self,
76+ from: message
77+ )
78+ else {
79+ try await error ( . invalidResponseFormat( messageType: . connectionAck) )
5280 return
5381 }
54-
55- let response : Response
56- do {
57- response = try decoder. decode ( Response . self, from: json)
58- } catch {
59- try await self . error ( . noType( ) )
82+ try await onConnectionAck ( connectionAckResponse, self )
83+ case . next:
84+ guard let nextResponse = try ? decoder. decode ( NextResponse . self, from: message) else {
85+ try await error ( . invalidResponseFormat( messageType: . next) )
6086 return
6187 }
62-
63- switch response. type {
64- case . connectionAck:
65- guard
66- let connectionAckResponse = try ? decoder. decode (
67- ConnectionAckResponse . self,
68- from: json
69- )
70- else {
71- try await error ( . invalidResponseFormat( messageType: . connectionAck) )
72- return
73- }
74- try await onConnectionAck ( connectionAckResponse, self )
75- case . next:
76- guard let nextResponse = try ? decoder. decode ( NextResponse . self, from: json) else {
77- try await error ( . invalidResponseFormat( messageType: . next) )
78- return
79- }
80- try await onNext ( nextResponse, self )
81- case . error:
82- guard let errorResponse = try ? decoder. decode ( ErrorResponse . self, from: json) else {
83- try await error ( . invalidResponseFormat( messageType: . error) )
84- return
85- }
86- try await onError ( errorResponse, self )
87- case . complete:
88- guard let completeResponse = try ? decoder. decode ( CompleteResponse . self, from: json)
89- else {
90- try await error ( . invalidResponseFormat( messageType: . complete) )
91- return
92- }
93- try await onComplete ( completeResponse, self )
94- default :
95- try await error ( . invalidType( ) )
88+ try await onNext ( nextResponse, self )
89+ case . error:
90+ guard let errorResponse = try ? decoder. decode ( ErrorResponse . self, from: message) else {
91+ try await error ( . invalidResponseFormat( messageType: . error) )
92+ return
93+ }
94+ try await onError ( errorResponse, self )
95+ case . complete:
96+ guard let completeResponse = try ? decoder. decode ( CompleteResponse . self, from: message)
97+ else {
98+ try await error ( . invalidResponseFormat( messageType: . complete) )
99+ return
96100 }
101+ try await onComplete ( completeResponse, self )
102+ default :
103+ try await error ( . invalidType( ) )
97104 }
98105 }
99106
100107 /// Send a `connection_init` request through the messenger
101108 public func sendConnectionInit( payload: InitPayload ) async throws {
102109 try await messenger. send (
103- ConnectionInitRequest (
104- payload: payload
105- ) . toJSON ( encoder)
110+ encoder. encode (
111+ ConnectionInitRequest (
112+ payload: payload
113+ )
114+ )
106115 )
107116 }
108117
109118 /// Send a `subscribe` request through the messenger
110119 public func sendStart( payload: GraphQLRequest , id: String ) async throws {
111120 try await messenger. send (
112- SubscribeRequest (
113- payload: payload,
114- id: id
115- ) . toJSON ( encoder)
121+ encoder. encode (
122+ SubscribeRequest (
123+ payload: payload,
124+ id: id
125+ )
126+ )
116127 )
117128 }
118129
119130 /// Send a `complete` request through the messenger
120131 public func sendStop( id: String ) async throws {
121132 try await messenger. send (
122- CompleteRequest (
123- id: id
124- ) . toJSON ( encoder)
133+ encoder. encode (
134+ CompleteRequest (
135+ id: id
136+ )
137+ )
125138 )
126139 }
127140
0 commit comments