@@ -18,6 +18,8 @@ public class Server<InitPayload: Equatable & Codable> {
18
18
19
19
var auth : ( InitPayload ) throws -> Void = { _ in }
20
20
var onExit : ( ) -> Void = { }
21
+ var onOperationComplete : ( String ) -> Void = { _ in }
22
+ var onOperationError : ( String ) -> Void = { _ in }
21
23
var onMessage : ( String ) -> Void = { _ in }
22
24
23
25
var initialized = false
@@ -64,6 +66,7 @@ public class Server<InitPayload: Equatable & Codable> {
64
66
return
65
67
}
66
68
69
+ // handle incoming message
67
70
switch request. type {
68
71
case . connectionInit:
69
72
guard let connectionInitRequest = try ? self . decoder. decode ( ConnectionInitRequest< InitPayload> . self , from: data) else {
@@ -82,15 +85,16 @@ public class Server<InitPayload: Equatable & Codable> {
82
85
self . error ( . invalidRequestFormat( messageType: . complete) )
83
86
return
84
87
}
85
- self . onComplete ( completeRequest)
88
+ self . onOperationComplete ( completeRequest. id )
86
89
case . unknown:
87
90
self . error ( . invalidType( ) )
88
91
}
89
92
}
90
93
}
91
94
92
95
/// Define the callback run during `connection_init` resolution that allows authorization using the `payload`.
93
- /// Throw to indicate that authorization has failed. /// - Parameter callback: The callback to assign
96
+ /// Throw to indicate that authorization has failed.
97
+ /// - Parameter callback: The callback to assign
94
98
public func auth( _ callback: @escaping ( InitPayload ) throws -> Void ) {
95
99
self . auth = callback
96
100
}
@@ -107,6 +111,18 @@ public class Server<InitPayload: Equatable & Codable> {
107
111
self . onMessage = callback
108
112
}
109
113
114
+ /// Define the callback run on the completion a full operation (query/mutation, end of subscription)
115
+ /// - Parameter callback: The callback to assign, taking a string parameter for the ID of the operation
116
+ public func onOperationComplete( _ callback: @escaping ( String ) -> Void ) {
117
+ self . onOperationComplete = callback
118
+ }
119
+
120
+ /// Define the callback to run on error of any full operation (failed query, interrupted subscription)
121
+ /// - Parameter callback: The callback to assign, taking a string parameter for the ID of the operation
122
+ public func onOperationError( _ callback: @escaping ( String ) -> Void ) {
123
+ self . onOperationError = callback
124
+ }
125
+
110
126
private func onConnectionInit( _ connectionInitRequest: ConnectionInitRequest < InitPayload > ) {
111
127
guard !initialized else {
112
128
self . error ( . tooManyInitializations( ) )
@@ -193,13 +209,6 @@ public class Server<InitPayload: Equatable & Codable> {
193
209
}
194
210
}
195
211
196
- private func onComplete( _: CompleteRequest ) {
197
- guard initialized else {
198
- self . error ( . notInitialized( ) )
199
- return
200
- }
201
- }
202
-
203
212
/// Send a `connection_ack` response through the messenger
204
213
private func sendConnectionAck( _ payload: [ String : Map ] ? = nil ) {
205
214
guard let messenger = messenger else { return }
@@ -227,6 +236,7 @@ public class Server<InitPayload: Equatable & Codable> {
227
236
id: id
228
237
) . toJSON ( encoder)
229
238
)
239
+ self . onOperationComplete ( id)
230
240
}
231
241
232
242
/// Send an `error` response through the messenger
@@ -238,6 +248,7 @@ public class Server<InitPayload: Equatable & Codable> {
238
248
id: id
239
249
) . toJSON ( encoder)
240
250
)
251
+ self . onOperationError ( id)
241
252
}
242
253
243
254
/// Send an `error` response through the messenger
0 commit comments