noncom
noncom

Reputation: 4992

PlayFramework 2.0.x -> 2.1-RC migration

In my program on Play 2.0.4 I had this piece of code:

val channel = Enumerator.imperative[JsValue](onStart = self ! NotifyJoin(username))

and now it says that imperative is deprecated, and the API says that I should use unicast or broadcast instead. I tend to use unicast since in my code the channel was unicast. So I make like

val channel = Concurrent.unicast[JsValue](onStart = self ! NotifyJoin(username))

But it does not work.. looks like that unicast wants something else. I cannot figure it out - there is no more info in the API... does anyone know what to do here?

UPDATE:

Started a discussion in Play Framework user group. Turns out to be a pretty common problem among developers, who are knew to the paradigm. Will hope the documentation is going to be improved.

Upvotes: 5

Views: 604

Answers (2)

OutNSpace
OutNSpace

Reputation: 413

Example of using Unicast:

// here is an enumerator that returns a chunk to the channel
val outEnumerator = Concurrent.unicast[JsValue] { channel =>
    val data = Json.obj("data" -> 12345)
    channel.push(data)
}

An alternative to using the old Enumerator.imperative is to use the generateM:

val out = Enumerator.generateM[JsValue] {
    Promise.timeout( {
        Some(Json.obj("data" -> 12345))
    }, 100, TimeUnit.MILLISECONDS )
}

Here, we generate a repeating value using a timeout. This enumerator repeats forever, although generateM allows you to return None to indicate when done.

Upvotes: 0

James Ward
James Ward

Reputation: 29433

The API for Concurrent.unicast is:

unicast[E](onStart: (Channel[E]) ⇒ Unit, onComplete: ⇒ Unit, onError: (String, Input[E]) ⇒ Unit): Enumerator[E]

The API for Concurrent.broadcast is:

broadcast[E]: (Enumerator[E], Channel[E])

You can get to the API in your app at:

http://localhost:9000/@documentation/api/scala/index.html#play.api.libs.iteratee.Concurrent$

Upvotes: 3

Related Questions