Created
October 11, 2019 13:59
-
-
Save woupiestek/62f22dc8f84d1736fccf1485d62ff4af to your computer and use it in GitHub Desktop.
Cats effect IO query context for phantom dsl.
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 cats.effect.{ContextShift, IO, Timer} | |
import cats.effect.concurrent.Deferred | |
import com.outworkers.phantom.DefaultImports | |
import com.outworkers.phantom.Manager.scalaExecutor | |
import com.outworkers.phantom.builder.query.execution.{FutureMonad, PromiseInterface} | |
import com.outworkers.phantom.ops.QueryContext | |
import scala.concurrent.ExecutionContextExecutor | |
import scala.concurrent.duration._ | |
object phantom { | |
private implicit lazy val phantomShift: ContextShift[IO] = IO.contextShift(scalaExecutor) | |
private implicit lazy val timer: Timer[IO] = IO.timer(scalaExecutor) | |
implicit val ioFutureMonad: FutureMonad[IO] = new FutureMonad[IO] { | |
override def pure[A](source: A): IO[A] = IO.pure(source) | |
override def map[A, B](source: IO[A])(f: A => B)(implicit ctx: ExecutionContextExecutor): IO[B] = | |
source.map(f) | |
override def flatMap[A, B](source: IO[A])(fn: A => IO[B])(implicit ctx: ExecutionContextExecutor): IO[B] = | |
source.flatMap(fn) | |
} | |
type Promise[T] = Deferred[IO, T] | |
implicit val ioPromiseInterface: PromiseInterface[Promise, IO] = new PromiseInterface[Promise, IO] { | |
override def empty[T]: Promise[T] = Deferred.unsafe | |
override def apply[T](value: T): IO[T] = IO.pure(value) | |
override def become[T](source: Promise[T], value: IO[T]): Promise[T] = { | |
value.flatMap(source.complete).unsafeRunAsyncAndForget() | |
source | |
} | |
override def future[T](source: Promise[T]): IO[T] = source.get | |
override def failed[T](exception: Throwable): IO[T] = IO.raiseError(exception) | |
} | |
class IOQueryContext extends QueryContext[Promise, IO, FiniteDuration](10.seconds) { | |
override def blockAwait[T](f: IO[T], timeout: FiniteDuration): T = | |
f.timeout(timeout).unsafeRunSync() | |
} | |
object catsdsl extends IOQueryContext with DefaultImports | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
import phantom.catsdsl._
instead ofcom.outworkers.phantom.dsl._
and getcats.effect.IO
out of phantom's methods.