This commit is contained in:
shabinder 2021-06-21 21:40:46 +05:30
parent 8d5e9cdccc
commit 7da3147b69
2 changed files with 9 additions and 12 deletions

View File

@ -8,9 +8,9 @@ inline fun <reified X> SuspendableEvent<*, *>.getAs() = when (this) {
is SuspendableEvent.Failure -> error as? X
}
suspend inline fun <V : Any?> SuspendableEvent<V, *>.success(crossinline f: suspend (V) -> Unit) = fold(f, {})
suspend inline fun <V : Any?> SuspendableEvent<V, *>.success(noinline f: suspend (V) -> Unit) = fold(f, {})
suspend inline fun <E : Throwable> SuspendableEvent<*, E>.failure(crossinline f: suspend (E) -> Unit) = fold({}, f)
suspend inline fun <E : Throwable> SuspendableEvent<*, E>.failure(noinline f: suspend (E) -> Unit) = fold({}, f)
infix fun <V : Any?, E : Throwable> SuspendableEvent<V, E>.or(fallback: V) = when (this) {
is SuspendableEvent.Success -> this
@ -105,7 +105,7 @@ sealed class SuspendableEvent<out V : Any?, out E : Throwable>: ReadOnlyProperty
abstract operator fun component1(): V?
abstract operator fun component2(): E?
suspend inline fun <X> fold(crossinline success: suspend (V) -> X, crossinline failure: suspend (E) -> X): X {
suspend inline fun <X> fold(noinline success: suspend (V) -> X, noinline failure: suspend (E) -> X): X {
return when (this) {
is Success -> success(this.value)
is Failure -> failure(this.error)
@ -164,11 +164,9 @@ sealed class SuspendableEvent<out V : Any?, out E : Throwable>: ReadOnlyProperty
Failure(ex as E)
}
suspend inline operator fun <V : Any?> invoke(crossinline block: suspend () -> V): SuspendableEvent<V, Throwable> = try {
Success(block())
} catch (ex: Throwable) {
Failure(ex)
}
suspend inline operator fun <V : Any?> invoke(
crossinline block: suspend () -> V
): SuspendableEvent<V, Throwable> = of(block)
}
}

View File

@ -17,7 +17,6 @@
package com.shabinder.common.di.providers
import co.touchlab.kermit.Kermit
import com.shabinder.common.di.audioToMp3.AudioToMp3
import com.shabinder.common.di.youtubeMp3.Yt1sMp3
import com.shabinder.common.models.corsApi
import com.shabinder.common.models.event.coroutines.SuspendableEvent
@ -30,9 +29,9 @@ interface YoutubeMp3: Yt1sMp3 {
operator fun invoke(
client: HttpClient,
logger: Kermit
): AudioToMp3 {
return object : AudioToMp3 {
override val client: HttpClient = client
): YoutubeMp3 {
return object : YoutubeMp3 {
override val httpClient: HttpClient = client
override val logger: Kermit = logger
}
}