Created
September 18, 2019 21:22
-
-
Save djspiewak/13c100a871db0c9c2006d851b5732930 to your computer and use it in GitHub Desktop.
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
def fromCompletableFuture[F[_]: Concurrent: ContextShift, A]( | |
fa: F[CompletableFuture[A]]) | |
: F[A] = { | |
val wrapped = Concurrent cancelableF[F, A] { k => | |
fa flatMap { cf => | |
val register = Sync[F] delay { | |
cf handleAsync { (a, e) => | |
if (a == null) | |
k(e) | |
else | |
k(a) | |
} | |
} | |
register >> Sync[F].delay(cf.cancel(false)) | |
} | |
} | |
wrapped.guarantee(ContextShift[F].shift) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment