@@ -82,7 +82,7 @@ public struct RPCRouter: Sendable {
82
82
}
83
83
84
84
@usableFromInline
85
- private( set) var handlers : [ MethodDescriptor : RPCHandler ]
85
+ private( set) var handlers : [ MethodDescriptor : ( handler : RPCHandler , interceptors : [ any ServerInterceptor ] ) ]
86
86
87
87
/// Creates a new router with no methods registered.
88
88
public init ( ) {
@@ -126,12 +126,25 @@ public struct RPCRouter: Sendable {
126
126
_ context: ServerContext
127
127
) async throws -> StreamingServerResponse < Output >
128
128
) {
129
- self . handlers [ descriptor ] = RPCHandler (
129
+ let handler = RPCHandler (
130
130
method: descriptor,
131
131
deserializer: deserializer,
132
132
serializer: serializer,
133
133
handler: handler
134
134
)
135
+ self . handlers [ descriptor] = ( handler, [ ] )
136
+ }
137
+
138
+ @inlinable
139
+ public mutating func registerInterceptors(
140
+ pipeline: [ ServerInterceptorOperation ]
141
+ ) {
142
+ for descriptor in self . handlers. keys {
143
+ let applicableOperations = pipeline. filter { $0. applies ( to: descriptor) }
144
+ if !applicableOperations. isEmpty {
145
+ self . handlers [ descriptor] ? . interceptors = applicableOperations. map { $0. interceptor }
146
+ }
147
+ }
135
148
}
136
149
137
150
/// Removes any handler registered for the specified method.
@@ -150,10 +163,9 @@ extension RPCRouter {
150
163
RPCAsyncSequence < RPCRequestPart , any Error > ,
151
164
RPCWriter < RPCResponsePart > . Closable
152
165
> ,
153
- context: ServerContext ,
154
- interceptors: [ any ServerInterceptor ]
166
+ context: ServerContext
155
167
) async {
156
- if let handler = self . handlers [ stream. descriptor] {
168
+ if let ( handler, interceptors ) = self . handlers [ stream. descriptor] {
157
169
await handler. handle ( stream: stream, context: context, interceptors: interceptors)
158
170
} else {
159
171
// If this throws then the stream must be closed which we can't do anything about, so ignore
0 commit comments