Skip to content

Commit

Permalink
Fix WriterT
Browse files Browse the repository at this point in the history
  • Loading branch information
Luka Jacobowitz committed Nov 19, 2018
1 parent 3325b42 commit d1a5a40
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions core/shared/src/main/scala/cats/effect/Sync.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import simulacrum._
import cats.data._
import cats.effect.concurrent.Ref
import cats.syntax.all._
import cats.instances.tuple._

/**
* A monad that can suspend the execution of side effects
Expand Down Expand Up @@ -247,19 +248,21 @@ object Sync {
def bracketCase[A, B](acquire: WriterT[F, L, A])
(use: A => WriterT[F, L, B])
(release: (A, ExitCase[Throwable]) => WriterT[F, L, Unit]): WriterT[F, L, B] =
WriterT.liftF(Ref.of[F, Option[L]](None)).flatMap { ref =>
acquire.flatMap { a =>
WriterT(
F.bracketCase[A, (L, B)](F.pure(a)) { a =>
use(a).run.flatTap { case (l, _) => ref.set(Some(l)) }
} {
case (a, ExitCase.Completed) =>
release(a, ExitCase.Completed).written.flatMap { l => ref.update(_.map(L.combine(_, l))) }
case (a, res) => release(a, res).value
}.flatMap { case (l, b) => ref.get.map(_.getOrElse(l)).tupleRight(b) }
)
WriterT(
Ref[F].of(L.empty).flatMap { ref =>
F.bracketCase(acquire.run) { la =>
WriterT(la.pure[F]).flatMap(use).run
} { case ((_, a), ec) =>
val r = release(a, ec).run
if (ec == ExitCase.Completed)
r.flatMap { case (l, _) => ref.set(l) }
else
r.void
}.flatMap { lb =>
ref.get.map(l => lb.leftMap(_ |+| l))
}
}
}
)

override def uncancelable[A](fa: WriterT[F, L, A]): WriterT[F, L, A] =
WriterT(F.uncancelable(fa.run))
Expand Down

0 comments on commit d1a5a40

Please sign in to comment.