File tree Expand file tree Collapse file tree 1 file changed +17
-0
lines changed
twitter-futures/src/main/scala/com/dwolla/util/async Expand file tree Collapse file tree 1 file changed +17
-0
lines changed Original file line number Diff line number Diff line change @@ -3,11 +3,14 @@ package com.dwolla.util.async
33import cats .*
44import cats .data .*
55import cats .effect .*
6+ import cats .effect .std .Dispatcher
7+ import cats .effect .syntax .all .*
68import cats .syntax .all .*
79import cats .tagless .*
810import cats .tagless .syntax .all .*
911import com .dwolla .util .async .twitter .CancelledViaCatsEffect
1012import com .twitter .util
13+ import com .twitter .util .{Future , Promise }
1114
1215import java .util .concurrent .CancellationException
1316import 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
3249class PartiallyAppliedProvide [F [_]](private val dummy : Boolean = true ) extends AnyVal {
You can’t perform that action at this time.
0 commit comments