Skip to content
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

Middleware refactor & remove Session shared state #120

Merged
merged 8 commits into from
Mar 10, 2016

Conversation

ketzusaka
Copy link
Contributor

@tannernelson This includes Application in the Middleware handle method, allowing the SessionMiddleware to operate on application-level data and configure the session there, instead of it being a shared class variable, as we discussed.

The spacing in the diff seems a bit off; It looks right in Xcode. Not sure what to do about that =\

This allows a middleware to have access to application-specific data
@@ -242,7 +250,7 @@ extension Application: ServerDriverDelegate {

// Loop through middlewares in order
for middleware in self.middleware {
handler = middleware.handle(handler)
handler = middleware.handle(forApplication: self, handler: handler)
Copy link
Member

Choose a reason for hiding this comment

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

Seems like the indentation got weird here, can we align it so it indents past for.

(Might just be Github)

@ketzusaka
Copy link
Contributor Author

@tannernelson @loganwright Updated!

@@ -16,6 +16,6 @@ public protocol Middleware {
Call `handler(request)` somewhere inside your custom
handler to get the `Response` object.
*/
static func handle(handler: Request.Handler) -> Request.Handler
static func handle(handler: Request.Handler, for application: Application) -> Request.Handler
Copy link
Member

Choose a reason for hiding this comment

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

Why for here and not application? Is this recommended by the Swift 3 evolution guidelines? (Not a criticism, genuinely curious)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, this is more in line with the latest swift API Design guidelines. The type is suppose to give more insight under them, so we're not expected to declare its an application.

Copy link
Member

Choose a reason for hiding this comment

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

Something about this seems off to me...idk why.

I'm wondering if something like this would be better

class Middleware {
  let app: Application
  init(app: Application) {
    self.app = app
  }

  func handle(handler: Request.Handler) -> Request.Handler {
    return handler
 }
}

Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I feel like it works well as it is.

The one thing that may be offputting here is that the handler is a function that isn't the last argument?

If we did want to push for an instance I'd like to keep it a protocol with an init requirement.

Copy link
Member

Choose a reason for hiding this comment

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

Keeping it static allows us to add and remove middleware while the application is running as well which could be useful.

And maybe you're right about the closure not being the last param...though we are only ever calling the middleware internally and the closure is usually be passed around as a variable. So it's not a huge deal.

tanner0101 added a commit that referenced this pull request Mar 10, 2016
Middleware refactor & remove Session shared state
@tanner0101 tanner0101 merged commit 832e92b into master Mar 10, 2016
@tanner0101 tanner0101 deleted the ketz/remove-shared-session-state branch March 10, 2016 02:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants