dave08
inline fun <reified T: Any> Kodein.Builder.singleton(noinline creator: suspend NoArgBindingKodein.() -> T) = SingletonCoroutineBinding(generic(), creator) class SingletonCoroutineBinding<T : Any>(override val createdType: TypeToken<T>, creator: suspend NoArgBindingKodein.() -> T) : ASingleton<T>(creator) { override fun factoryName() = "singleton" }
init
ASingleton
abstract class ACoroutineSingleton<T : Any> internal constructor(val creator: suspend NoArgBindingKodein.() -> T) : NoArgBinding<T> { private @Volatile var _instance: T? = null private val _lock = Any() override suspend fun getInstance(kodein: NoArgBindingKodein, key: Kodein.Key<Unit, T>): T { synchronizedIfNull(_lock, this::_instance, { return it }) { _instance = kodein.creator() } return _instance!! } }
A modern programming language that makes developers happier.