Last week I finally decided to address the fact that we constantly need to grab the current LiftSession and re-initialize it when mapping over futures produced by other libraries such as akka and dispatch. Shortly after I began that effort, I remembered that as of 3.0 Lift only supports 2.11.x and up. I figure the FutureWithSession wrapper I'm working on could just as well go into Lift itself to help others have smooth integration with scala's built in Future which has been in the language since 2.10.
The idea is pretty simple. I made an implicit conversion that allows you to ask for `withCurrentSession` or `withImplicitSession` on a Future. This returns a future which will wrap all of your maps, flatMaps, etc in a S.initIfUninitted with your session so you no longer have to manage all that yourself.
The test code below shows an example of how the functions passed to `map` now can access session variables.
object TestVar1 extends SessionVar[String]("test1")
object TestVar2 extends SessionVar[String]("test2")
"A FutureWithSession" should {
"have access to session variables in map() chains" in {
val session = new LiftSession("Test Session", "", Empty)
val future:Future[String] = S.initIfUninitted(session) {
TestVar1("map1")
TestVar2("map2")
Future("something").withCurrentSession
}
val actual:Future[String] = future
.map(_+"-"+TestVar1.get)
.map(_+"-"+TestVar2.get)
val expected:Option[Try[String]] = Some(Success("something-map1-map2"))
actual.value must eventually(beEqualTo(expected))
}
}
What does everyone think about having this in Lift? I have about half of it done in
this PR. I'll finish it out if everyone likes the idea.
Joe