Skip to content

Commit 35904e4

Browse files
committed
Made derived's context configurable
1 parent 8f94847 commit 35904e4

File tree

1 file changed

+17
-6
lines changed
  • reactivestate-core/src/commonMain/kotlin/com/ensody/reactivestate

1 file changed

+17
-6
lines changed

reactivestate-core/src/commonMain/kotlin/com/ensody/reactivestate/Derived.kt

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import kotlinx.coroutines.flow.StateFlow
99
import kotlinx.coroutines.flow.callbackFlow
1010
import kotlinx.coroutines.flow.stateIn
1111
import kotlinx.coroutines.sync.Mutex
12+
import kotlin.coroutines.CoroutineContext
13+
import kotlin.coroutines.EmptyCoroutineContext
1214

1315
private fun Map<Any, FrozenAutoRunnerObservable<*, *>>.getFrozenValues(): List<Any?> =
1416
values.map { it.revisionedValue }
@@ -18,6 +20,7 @@ private fun Map<Any, FrozenAutoRunnerObservable<*, *>>.getNewValues(): List<Any?
1820

1921
private fun <T> derivedCached(
2022
synchronous: Boolean,
23+
context: CoroutineContext,
2124
observer: AutoRunCallback<T>,
2225
): StateFlow<T> {
2326
// The values of the observed dependencies. We only track this if nobody is subscribed.
@@ -77,24 +80,26 @@ private fun <T> derivedCached(
7780

7881
private fun <T> derivedOnDemand(
7982
synchronous: Boolean,
83+
context: CoroutineContext,
8084
observer: AutoRunCallback<T>,
8185
): StateFlow<T> =
8286
callbackFlow {
8387
autoRun { trySend(observer()) }
8488
awaitClose {}
85-
}.stateOnDemand(synchronous = synchronous, emitValueOnStart = false) {
89+
}.stateOnDemand(context = context, synchronous = synchronous, emitValueOnStart = false) {
8690
runWithResolver(observer)
8791
}
8892

8993
internal fun <T> scopelessDerived(
94+
context: CoroutineContext = EmptyCoroutineContext,
9095
synchronous: Boolean = true,
9196
cache: Boolean = true,
9297
observer: AutoRunCallback<T>,
9398
): StateFlow<T> =
9499
if (cache) {
95-
derivedCached(synchronous = synchronous, observer = observer)
100+
derivedCached(context = context, synchronous = synchronous, observer = observer)
96101
} else {
97-
derivedOnDemand(synchronous = synchronous, observer = observer)
102+
derivedOnDemand(context = context, synchronous = synchronous, observer = observer)
98103
}
99104

100105
/**
@@ -105,8 +110,13 @@ internal fun <T> scopelessDerived(
105110
* @param synchronous Whether `.value` access synchronously recomputes even if someone collects. Defaults to `true`.
106111
* @param cache Caching of [StateFlow.value] expensive computations while nobody collects. Defaults to `true`.
107112
*/
108-
public fun <T> derived(synchronous: Boolean = true, cache: Boolean = true, observer: AutoRunCallback<T>): StateFlow<T> =
109-
scopelessDerived(synchronous = synchronous, cache = cache, observer = observer)
113+
public fun <T> derived(
114+
context: CoroutineContext = EmptyCoroutineContext,
115+
synchronous: Boolean = true,
116+
cache: Boolean = true,
117+
observer: AutoRunCallback<T>,
118+
): StateFlow<T> =
119+
scopelessDerived(context = context, synchronous = synchronous, cache = cache, observer = observer)
110120

111121
/**
112122
* Creates a [StateFlow] that computes its value based on other [StateFlow]s via an [autoRun] block.
@@ -115,11 +125,12 @@ public fun <T> derived(synchronous: Boolean = true, cache: Boolean = true, obser
115125
* @param cache Caching of [StateFlow.value] expensive computations while nobody collects. Defaults to `true`.
116126
*/
117127
public fun <T> CoroutineLauncher.derived(
128+
context: CoroutineContext = EmptyCoroutineContext,
118129
synchronous: Boolean = true,
119130
cache: Boolean = synchronous,
120131
observer: AutoRunCallback<T>,
121132
): StateFlow<T> =
122-
scopelessDerived(synchronous = synchronous, cache = cache, observer = observer).also {
133+
scopelessDerived(context = context, synchronous = synchronous, cache = cache, observer = observer).also {
123134
// Keep the value asynchronously updated in the background
124135
if (!synchronous) launch { it.collect {} }
125136
}

0 commit comments

Comments
 (0)