Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Play 2.1, What to use instead of deprecated PushEnumerator

PushEnumerator becomes deprecated in Play framework 2.1-RC. The documentation tells me to use Concurrent.broadcast instead. However, I the data I'm pushing is dependent on the user, so I can't broadcast the same data to each user.

In other words, Concurrent.broadcast will give me one enumerator that connects to many iteratees whereas I need many enumerators connecting to many iteratees.

like image 409
Mark Avatar asked Jan 29 '13 17:01

Mark


1 Answers

Here's a simple example for using Concurrent.unicast[E]:

// assume the following exist:
def readValueAsync(source: MySource): Future[Any]
val source: MySource = ...

// this is where the meat is:
val valueEnumerator = Concurrent.unicast[Any] {
  (channel: Concurrent.Channel[Any]) =>
    readValueAsync(source) onComplete {
      case Success(x: Any) => channel.push(x)
      case Failure(t) => channel.end(t)
    }
}

// you can then collect it using an iteratee
// since my enumerator never really ends, I only take 10 elements here
val result: List[Any] = 
  valueEnumerator through Enumeratee.take(10) run Interatee.getChunks[Any]
like image 92
nadavwr Avatar answered Sep 20 '22 11:09

nadavwr