Skip to content

Instantly share code, notes, and snippets.

@djspiewak
Created September 18, 2019 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djspiewak/13c100a871db0c9c2006d851b5732930 to your computer and use it in GitHub Desktop.
Save djspiewak/13c100a871db0c9c2006d851b5732930 to your computer and use it in GitHub Desktop.
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