Stream Future in Play 2.5

Viewed 270

Once again I am attempting to update some pre Play 2.5 code (based on this vid). For example the following used to be how to stream a Future:

  Ok.chunked(Enumerator.generateM(Promise.timeout(Some("hello"), 500)))

I have created the following method for the work-around for Promise.timeout (deprecated) using Akka:

  private def keepResponding(data: String, delay: FiniteDuration, interval: FiniteDuration): Future[Result] = {
    val promise: Promise[Result] = Promise[Result]()
    actorSystem.scheduler.schedule(delay, interval) { promise.success(Ok(data)) }
    promise.future
  }

According to the Play Framework Migration Guide; Enumerators should be rewritten to a Source and Source.unfoldAsync is apparently the equivalent of Enumerator.generateM so I was hoping that this would work (where str is a Future[String]):

  def inf = Action { request =>
    val str = keepResponding("stream me", 1.second, 2.second)

    Ok.chunked(Source.unfoldAsync(str))
  }

Of course I'm getting a Type mismatch error and when looking at the case class signature of unfoldAsync:

final class UnfoldAsync[S, E](s: S, f: S ⇒ Future[Option[(S, E)]])

I can see that the parameters are not correct but I'm not fully understanding what/how I should pass this through.

1 Answers
Related