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