59 lines
1.3 KiB
Scala
59 lines
1.3 KiB
Scala
|
package util
|
||
|
|
||
|
import scala.concurrent.Future
|
||
|
import cats.effect.IO
|
||
|
import cats.implicits._
|
||
|
import scala.util.Success
|
||
|
import scala.util.Failure
|
||
|
import cats.effect.Effect
|
||
|
import play.api.mvc._
|
||
|
import scala.concurrent.ExecutionContext
|
||
|
|
||
|
object IOHttp {
|
||
|
|
||
|
/**
|
||
|
* Actions to convert an effect wrapper F such as cats IO
|
||
|
* or monix Task into a future implicitly
|
||
|
*
|
||
|
* @param ab
|
||
|
*/
|
||
|
implicit class ActionBuilderOps[+R[_], B](ab: ActionBuilder[R, B]) {
|
||
|
|
||
|
import cats.effect.implicits._
|
||
|
|
||
|
/**
|
||
|
* Action to convert an effect wrapper F such as cats IO
|
||
|
* or monix Task into a future implicitly
|
||
|
*
|
||
|
* @param ab
|
||
|
*/
|
||
|
def asyncFR[F[_]: Effect](cb: R[B] => F[Result]): Action[B] = ab.async {
|
||
|
c => cb(c).toIO.unsafeToFuture()
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Action to convert an effect wrapper F such as cats IO
|
||
|
* or monix Task into a future implicitly
|
||
|
*
|
||
|
* @param ab
|
||
|
*/
|
||
|
def asyncF[F[_]: Effect](cb: => F[Result]): Action[AnyContent] =
|
||
|
ab.async { cb.toIO.unsafeToFuture() }
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
object RepoUtil {
|
||
|
def fromFuture[IOEffect[A], A](
|
||
|
f: => Future[A]
|
||
|
)(implicit ec: ExecutionContext): IO[A] =
|
||
|
IO.delay(f) >>= (f =>
|
||
|
IO.async[A] { cb =>
|
||
|
f.onComplete {
|
||
|
case Success(a) => cb(Right(a))
|
||
|
case Failure(ex) => cb(Left(ex))
|
||
|
}
|
||
|
}
|
||
|
)
|
||
|
}
|