@@ -89,13 +89,17 @@ public class Server<
89
89
try await self . error ( . invalidRequestFormat( messageType: . complete) )
90
90
return
91
91
}
92
- try await self . onOperationComplete ( completeRequest. id )
92
+ try await self . onOperationComplete ( completeRequest)
93
93
case . unknown:
94
94
try await self . error ( . invalidType( ) )
95
95
}
96
96
}
97
97
}
98
98
99
+ deinit {
100
+ subscriptionTasks. values. forEach { $0. cancel ( ) }
101
+ }
102
+
99
103
/// Define a custom callback run during `connection_init` resolution that allows authorization using the `payload`.
100
104
/// Throw from this closure to indicate that authorization has failed.
101
105
/// - Parameter callback: The callback to assign
@@ -171,18 +175,15 @@ public class Server<
171
175
let stream = try await onSubscribe ( graphQLRequest)
172
176
for try await event in stream {
173
177
try Task . checkCancellation ( )
174
- do {
175
- try await self . sendNext ( event, id: id)
176
- } catch {
177
- try await self . sendError ( error, id: id)
178
- throw error
179
- }
178
+ try await self . sendNext ( event, id: id)
180
179
}
181
180
} catch {
182
181
try await sendError ( error, id: id)
182
+ subscriptionTasks. removeValue ( forKey: id)
183
183
throw error
184
184
}
185
185
try await self . sendComplete ( id: id)
186
+ subscriptionTasks. removeValue ( forKey: id)
186
187
}
187
188
} else {
188
189
do {
@@ -196,6 +197,20 @@ public class Server<
196
197
}
197
198
}
198
199
200
+ private func onOperationComplete( _ completeRequest: CompleteRequest ) async throws {
201
+ guard initialized else {
202
+ try await error ( . notInitialized( ) )
203
+ return
204
+ }
205
+
206
+ let id = completeRequest. id
207
+ if let task = subscriptionTasks [ id] {
208
+ task. cancel ( )
209
+ subscriptionTasks. removeValue ( forKey: id)
210
+ }
211
+ try await self . onOperationComplete ( id)
212
+ }
213
+
199
214
/// Send a `connection_ack` response through the messenger
200
215
private func sendConnectionAck( _ payload: [ String : Map ] ? = nil ) async throws {
201
216
guard let messenger = messenger else { return }
0 commit comments