play-iteratees
play-iteratees copied to clipboard
Improve Concurrent.unicast API
From https://github.com/playframework/playframework/issues/3619
At the moment Concurrent.unicast is really hard to use. It should often be used instead of Concurrent.broadcast, but I think it's just too hard to work out. I wanted to suggest updating the docs in #3336 to use Concurrent.unicast but I couldn't write the code succinctly.
Here's the current method signature:
def unicast[E](
onStart: Channel[E] => Unit,
onComplete: => Unit = (),
onError: (String, Input[E]) => Unit = (_: String, _: Input[E]) => ())(implicit ec: ExecutionContext) = new Enumerator[E] {
What I propose:
- Make a new
Concurrent.unicastmethod that creates anEnumeratorthat can only be used once. Only being usable once will simplify the code and make things more efficient.. - Change the signature to use
Futures instead of callbacks. We can do this because we're only using theEnumeratoronce.
/**
* @param channel The Channel to push inputs down.
* @param completion A future that will be completed when the attached
* Iteratee is Done or completes with an Error. Will be a failure if the
* Iterate had an error.
*/
final case class Unicast[E](channel: Channel[E], completion: Future[Unit])
def unicast[E]: (Enumerator, Future[Unicast])