more annotations to god of annotations

This commit is contained in:
2020-10-15 00:26:37 +06:00
parent f44174b5b3
commit f8a8808508
53 changed files with 227 additions and 10 deletions

View File

@@ -3,7 +3,9 @@ package dev.inmo.micro_utils.coroutines
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.launch
import kotlin.js.JsExport
@JsExport
fun <T> CoroutineScope.actor(
channelCapacity: Int = Channel.UNLIMITED,
block: suspend (T) -> Unit

View File

@@ -2,7 +2,9 @@ package dev.inmo.micro_utils.coroutines
import kotlinx.coroutines.channels.*
import kotlinx.coroutines.flow.*
import kotlin.js.JsExport
@JsExport
@Suppress("FunctionName")
fun <T> BroadcastFlow(
internalChannelSize: Int = Channel.BUFFERED
@@ -15,6 +17,7 @@ fun <T> BroadcastFlow(
)
}
@JsExport
class BroadcastFlow<T> internal constructor(
private val channel: BroadcastChannel<T>,
private val flow: Flow<T>

View File

@@ -4,7 +4,9 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.*
import kotlin.js.JsExport
@JsExport
class BroadcastStateFlow<T> internal constructor(
parentFlow: Flow<T>,
private val stateGetter: () -> T
@@ -13,6 +15,7 @@ class BroadcastStateFlow<T> internal constructor(
get() = stateGetter()
}
@JsExport
fun <T> BroadcastChannel<T>.asStateFlow(value: T, scope: CoroutineScope): StateFlow<T> = asFlow().let {
var state: T = value
it.onEach { state = it }.launchIn(scope)
@@ -21,13 +24,16 @@ fun <T> BroadcastChannel<T>.asStateFlow(value: T, scope: CoroutineScope): StateF
}
}
@JsExport
fun <T> BroadcastChannel<T?>.asStateFlow(scope: CoroutineScope): StateFlow<T?> = asStateFlow(null, scope)
@JsExport
fun <T> broadcastStateFlow(initial: T, scope: CoroutineScope, channelSize: Int = Channel.BUFFERED) = BroadcastChannel<T>(
channelSize
).let {
it to it.asStateFlow(initial, scope)
}
@JsExport
fun <T> broadcastStateFlow(scope: CoroutineScope, channelSize: Int = Channel.BUFFERED) = broadcastStateFlow<T?>(null, scope, channelSize)

View File

@@ -2,6 +2,8 @@ package dev.inmo.micro_utils.coroutines
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.Deferred
import kotlin.js.JsExport
@JsExport
val <T> T.asDeferred: Deferred<T>
get() = CompletableDeferred(this)

View File

@@ -1,10 +1,12 @@
package dev.inmo.micro_utils.coroutines
import kotlin.coroutines.*
import kotlin.js.JsExport
/**
* Call this method in case you need to do something in common thread (like reading of file in JVM)
*/
@JsExport
suspend fun <T> doOutsideOfCoroutine(block: () -> T): T = suspendCoroutine {
try {
it.resume(block())

View File

@@ -2,6 +2,7 @@ package dev.inmo.micro_utils.coroutines
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.supervisorScope
import kotlin.js.JsExport
typealias ExceptionHandler<T> = suspend (Throwable) -> T
@@ -11,6 +12,7 @@ typealias ExceptionHandler<T> = suspend (Throwable) -> T
* @param [onException] Will be called when happen exception inside of [block]. By default will throw exception - this
* exception will be available for catching
*/
@JsExport
suspend inline fun <T> safely(
noinline onException: ExceptionHandler<T> = { throw it },
noinline block: suspend CoroutineScope.() -> T