Last active
June 29, 2022 18:43
-
-
Save ChristopherDavenport/8abe8cb6fb7bea3f3714f94874e77260 to your computer and use it in GitHub Desktop.
Soft Cancellation, moving cancellation into a Supervisor so you can cancel in flow, but not cancel what is happening.
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
package io.chrisdavenport.softcancel | |
import cats.effect._ | |
import cats.effect.std._ | |
import cats.syntax.all._ | |
object SoftCancel { | |
def makeSoftCancelable[F[_]: Concurrent, A](fa: F[A], supervisor: Supervisor[F]): F[A] = { | |
supervisor.supervise(fa) | |
.flatMap(_.joinWith(Concurrent[F].raiseError(new java.util.concurrent.CancellationException("Outcome was Canceled")))) | |
} | |
} | |
object syntax { | |
object all extends all | |
trait all extends supervisor | |
object supervisor extends supervisor | |
trait supervisor { | |
implicit class SupervisorSoftCancel[F[_]](private val s: Supervisor[F]){ | |
def makeSoftCancelable[A](fa: F[A])(implicit F: Concurrent[F]): F[A] = | |
SoftCancel.makeSoftCancelable(fa, s) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment