Skip to content

Instantly share code, notes, and snippets.

@dnaumenko
Last active February 11, 2020 12:44
Show Gist options
  • Save dnaumenko/eb991ca864969fbea4a994b090ba05eb to your computer and use it in GitHub Desktop.
Save dnaumenko/eb991ca864969fbea4a994b090ba05eb to your computer and use it in GitHub Desktop.
object catz extends CatsInstance
abstract class CatsInstance {
import zio.interop.catz._
implicit def contextShift[Ctx]: ContextShift[Env[Ctx, ?]] = new EnvContextShift()
implicit def timer[Ctx]: EnvTimer[Ctx] = new EnvTimer
}
class EnvTimer extends Timer[Env[Ctx, ?]] {
import zio.interop.catz.implicits._
override def clock: Clock[Env[Ctx, ?]] = {
val timer = ioTimer[Throwable]
val clock = timer.clock
new Clock[Env[Ctx, ?]] {
override def monotonic(unit: TimeUnit): Env[Ctx, Long] = ofZIO(clock.monotonic(unit))
override def realTime(unit: TimeUnit): Env[Ctx, Long] = ofZIO(clock.realTime(unit))
}
}
override def sleep(timespan: FiniteDuration): Env[Ctx, Unit] = ofZIO(timer.sleep(timespan))
}
class EnvContextShift[Ctx](implicit contextShift: ContextShift[RIO[Context[Ctx], ?]]) extends ContextShift[Env[Ctx, ?]] {
override def shift: Env[Ctx, Unit] = ofZIO(contextShift.shift)
override def evalOn[A](ec: ExecutionContext)(fa: Env[Ctx, A]): Env[Ctx, A] = ofZIO(contextShift.evalOn(ec)(fa.run))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment