@@ -82,7 +82,7 @@ public struct RPCRouter: Sendable {
8282 }
8383
8484 @usableFromInline
85- private( set) var handlers : [ MethodDescriptor : RPCHandler ]
85+ private( set) var handlers : [ MethodDescriptor : ( handler : RPCHandler , interceptors : [ any ServerInterceptor ] ) ]
8686
8787 /// Creates a new router with no methods registered.
8888 public init ( ) {
@@ -126,12 +126,25 @@ public struct RPCRouter: Sendable {
126126 _ context: ServerContext
127127 ) async throws -> StreamingServerResponse < Output >
128128 ) {
129- self . handlers [ descriptor ] = RPCHandler (
129+ let handler = RPCHandler (
130130 method: descriptor,
131131 deserializer: deserializer,
132132 serializer: serializer,
133133 handler: handler
134134 )
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+ }
135148 }
136149
137150 /// Removes any handler registered for the specified method.
@@ -150,10 +163,9 @@ extension RPCRouter {
150163 RPCAsyncSequence < RPCRequestPart , any Error > ,
151164 RPCWriter < RPCResponsePart > . Closable
152165 > ,
153- context: ServerContext ,
154- interceptors: [ any ServerInterceptor ]
166+ context: ServerContext
155167 ) async {
156- if let handler = self . handlers [ stream. descriptor] {
168+ if let ( handler, interceptors ) = self . handlers [ stream. descriptor] {
157169 await handler. handle ( stream: stream, context: context, interceptors: interceptors)
158170 } else {
159171 // If this throws then the stream must be closed which we can't do anything about, so ignore
0 commit comments