Skip to content

Commit

Permalink
Publicizes variadic / array ChannelPipeline.addHandler methods and …
Browse files Browse the repository at this point in the history
…moves them from NIOHTTP1 to NIO. (#79) (#79)

Motivation:

The `addHandler` method is useful for creating extensions to add handlers similar to what Swift NIO offers w/ HTTP. For example:

```swift
extension ChannelPipeline {
    func addPostgreSQLClientHandlers(first: Bool = false) -> EventLoopFuture<Void> {
        return addHandlers(PostgreSQLMessageEncoder(), PostgreSQLMessageDecoder(), first: first)
    }
}
```

Result:

Array and variadic accepting `ChannelPipeline.addHandler` methods are now available when importing the NIO module.
  • Loading branch information
tanner0101 authored and Lukasa committed Mar 3, 2018
1 parent da65c4f commit 5ff6d9b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
33 changes: 33 additions & 0 deletions Sources/NIO/ChannelPipeline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,39 @@ public final class ChannelPipeline: ChannelInvoker {
}
}

extension ChannelPipeline {
/// Adds the provided channel handlers to the pipeline in the order given, taking account
/// of the behaviour of `ChannelHandler.add(first:)`.
///
/// - parameters:
/// - handlers: The array of `ChannelHandler`s to be added.
/// - first: If `true`, the supplied `ChannelHandler`s will be added to the front of the pipeline.
/// If `false`, they will be added to the back.
///
/// - returns: A future that will be completed when all of the supplied `ChannelHandler`s were added.
public func addHandlers(_ handlers: [ChannelHandler], first: Bool) -> EventLoopFuture<Void> {
var handlers = handlers
if first {
handlers = handlers.reversed()
}

return EventLoopFuture<Void>.andAll(handlers.map { add(handler: $0) }, eventLoop: eventLoop)
}

/// Adds the provided channel handlers to the pipeline in the order given, taking account
/// of the behaviour of `ChannelHandler.add(first:)`.
///
/// - parameters:
/// - handlers: One or more `ChannelHandler`s to be added.
/// - first: If `true`, the supplied `ChannelHandler`s will be added to the front of the pipeline.
/// If `false`, they will be added to the back.
///
/// - returns: A future that will be completed when all of the supplied `ChannelHandler`s were added.
public func addHandlers(_ handlers: ChannelHandler..., first: Bool) -> EventLoopFuture<Void> {
return addHandlers(handlers, first: first)
}
}

/// Special `ChannelHandler` that forwards all events to the `Channel.Unsafe` implementation.
private final class HeadChannelHandler: _ChannelOutboundHandler {

Expand Down
11 changes: 0 additions & 11 deletions Sources/NIOHTTP1/HTTPDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,6 @@ public extension ChannelPipeline {
upgradeCompletionHandler: upgradeCompletionHandler)
return addHandlers(responseEncoder, requestDecoder, upgrader, first: first)
}

/// Adds the provided channel handlers to the pipeline in the order given, taking account
/// of the behaviour of `ChannelHandler.add(first:)`.
private func addHandlers(_ handlers: ChannelHandler..., first: Bool) -> EventLoopFuture<Void> {
var handlers = handlers
if first {
handlers = handlers.reversed()
}

return EventLoopFuture<Void>.andAll(handlers.map { add(handler: $0) }, eventLoop: eventLoop)
}
}

/// A `ChannelInboundHandler` used to decode HTTP requests. See the documentation
Expand Down

1 comment on commit 5ff6d9b

@normanmaurer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one... @tanner0101 thanks!

Please sign in to comment.