Skip to content

Commit

Permalink
Expose toEffect and fromEffect (#418)
Browse files Browse the repository at this point in the history
Expose `toEffect` and `fromEffect` as public API.

Use case: with `doobie.ConnectionIO` to use `zio` effects in transaction with queries.
```scala
for
    r1 <- selectSomethingForUpdate
    _ <- log.info(s"selected: $r1").toEffect[ConnectionIO]
    r2 <- updateSomething(r1)
yield r2
```

> I agree that and `fromEffect` seem generically useful. 
(c) @adamgfraser
  • Loading branch information
senia-psm authored Nov 8, 2021
1 parent 04d9a74 commit 66b2ad8
Showing 1 changed file with 3 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,22 @@ package object interop {
}
}

@inline private[zio] def fromEffect[F[_], A](fa: F[A])(implicit F: Dispatcher[F]): Task[A] =
@inline def fromEffect[F[_], A](fa: F[A])(implicit F: Dispatcher[F]): Task[A] =
ZIO
.effectTotal(F.unsafeToFutureCancelable(fa))
.flatMap { case (future, cancel) =>
ZIO.fromFuture(_ => future).onInterrupt(ZIO.fromFuture(_ => cancel()).orDie).interruptible
}
.uninterruptible

@inline private[zio] def toEffect[F[_], R, A](rio: RIO[R, A])(implicit R: Runtime[R], F: Async[F]): F[A] =
@inline def toEffect[F[_], R, A](rio: RIO[R, A])(implicit R: Runtime[R], F: Async[F]): F[A] =
F.uncancelable { poll =>
F.delay(R.unsafeRunToFuture(rio)).flatMap { future =>
poll(F.onCancel(F.fromFuture(F.pure[Future[A]](future)), F.fromFuture(F.delay(future.cancel())).void))
}
}

private[zio] implicit class ToEffectSyntax[R, A](private val rio: RIO[R, A]) extends AnyVal {
implicit class ToEffectSyntax[R, A](private val rio: RIO[R, A]) extends AnyVal {
@inline def toEffect[F[_]: Async](implicit R: Runtime[R]): F[A] = interop.toEffect(rio)
}
}

0 comments on commit 66b2ad8

Please sign in to comment.