gildor
06/07/2019, 10:31 AMMark
06/07/2019, 10:35 AMgildor
06/07/2019, 10:35 AMMark
06/07/2019, 10:36 AMwbertan
06/07/2019, 10:38 AMMark
06/07/2019, 10:39 AMwbertan
06/07/2019, 10:44 AMNever use this anti-pattern
, but I believe this could achieve what you want (I think the Asas2
is what you want)
class TestAsas {
object Asas : (String) -> String {
override fun invoke(p1: String): String = "Welcome $p1!"
}
@Test
fun asas() {
val result = Asas("Me")
Assert.assertEquals("Welcome Me!", result)
}
object Asas2 : (String) -> () -> String {
lateinit var INSTANCE: () -> String
override fun invoke(p1: String): () -> String {
if(!::INSTANCE.isInitialized){
{ "Welcome $p1!" }.also { INSTANCE = it }
}
return INSTANCE
}
}
@Test
fun asas2() {
Asas2("Me")
val result1 = Asas2.INSTANCE()
Assert.assertEquals("Welcome Me!", result1)
val result2 = Asas2.INSTANCE()
Assert.assertEquals("Welcome Me!", result2)
}
}
@Test
fun asas3() {
Asas2("Me")
val result1 = Asas2.INSTANCE()
Assert.assertEquals("Welcome Me!", result1)
Asas2("Other")
val result2 = Asas2.INSTANCE()
Assert.assertEquals("Welcome Me!", result2)
}
louiscad
06/07/2019, 10:49 AMappCtx
property.Mark
06/07/2019, 10:50 AMgildor
06/07/2019, 10:53 AMI need something other than the context.Yes, most probably you need DI
Dico
06/07/2019, 10:56 AMgildor
06/07/2019, 10:56 AMDico
06/07/2019, 10:57 AMgildor
06/07/2019, 10:57 AMMark
06/07/2019, 10:58 AMgildor
06/07/2019, 10:58 AMJust pass all dependencies to constructorBut because you cannot do this for Android components, instead you can do this by getting from Application context
Mark
06/07/2019, 12:20 PMMyApplication
object (Application
subclass) has a property myImmutableConfig
and there is a singleton (by intention) class that can be instantiated by suspend fun createMySingleton(myImmutableConfig): MySingleton
and I want to be able to access MySingleton from a suspending function. If accessed from a blocking function, then presumably it would potentially block the main thread.private lateinit var INSTANCE: MySingleton
suspend fun getInstance(): MySingleton {
if(!::INSTANCE.isInitialized){
INSTANCE = createMySingleton(myApplication.myImmutableConfig)
}
return INSTANCE
}
fun getInstanceBlocking(): MySingleton {
if(!::INSTANCE.isInitialized) {
runBlocking {
return@runBlocking getInstance()
}
}
return INSTANCE
}
gildor
06/07/2019, 1:04 PMval myConfig: Deferred<MyConfig> = GlobalScope.async(start = Lazy) {
createMyConfig(myApplication.myImmutableConfig)
}
And to use just myConfig.await()
Mark
06/07/2019, 1:06 PMgildor
06/07/2019, 1:06 PMprivate val myConfig: Deferred<MyConfig> = ...
suspend fun myConfig(): MyConfig = myConfig.await()
Mark
06/07/2019, 1:13 PMprivate val INSTANCE_DEFERRED = GlobalScope.async(start = CoroutineStart.LAZY) {
createMySingleton(myApplication.myImmutableConfig)
}
suspend fun getInstance() = INSTANCE_DEFERRED.await()
gildor
06/07/2019, 1:16 PMMark
06/07/2019, 1:18 PMfun getInstanceBlocking() = if (INSTANCE_DEFERRED.isCompleted) {
INSTANCE_DEFERRED.getCompleted()
} else {
runBlocking {
getInstance()
}
}
gildor
06/07/2019, 1:36 PMMark
06/07/2019, 1:37 PMgildor
06/07/2019, 1:38 PMMark
06/07/2019, 1:39 PM