Skip to content

Instantly share code, notes, and snippets.

@manuelbernhardt
Created February 20, 2012 07:42
Show Gist options
  • Save manuelbernhardt/1868295 to your computer and use it in GitHub Desktop.
Save manuelbernhardt/1868295 to your computer and use it in GitHub Desktop.
Session composition for action composition
val newSession = additionalSessionParams.foldLeft[Session](request.session) { _ + _ }
val r: PlainResult = action(request).asInstanceOf[PlainResult]
// workaround since withSession calls aren't composable it seems
val innerSession = r.header.headers.get(SET_COOKIE).map(cookies => Session.decodeFromCookie(Cookies.decode(cookies).find(_.name == Session.COOKIE_NAME)))
if(innerSession.isDefined) {
// there really should be an API method for adding sessions
val combined = innerSession.get.data.foldLeft(newSession) { _ + _ }
r.withSession(combined)
} else {
r.withSession(newSession)
}
@myyk
Copy link

myyk commented Oct 5, 2012

Wow, this is awesome. I was just trying to figure out how to do this. If Play! is pushing Action composition they should include this kind of functionality so we can use composition for the way out, not just for the way in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment