Skip to content

Commit 888f1f1

Browse files
committed
wip add UnsafeFToFuture
1 parent 20a0ce5 commit 888f1f1

File tree

1 file changed

+17
-0
lines changed
  • twitter-futures/src/main/scala/com/dwolla/util/async

1 file changed

+17
-0
lines changed

twitter-futures/src/main/scala/com/dwolla/util/async/twitter.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ package com.dwolla.util.async
33
import cats.*
44
import cats.data.*
55
import cats.effect.*
6+
import cats.effect.std.Dispatcher
7+
import cats.effect.syntax.all.*
68
import cats.syntax.all.*
79
import cats.tagless.*
810
import cats.tagless.syntax.all.*
911
import com.dwolla.util.async.twitter.CancelledViaCatsEffect
1012
import com.twitter.util
13+
import com.twitter.util.{Future, Promise}
1114

1215
import java.util.concurrent.CancellationException
1316
import scala.util.control.NoStackTrace
@@ -27,6 +30,20 @@ object twitter extends ToAsyncFunctorKOps {
2730
private[async] case object CancelledViaCatsEffect
2831
extends CancellationException("Cancelled via cats-effect")
2932
with NoStackTrace
33+
34+
class UnsafeFToFuture[F[_] : Sync](dispatcher: Dispatcher[F]) extends (F ~> Future) {
35+
override def apply[A](fa: F[A]): Future[A] = {
36+
val p = Promise[A]()
37+
dispatcher.unsafeRunAndForget {
38+
fa.attempt.flatMap {
39+
case Left(ex) => Sync[F].delay(p.setException(ex))
40+
case Right(a) => Sync[F].delay(p.setValue(a))
41+
}
42+
.onCancel(Sync[F].delay(p.raise(CancelledViaCatsEffect)))
43+
}
44+
p
45+
}
46+
}
3047
}
3148

3249
class PartiallyAppliedProvide[F[_]](private val dummy: Boolean = true) extends AnyVal {

0 commit comments

Comments
 (0)