All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.korlibs.inject.AsyncInjectorSuspendContext.kt Maven / Gradle / Ivy

There is a newer version: 4.0.10
Show newest version
package korlibs.inject

import kotlinx.coroutines.*
import kotlin.coroutines.*

suspend fun  withInjector(injector: AsyncInjector, block: suspend () -> T): T =
    withContext(AsyncInjectorContext(injector)) {
        block()
    }

suspend fun injector(): AsyncInjector =
    coroutineContext[AsyncInjectorContext]?.injector
        ?: error("AsyncInjector not in the context, please call withInjector function")

class AsyncInjectorContext(val injector: AsyncInjector) : CoroutineContext.Element {
    companion object : CoroutineContext.Key

    override val key get() = AsyncInjectorContext
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy