Created
April 12, 2014 19:41
-
-
Save ezhulenev/10553038 to your computer and use it in GitHub Desktop.
scalaz-stream equivalent to Play Framework's Enumerator.fromCallback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent.atomic.AtomicInteger | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.Future | |
import scala.concurrent.future | |
import scala.util.{Failure, Success} | |
import scalaz.concurrent.Task | |
import scalaz.stream.Process.End | |
import scalaz.stream._ | |
import scalaz.{-\/, \/-} | |
val cnt = new AtomicInteger(0) | |
def serviceCall: Future[Option[String]] = future { | |
if (cnt.incrementAndGet() <= 10) Some(s"Task #${cnt.get()}") else None | |
} | |
val task: Task[String] = Task.async(cb => serviceCall onComplete { | |
case Success(Some(str)) => cb(\/-(str)) | |
case Success(None) => cb(-\/(End)) | |
case Failure(err) => cb(-\/(err)) | |
}) | |
Process.repeatEval(task).runLog.run.foreach(println) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment