Adding WeakHashMap to hold scopes for ScopeProviders

This commit is contained in:
tys
2022-04-22 15:07:37 -07:00
parent 4dc37ef84d
commit 22d7868a71
3 changed files with 23 additions and 15 deletions
@@ -35,10 +35,9 @@ class MainInteractor(
override fun didBecomeActive(savedInstanceState: Bundle?) {
super.didBecomeActive(savedInstanceState)
CoroutineScope(RibDispatchers.Main).launch { }
router.view.setContent { MainView(childContent = childContent) }
authStream.observe()
authStream.observe()
.onEach {
if (it.isLoggedIn) {
router.detachLoggedOut()
@@ -35,7 +35,6 @@ class LoggedOutInteractor(
private val eventStream: EventStream<LoggedOutEvent>,
private val stateStream: StateStream<LoggedOutViewModel>
) : BasicInteractor<ComposePresenter, LoggedOutRouter>(presenter) {
override fun didBecomeActive(savedInstanceState: Bundle?) {
super.didBecomeActive(savedInstanceState)
eventStream.observe()
@@ -3,21 +3,31 @@ package com.uber.rib.core
import com.uber.autodispose.ScopeProvider
import com.uber.autodispose.coroutinesinterop.asCoroutineScope
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import java.util.*
import kotlin.reflect.KProperty
//TODO add weak hashmap to hold values of getters.
val ScopeProvider.defaultScope by LazyWithReceiver<ScopeProvider, CoroutineScope> {
asCoroutineScope(SupervisorJob() + RibDispatchers.Main)
}
val ScopeProvider.defaultScope: CoroutineScope
get() = asCoroutineScope(SupervisorJob() + RibDispatchers.Default)
val ScopeProvider.mainScope by LazyWithReceiver<ScopeProvider, CoroutineScope> {
asCoroutineScope(SupervisorJob() + RibDispatchers.Main)
}
val ScopeProvider.mainScope: CoroutineScope
get() = asCoroutineScope(SupervisorJob() + RibDispatchers.Main)
val ScopeProvider.ioScope: CoroutineScope
get() = asCoroutineScope(SupervisorJob() + RibDispatchers.IO)
val ScopeProvider.unconfinedScope: CoroutineScope
get() = asCoroutineScope(SupervisorJob() + RibDispatchers.Unconfined)
val ScopeProvider.ioScope by LazyWithReceiver<ScopeProvider, CoroutineScope> {
asCoroutineScope(SupervisorJob() + RibDispatchers.IO)
}
val ScopeProvider.unconfinedScope by LazyWithReceiver<ScopeProvider, CoroutineScope> {
asCoroutineScope(SupervisorJob() + RibDispatchers.Unconfined)
}
private class LazyWithReceiver<This,Return>(val initializer:This.()->Return)
{
private val values = WeakHashMap<This,Return>()
operator fun getValue(thisRef:This, property: KProperty<*>):Return = synchronized(values)
{
return values.getOrPut(thisRef) { thisRef.initializer() }
}
}