-
Notifications
You must be signed in to change notification settings - Fork 643
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a way to get a handler from a pipeline #974
Conversation
Can one of the admins verify this patch? |
4 similar comments
Can one of the admins verify this patch? |
Can one of the admins verify this patch? |
Can one of the admins verify this patch? |
Can one of the admins verify this patch? |
Sources/NIO/ChannelPipeline.swift
Outdated
/// | ||
/// - parameters: | ||
/// - handlerType: the type of `ChannelHandler` to returns | ||
public func handler<T: ChannelHandler>(type _: T.Type) -> EventLoopFuture<T> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we have a version of this for each of our context
methods? I've always thought the context
methods were pretty weird, and ideally I'd get rid of them and replace them with these versions. @normanmaurer is there a good reason not to do that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Lukasa I think the context
ones are there so you can search and later on remove without walking the pipeline. But I agree they're a bit weird, not sure if we should kill them though.
XCTAssertTrue(result2 === handler2) | ||
|
||
let result3 = try channel.pipeline.handler(type: SimpleTypedHandler3.self).wait() | ||
XCTAssertTrue(result3 === handler3) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice! We could do some more tests here, like
- two handlers of the same type in the pipeline, verify that we get the first of those
- no handler of the given type there, verify that we get
nil
Sources/NIO/ChannelPipeline.swift
Outdated
@@ -1555,6 +1555,20 @@ extension ChannelPipeline: CustomDebugStringConvertible { | |||
return desc.joined(separator: "\n") | |||
} | |||
|
|||
/// Returns the `ChannelHandlerContext` that belongs to a `ChannelHandler` of the given type. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that documentation isn't quite right. It returns the first ChannelHandler
of a given type.
Sources/NIO/ChannelPipeline.swift
Outdated
/// | ||
/// - parameters: | ||
/// - handlerType: the type of `ChannelHandler` to returns | ||
public func handler<T: ChannelHandler>(type _: T.Type) -> EventLoopFuture<T> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should name the generic parameter Handler
instead of just T
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, that looks great already. A few suggestions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, just two nits left
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! Looks good to me
@swift-nio-bot test this please |
Sources/NIO/ChannelPipeline.swift
Outdated
/// - parameters: | ||
/// - handlerType: the type of `ChannelHandler` to return. | ||
public func handler<Handler: ChannelHandler>(type _: Handler.Type) -> EventLoopFuture<Handler> { | ||
return context(handlerType: Handler.self).map { context in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: can we use the explicit self
keyword here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Lukasa done!
@swift-nio-bot test this please |
Motivation:
#966
Modifications:
handler(type:)
method on ChannelPipelineResult:
Provides a nicer way to get a typed handler directly from the pipeline
Closes #966