Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Add wrapper for ApiFuture? #463

Open
fancywriter opened this issue Oct 5, 2023 · 6 comments
Open

Add wrapper for ApiFuture? #463

fancywriter opened this issue Oct 5, 2023 · 6 comments

Comments

@fancywriter
Copy link

I don't have good understanding why google decided to add yet another future interface (seems it's the same listenable future under the hood) https://cloud.google.com/java/docs/reference/api-common/latest/com.google.api.core.ApiFutures
Would be nice to have transparent wrapper for this too...

@sideeffffect
Copy link
Collaborator

https://cloud.google.com/java/docs/reference/api-common/latest/com.google.api.core.ApiFuture

A Future that can have a listener added.

Similar to Guava's ListenableFuture, but redeclared so that Guava could be shaded.

@sideeffffect
Copy link
Collaborator

@fancywriter what are the Maven coordinates of the JAR that contains this interface?

@fancywriter
Copy link
Author

fancywriter commented Oct 20, 2023

@sideeffffect this one https://mvnrepository.com/artifact/com.google.api/api-common

This is my naive implementation

import com.google.api.core.{ApiFuture, ApiFutureCallback, ApiFutures}
import zio.{Task, ZIO}

object ZIOCompat {
  def fromApiFuture[V](future: => ApiFuture[V]): Task[V] =
    ZIO.executor.flatMap(executor =>
      ZIO.async[Any, Throwable, V] { cb =>
        ApiFutures.addCallback(
          future,
          new ApiFutureCallback[V] {
            def onFailure(t: Throwable): Unit = cb(ZIO.fail(t))
            def onSuccess(v: V): Unit = cb(ZIO.succeed(v))
          },
          executor.asJava
        )
      }
    )
}

extension[V] (future: => ApiFuture[V]) {
  def toZIO: Task[V] = ZIOCompat.fromApiFuture(future)
}

I may not covered all corner cases like cancellation, didn't dig too much there.

@sideeffffect
Copy link
Collaborator

@fancywriter would you consider opening a PR for this?

@fancywriter
Copy link
Author

@sideeffffect theoretically, I can. How should I organise the code, does it have to be separate module?

@sideeffffect
Copy link
Collaborator

sideeffffect commented Oct 30, 2024

Yes, please add a new sbt module for this.

Currently we have a module zio-interop-guava with Scala package zio.interop.guava.

I think we can name the new one zio-interop-guava-google and use the Scala package zio.interop.guava.google.

Thank you

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants