AsyncJavaHelpers

Entry points for calling the suspending and Flow-returning parts of the ADK from Java, and for bridging a Flow to Reactive Streams.

A suspend function compiles to a method with a trailing Continuation parameter, so Java passes the call as a lambda that forwards it. Kotlin default arguments are not visible to Java, so every parameter must be supplied:

Session session = AsyncJavaHelpers.await(c -> sessionService.createSession(key, null, c));
List<Event> events =
AsyncJavaHelpers.collect(runner.runAsync(user, id, null, message, null, null));

A Flow also converts to a Reactive Streams Publisher, the common currency of RxJava, Project Reactor, and java.util.concurrent.Flow; the final hop is one line in caller code:

Flowable<Event> rx   = Flowable.fromPublisher(AsyncJavaHelpers.asPublisher(flow)); // RxJava 3
Flux<Event> flux = Flux.from(AsyncJavaHelpers.asPublisher(flow)); // Reactor
Flow.Publisher<Event> jdk =
FlowAdapters.toFlowPublisher(AsyncJavaHelpers.asPublisher(flow)); // JDK

Kotlin callers should use the suspending APIs directly instead.

Functions

Link copied to clipboard
@CheckReturnValue
fun <T : Any> asFlow(publisher: Publisher<T>): ERROR CLASS: Symbol not found for Flow<T>

Adapts publisher to a Flow, for feeding an Rx or Reactor source into the ADK.

Link copied to clipboard
@CheckReturnValue
fun <T : Any> asPublisher(flow: ERROR CLASS: Symbol not found for Flow<T>): Publisher<T>

Exposes flow as a Publisher. Elements must be non-null; Reactive Streams forbids null. For a single value, use async and Mono.fromFuture(...) instead.

Link copied to clipboard
@CheckReturnValue
fun <T> async(scope: ERROR CLASS: Symbol not found for CoroutineScope, block: suspend () -> T): CompletableFuture<T>

Runs block on scope and returns a future. Cancelling the future cancels the coroutine, and cancelling scope cancels work already in flight.

Link copied to clipboard
@CheckReturnValue
fun <T> await(block: suspend () -> T): T

Runs block and blocks the calling thread until it completes.

Link copied to clipboard
@CheckReturnValue
fun <T> collect(flow: ERROR CLASS: Symbol not found for Flow<T>): List<T>

Collects every element of flow, blocking until it completes.

Link copied to clipboard
fun <T> forEach(flow: ERROR CLASS: Symbol not found for Flow<T>, action: Consumer<T>)

Passes each element of flow to action as it arrives, blocking until flow completes.