Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@sideeffffect
Copy link
Member

@adamgfraser this will eventually be forward-ported to seriers/2.x, right?

@adamgfraser
Copy link
Contributor

@sideeffffect Yes I pull all changes to master into series/2.x.

@adamgfraser adamgfraser merged commit 4585185 into zio:master Feb 24, 2022
@sideeffffect sideeffffect deleted the cancellable-java-completionstage branch February 24, 2022 23:42
@sideeffffect
Copy link
Member Author

sideeffffect commented Feb 25, 2022

For the record, in Cats Effect, there has been a recent change typelevel/cats-effect#2838 that "switched the order" of async and flatMap but I wasn't able to do anything like this with ZIO's effectAsyncInterrupt.
Do you think this is something we should look into deeper?

@adamgfraser
Copy link
Contributor

We can handle that with uninterruptibleMask. Something like this:

  def fromCompletionStage[A](thunk: => CompletionStage[A]): Task[A] =
    Task.uninterruptibleMask { restore =>
      Task.effect(thunk).flatMap { cs =>
        Task.effectSuspendTotalWith { (p, _) =>
          val cf = cs.toCompletableFuture
          if (cf.isDone) {
            unwrapDone(p.fatal)(cf)
          } else {
            restore(Task.effectAsyncInterrupt[A] { cb =>
              val _ = cs.handle[Unit] { (v: A, t: Throwable) =>
                val io = Option(t).fold[Task[A]](Task.succeed(v)) { t =>
                  catchFromGet(p.fatal).lift(t).getOrElse(Task.die(t))
                }
                cb(io)
              }
              Left(UIO(cf.cancel(false)))
            }).onInterrupt(UIO(cf.cancel(false)))
          }
        }
      }
    }

Would be good to add a test for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants