Add compatibility util to collect flows from Java (#338)

* add compatibility util to consume flows from Java

* add api changes

* Make JavaFlow static and tweak the behavior a bit

Co-authored-by: Niels van Velzen <git@ndat.nl>
This commit is contained in:
GiviMAD 2021-12-11 18:51:49 +01:00 committed by GitHub
parent 4e49ac99b2
commit 18ca13abdf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 107 additions and 0 deletions

View File

@ -217,6 +217,32 @@ public final class org/jellyfin/sdk/discovery/RecommendedServerIssue$Unsupported
public fun toString ()Ljava/lang/String;
}
public final class org/jellyfin/sdk/discovery/compatibility/JavaFlow {
public static final field INSTANCE Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow;
public static final fun collect (Lkotlinx/coroutines/flow/Flow;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$ResultCallback;)Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$FlowJob;
public static final fun collect (Lkotlinx/coroutines/flow/Flow;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$StartCallback;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$ResultCallback;)Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$FlowJob;
public static final fun collect (Lkotlinx/coroutines/flow/Flow;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$StartCallback;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$ResultCallback;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$CompletionCallback;)Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$FlowJob;
public static final fun collect (Lkotlinx/coroutines/flow/Flow;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$StartCallback;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$ResultCallback;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$CompletionCallback;Lkotlinx/coroutines/CoroutineScope;)Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$FlowJob;
public static synthetic fun collect$default (Lkotlinx/coroutines/flow/Flow;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$StartCallback;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$ResultCallback;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$CompletionCallback;Lkotlinx/coroutines/CoroutineScope;ILjava/lang/Object;)Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$FlowJob;
}
public abstract interface class org/jellyfin/sdk/discovery/compatibility/JavaFlow$CompletionCallback {
public abstract fun onCompletion (Ljava/lang/Throwable;)V
}
public final class org/jellyfin/sdk/discovery/compatibility/JavaFlow$FlowJob : java/io/Closeable {
public fun <init> (Lkotlinx/coroutines/Job;)V
public fun close ()V
}
public abstract interface class org/jellyfin/sdk/discovery/compatibility/JavaFlow$ResultCallback {
public abstract fun onResult (Ljava/lang/Object;)V
}
public abstract interface class org/jellyfin/sdk/discovery/compatibility/JavaFlow$StartCallback {
public abstract fun onStart ()V
}
public abstract interface class org/jellyfin/sdk/util/ApiClientFactory {
public abstract fun create (Ljava/lang/String;Ljava/lang/String;Ljava/util/UUID;Lorg/jellyfin/sdk/model/ClientInfo;Lorg/jellyfin/sdk/model/DeviceInfo;Lorg/jellyfin/sdk/api/client/HttpClientOptions;)Lorg/jellyfin/sdk/api/client/ApiClient;
}

View File

@ -204,6 +204,32 @@ public final class org/jellyfin/sdk/discovery/RecommendedServerIssue$Unsupported
public fun toString ()Ljava/lang/String;
}
public final class org/jellyfin/sdk/discovery/compatibility/JavaFlow {
public static final field INSTANCE Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow;
public static final fun collect (Lkotlinx/coroutines/flow/Flow;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$ResultCallback;)Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$FlowJob;
public static final fun collect (Lkotlinx/coroutines/flow/Flow;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$StartCallback;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$ResultCallback;)Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$FlowJob;
public static final fun collect (Lkotlinx/coroutines/flow/Flow;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$StartCallback;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$ResultCallback;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$CompletionCallback;)Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$FlowJob;
public static final fun collect (Lkotlinx/coroutines/flow/Flow;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$StartCallback;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$ResultCallback;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$CompletionCallback;Lkotlinx/coroutines/CoroutineScope;)Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$FlowJob;
public static synthetic fun collect$default (Lkotlinx/coroutines/flow/Flow;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$StartCallback;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$ResultCallback;Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$CompletionCallback;Lkotlinx/coroutines/CoroutineScope;ILjava/lang/Object;)Lorg/jellyfin/sdk/discovery/compatibility/JavaFlow$FlowJob;
}
public abstract interface class org/jellyfin/sdk/discovery/compatibility/JavaFlow$CompletionCallback {
public abstract fun onCompletion (Ljava/lang/Throwable;)V
}
public final class org/jellyfin/sdk/discovery/compatibility/JavaFlow$FlowJob : java/io/Closeable {
public fun <init> (Lkotlinx/coroutines/Job;)V
public fun close ()V
}
public abstract interface class org/jellyfin/sdk/discovery/compatibility/JavaFlow$ResultCallback {
public abstract fun onResult (Ljava/lang/Object;)V
}
public abstract interface class org/jellyfin/sdk/discovery/compatibility/JavaFlow$StartCallback {
public abstract fun onStart ()V
}
public abstract interface class org/jellyfin/sdk/util/ApiClientFactory {
public abstract fun create (Ljava/lang/String;Ljava/lang/String;Ljava/util/UUID;Lorg/jellyfin/sdk/model/ClientInfo;Lorg/jellyfin/sdk/model/DeviceInfo;Lorg/jellyfin/sdk/api/client/HttpClientOptions;)Lorg/jellyfin/sdk/api/client/ApiClient;
}

View File

@ -0,0 +1,55 @@
package org.jellyfin.sdk.discovery.compatibility
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.launch
import java.io.Closeable
/**
* Compatibility class to use Kotlin Coroutine [Flow] in Java files.
*/
public object JavaFlow {
public fun interface ResultCallback<T> {
public fun onResult(result: T)
}
public fun interface StartCallback {
public fun onStart()
}
public fun interface CompletionCallback {
public fun onCompletion(error: Throwable?)
}
public class FlowJob(private val job: Job) : Closeable {
public override fun close(): Unit = job.cancel()
}
/**
* Collect all entries in a given [Flow].
*
* @param flow The [Flow] instance.
* @param startCallback Called when the flow starts emitting entries.
* @param resultCallback Called for each entry.
* @param completeCallback Called when the flow ends.
*/
@JvmOverloads
@JvmStatic
public fun <T> collect(
flow: Flow<T>,
startCallback: StartCallback? = null,
resultCallback: ResultCallback<T>,
completeCallback: CompletionCallback? = null,
coroutineScope: CoroutineScope = CoroutineScope(Dispatchers.Default),
): FlowJob = coroutineScope.launch {
flow
.onStart { startCallback?.onStart() }
.onCompletion { error -> completeCallback?.onCompletion(error) }
.collect { entry -> resultCallback.onResult(entry) }
}.let(::FlowJob)
}