-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement retained store builders and immediate lifecycle
- Loading branch information
Showing
9 changed files
with
139 additions
and
34 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
core/src/commonMain/kotlin/pro/respawn/flowmvi/util/ImmediateDispatcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package pro.respawn.flowmvi.util | ||
|
||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.MainCoroutineDispatcher | ||
import kotlin.concurrent.Volatile | ||
|
||
@Volatile | ||
private var isImmediateSupported: Boolean = true | ||
|
||
@Suppress("UnusedReceiverParameter") | ||
public val MainCoroutineDispatcher.immediateOrDefault: MainCoroutineDispatcher | ||
get() { | ||
if (isImmediateSupported) { | ||
try { | ||
return Dispatchers.Main.immediate | ||
} catch (ignored: UnsupportedOperationException) { | ||
} catch (ignored: NotImplementedError) { | ||
} | ||
|
||
isImmediateSupported = false | ||
} | ||
return Dispatchers.Main | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
decompose/src/commonMain/kotlin/pro/respawn/flowmvi/decompose/dsl/Internal.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package pro.respawn.flowmvi.decompose.dsl | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import pro.respawn.flowmvi.api.MVIAction | ||
import pro.respawn.flowmvi.api.MVIIntent | ||
import pro.respawn.flowmvi.api.MVIState | ||
import pro.respawn.flowmvi.api.Store | ||
import pro.respawn.flowmvi.decompose.api.RetainedStore | ||
|
||
@PublishedApi | ||
internal fun <S : MVIState, I : MVIIntent, A : MVIAction> retained( | ||
store: Store<S, I, A>, | ||
scope: CoroutineScope?, | ||
): RetainedStore<S, I, A> = object : Store<S, I, A> by store, RetainedStore<S, I, A> { | ||
init { | ||
if (scope != null) start(scope) | ||
} | ||
} | ||
|
||
@PublishedApi | ||
internal fun <S : MVIState, I : MVIIntent, A : MVIAction> Store<S, I, A>.retain( | ||
scope: CoroutineScope? | ||
): RetainedStore<S, I, A> = retained(this, scope) |
25 changes: 13 additions & 12 deletions
25
decompose/src/commonMain/kotlin/pro/respawn/flowmvi/decompose/dsl/RetainedScope.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,28 @@ | ||
// TODO: https://github.com/arkivanov/Essenty/issues/158 | ||
@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") | ||
|
||
package pro.respawn.flowmvi.decompose.dsl | ||
|
||
import com.arkivanov.decompose.ComponentContext | ||
import com.arkivanov.essenty.instancekeeper.InstanceKeeper | ||
import com.arkivanov.essenty.instancekeeper.InstanceKeeperOwner | ||
import com.arkivanov.essenty.instancekeeper.getOrCreate | ||
import com.arkivanov.essenty.lifecycle.coroutines.immediateOrFallback | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.SupervisorJob | ||
import pro.respawn.flowmvi.decompose.api.RetainedScope | ||
import pro.respawn.flowmvi.util.immediateOrDefault | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
public fun createRetainedScope( | ||
context: CoroutineContext = Dispatchers.Main.immediateOrFallback | ||
private const val DefaultScopeKey = "CoroutineScope" | ||
|
||
internal fun createRetainedScope( | ||
context: CoroutineContext = Dispatchers.Main.immediateOrDefault | ||
): RetainedScope = object : RetainedScope, CoroutineScope by CoroutineScope(context + SupervisorJob(context[Job])) {} | ||
|
||
public fun InstanceKeeper.retainedScope( | ||
context: CoroutineContext = Dispatchers.Main.immediateOrFallback, | ||
): CoroutineScope = getOrCreate("CoroutineScope") { createRetainedScope(context) } | ||
context: CoroutineContext = Dispatchers.Main.immediateOrDefault, | ||
key: String = DefaultScopeKey, | ||
): CoroutineScope = getOrCreate(key) { createRetainedScope(context) } | ||
|
||
public fun ComponentContext.retainedScope( | ||
context: CoroutineContext = Dispatchers.Main.immediateOrFallback, | ||
): CoroutineScope = instanceKeeper.retainedScope(context) | ||
public fun InstanceKeeperOwner.retainedScope( | ||
context: CoroutineContext = Dispatchers.Main.immediateOrDefault, | ||
key: String = DefaultScopeKey, | ||
): CoroutineScope = instanceKeeper.retainedScope(context, key) |
40 changes: 23 additions & 17 deletions
40
decompose/src/commonMain/kotlin/pro/respawn/flowmvi/decompose/dsl/RetainedStore.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,41 @@ | ||
@file:Suppress("Indentation") // conflict between detekt <> ide | ||
package pro.respawn.flowmvi.decompose.dsl | ||
|
||
import com.arkivanov.decompose.ComponentContext | ||
import com.arkivanov.essenty.instancekeeper.InstanceKeeper | ||
import com.arkivanov.essenty.instancekeeper.InstanceKeeperOwner | ||
import com.arkivanov.essenty.instancekeeper.getOrCreate | ||
import kotlinx.coroutines.CoroutineScope | ||
import pro.respawn.flowmvi.api.MVIAction | ||
import pro.respawn.flowmvi.api.MVIIntent | ||
import pro.respawn.flowmvi.api.MVIState | ||
import pro.respawn.flowmvi.api.Store | ||
import pro.respawn.flowmvi.decompose.api.RetainedStore | ||
import pro.respawn.flowmvi.util.nameByType | ||
|
||
public fun <S : MVIState, I : MVIIntent, A : MVIAction> retained( | ||
store: Store<S, I, A>, | ||
): RetainedStore<S, I, A> = object : Store<S, I, A> by store, RetainedStore<S, I, A> {} | ||
// keeper | ||
|
||
public inline fun <reified S : MVIState, I : MVIIntent, A : MVIAction> InstanceKeeper.retainedStore( | ||
store: Store<S, I, A>, | ||
key: Any = store.name ?: nameByType<S>()?.let { "${it}Store" } ?: store::class, | ||
): Store<S, I, A> = getOrCreate(key = key) { retained(store) } | ||
scope: CoroutineScope? = retainedScope(), | ||
key: Any = "${requireNotNull(nameByType<S>())}Store", | ||
@BuilderInference factory: () -> Store<S, I, A>, | ||
): Store<S, I, A> = getOrCreate(key) { retained(factory(), scope) } | ||
|
||
public fun <S : MVIState, I : MVIIntent, A : MVIAction> InstanceKeeper.retainedStore( | ||
public inline fun <S : MVIState, I : MVIIntent, A : MVIAction> InstanceKeeper.retainedStore( | ||
key: Any, | ||
store: Store<S, I, A>, | ||
): Store<S, I, A> = getOrCreate(key = key) { retained(store) } | ||
scope: CoroutineScope? = retainedScope(), | ||
factory: () -> Store<S, I, A>, | ||
): Store<S, I, A> = getOrCreate(key) { retained(factory(), scope) } | ||
|
||
public fun <S : MVIState, I : MVIIntent, A : MVIAction> ComponentContext.retainedStore( | ||
// keeper owner | ||
|
||
public inline fun <S : MVIState, I : MVIIntent, A : MVIAction> InstanceKeeperOwner.retainedStore( | ||
key: Any, | ||
store: Store<S, I, A>, | ||
): Store<S, I, A> = instanceKeeper.retainedStore(key, store) | ||
scope: CoroutineScope? = retainedScope(), | ||
factory: () -> Store<S, I, A>, | ||
): Store<S, I, A> = instanceKeeper.retainedStore(key, scope, factory) | ||
|
||
public inline fun <reified S : MVIState, I : MVIIntent, A : MVIAction> ComponentContext.retainedStore( | ||
store: Store<S, I, A>, | ||
key: Any = store.name ?: nameByType<S>()?.let { "${it}Store" } ?: store::class, | ||
): Store<S, I, A> = instanceKeeper.retainedStore(store, key) | ||
public inline fun <reified S : MVIState, I : MVIIntent, A : MVIAction> InstanceKeeperOwner.retainedStore( | ||
scope: CoroutineScope? = retainedScope(), | ||
key: Any = "${requireNotNull(nameByType<S>())}Store", | ||
@BuilderInference factory: () -> Store<S, I, A>, | ||
): Store<S, I, A> = instanceKeeper.retainedStore(key, scope, factory) |
50 changes: 50 additions & 0 deletions
50
decompose/src/commonMain/kotlin/pro/respawn/flowmvi/decompose/dsl/RetainedStoreBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package pro.respawn.flowmvi.decompose.dsl | ||
|
||
import com.arkivanov.essenty.instancekeeper.InstanceKeeper | ||
import com.arkivanov.essenty.instancekeeper.InstanceKeeperOwner | ||
import com.arkivanov.essenty.instancekeeper.getOrCreate | ||
import kotlinx.coroutines.CoroutineScope | ||
import pro.respawn.flowmvi.api.MVIAction | ||
import pro.respawn.flowmvi.api.MVIIntent | ||
import pro.respawn.flowmvi.api.MVIState | ||
import pro.respawn.flowmvi.api.Store | ||
import pro.respawn.flowmvi.dsl.BuildStore | ||
import pro.respawn.flowmvi.dsl.store | ||
import pro.respawn.flowmvi.util.nameByType | ||
|
||
// keeper | ||
|
||
public inline fun <S : MVIState, I : MVIIntent, A : MVIAction> InstanceKeeper.retainedStore( | ||
initial: S, | ||
name: String, | ||
scope: CoroutineScope? = retainedScope(), | ||
@BuilderInference builder: BuildStore<S, I, A>, | ||
): Store<S, I, A> = getOrCreate(name) { | ||
store(initial) { | ||
this.name = name | ||
builder() | ||
}.retain(scope) | ||
} | ||
|
||
public inline fun <reified S : MVIState, I : MVIIntent, A : MVIAction> InstanceKeeper.retainedStore( | ||
initial: S, | ||
scope: CoroutineScope? = retainedScope(), | ||
name: String = "${requireNotNull(nameByType<S>())}Store", | ||
@BuilderInference builder: BuildStore<S, I, A>, | ||
): Store<S, I, A> = retainedStore(initial, name, scope, builder) | ||
|
||
// owner | ||
|
||
public inline fun <S : MVIState, I : MVIIntent, A : MVIAction> InstanceKeeperOwner.retainedStore( | ||
initial: S, | ||
name: String, | ||
scope: CoroutineScope? = retainedScope(), | ||
@BuilderInference builder: BuildStore<S, I, A>, | ||
): Store<S, I, A> = instanceKeeper.retainedStore(initial, name, scope, builder) | ||
|
||
public inline fun <reified S : MVIState, I : MVIIntent, A : MVIAction> InstanceKeeperOwner.retainedStore( | ||
initial: S, | ||
scope: CoroutineScope? = retainedScope(), | ||
name: String = "${requireNotNull(nameByType<S>())}Store", | ||
@BuilderInference builder: BuildStore<S, I, A>, | ||
): Store<S, I, A> = instanceKeeper.retainedStore(initial, name, scope, builder) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters