Skip to content

Commit

Permalink
require MonadError rather than Bracket
Browse files Browse the repository at this point in the history
  • Loading branch information
t3hnar committed Sep 19, 2019
1 parent 37737e0 commit 0b70423
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 24 deletions.
2 changes: 1 addition & 1 deletion project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import sbt._
object Dependencies {

val scalatest = "org.scalatest" %% "scalatest" % "3.0.8"
val `cats-helper` = "com.evolutiongaming" %% "cats-helper" % "1.0.0"
val `cats-helper` = "com.evolutiongaming" %% "cats-helper" % "1.0.1"
val random = "com.evolutiongaming" %% "random" % "0.0.5"

object Cats {
Expand Down
14 changes: 7 additions & 7 deletions src/main/scala/com/evolutiongaming/retry/Retry.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package com.evolutiongaming.retry
import java.time.Instant

import cats.arrow.FunctionK
import cats.effect.{Bracket, Clock, Timer}
import cats.effect.{Clock, Timer}
import cats.implicits._
import cats.~>
import cats.{MonadError, ~>}
import com.evolutiongaming.catshelper.ClockHelper._
import com.evolutiongaming.catshelper.EffectHelper._
import com.evolutiongaming.catshelper.CatsHelper._

import scala.concurrent.duration._

Expand All @@ -29,7 +29,7 @@ object Retry {
def apply[F[_] : Timer, E](
strategy: Strategy,
onError: OnError[F, E])(implicit
bracket: Bracket[F, E]
F: MonadError[F, E]
): Retry[F] = {

type S = (Status, Decide)
Expand Down Expand Up @@ -70,8 +70,8 @@ object Retry {
zero = (Status.empty(now), strategy.decide)
result <- zero.tailRecM[F, A] { case (status, decide) =>
fa.redeemWith[Either[S, A], E](
error => retry[A](status, decide, error),
result => result.asRight[(Status, Decide)].pure[F])
a => retry[A](status, decide, a),
a => a.asRight[(Status, Decide)].pure[F])
}
} yield result
}
Expand All @@ -81,7 +81,7 @@ object Retry {

def apply[F[_] : Timer, E](
strategy: Strategy)(implicit
bracket: Bracket[F, E]
F: MonadError[F, E]
): Retry[F] = {
apply(strategy, OnError.empty[F, E])
}
Expand Down
18 changes: 2 additions & 16 deletions src/test/scala/com/evolutiongaming/retry/RetrySpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.evolutiongaming.retry

import cats._
import cats.arrow.FunctionK
import cats.effect.{Bracket, Clock, ExitCase, Timer}
import cats.effect.{Clock, Timer}
import cats.implicits._
import com.evolutiongaming.catshelper.ClockHelper._
import com.evolutiongaming.random.Random
Expand Down Expand Up @@ -161,7 +161,7 @@ object RetrySpec {

object StateT {

implicit val BracketImpl: Bracket[StateT, Error] = new Bracket[StateT, Error] {
implicit val MonadErrorStateT: MonadError[StateT, Error] = new MonadError[StateT, Error] {

def flatMap[A, B](fa: StateT[A])(f: A => StateT[B]) = {
StateT[B] { s =>
Expand All @@ -185,20 +185,6 @@ object RetrySpec {
StateT { s => apply(s, a) }
}

def bracketCase[A, B](acquire: StateT[A])(use: A => StateT[B])(release: (A, ExitCase[Error]) => StateT[Error]) = {
StateT { s =>
val (s1, a) = acquire.run(s)
a match {
case Left(a) => (s1, a.asLeft[B])
case Right(a) =>
val (s2, b) = use(a).run(s1)
val exitCase = b.fold(ExitCase.error, _ => ExitCase.complete)
val(s3, _) = release(a, exitCase).run(s2)
(s3, b)
}
}
}

def raiseError[A](e: Error) = {
StateT { s => (s, e.asLeft) }
}
Expand Down

0 comments on commit 0b70423

Please sign in to comment.