@@ -144,8 +144,8 @@ public fun <T> Flow<T>.shareIn(
144144 onBufferOverflow = config.onBufferOverflow
145145 )
146146 @Suppress(" UNCHECKED_CAST" )
147- scope.launchSharing(config.context, config.upstream, shared, started, NO_VALUE as T )
148- return shared.asSharedFlow( )
147+ val job = scope.launchSharing(config.context, config.upstream, shared, started, NO_VALUE as T )
148+ return ReadonlySharedFlow (shared, job )
149149}
150150
151151private class SharingConfig <T >(
@@ -197,7 +197,7 @@ private fun <T> CoroutineScope.launchSharing(
197197 shared : MutableSharedFlow <T >,
198198 started : SharingStarted ,
199199 initialValue : T
200- ) {
200+ ): Job =
201201 launch(context) { // the single coroutine to rule the sharing
202202 // Optimize common built-in started strategies
203203 when {
@@ -230,7 +230,6 @@ private fun <T> CoroutineScope.launchSharing(
230230 }
231231 }
232232 }
233- }
234233
235234// -------------------------------- stateIn --------------------------------
236235
@@ -303,8 +302,8 @@ public fun <T> Flow<T>.stateIn(
303302): StateFlow <T > {
304303 val config = configureSharing(1 )
305304 val state = MutableStateFlow (initialValue)
306- scope.launchSharing(config.context, config.upstream, state, started, initialValue)
307- return state.asStateFlow( )
305+ val job = scope.launchSharing(config.context, config.upstream, state, started, initialValue)
306+ return ReadonlyStateFlow (state, job )
308307}
309308
310309/* *
@@ -332,7 +331,7 @@ private fun <T> CoroutineScope.launchSharingDeferred(
332331 upstream.collect { value ->
333332 state?.let { it.value = value } ? : run {
334333 state = MutableStateFlow (value).also {
335- result.complete(it.asStateFlow( ))
334+ result.complete(ReadonlyStateFlow (it, coroutineContext.job ))
336335 }
337336 }
338337 }
@@ -351,23 +350,27 @@ private fun <T> CoroutineScope.launchSharingDeferred(
351350 * Represents this mutable shared flow as a read-only shared flow.
352351 */
353352public fun <T > MutableSharedFlow<T>.asSharedFlow (): SharedFlow <T > =
354- ReadonlySharedFlow (this )
353+ ReadonlySharedFlow (this , null )
355354
356355/* *
357356 * Represents this mutable state flow as a read-only state flow.
358357 */
359358public fun <T > MutableStateFlow<T>.asStateFlow (): StateFlow <T > =
360- ReadonlyStateFlow (this )
359+ ReadonlyStateFlow (this , null )
361360
362361private class ReadonlySharedFlow <T >(
363- flow : SharedFlow <T >
362+ flow : SharedFlow <T >,
363+ @Suppress(" unused" )
364+ private val job : Job ? // keeps a strong reference to the job (if present)
364365) : SharedFlow<T> by flow, CancellableFlow<T>, FusibleFlow<T> {
365366 override fun fuse (context : CoroutineContext , capacity : Int , onBufferOverflow : BufferOverflow ) =
366367 fuseSharedFlow(context, capacity, onBufferOverflow)
367368}
368369
369370private class ReadonlyStateFlow <T >(
370- flow : StateFlow <T >
371+ flow : StateFlow <T >,
372+ @Suppress(" unused" )
373+ private val job : Job ? // keeps a strong reference to the job (if present)
371374) : StateFlow<T> by flow, CancellableFlow<T>, FusibleFlow<T> {
372375 override fun fuse (context : CoroutineContext , capacity : Int , onBufferOverflow : BufferOverflow ) =
373376 fuseStateFlow(context, capacity, onBufferOverflow)
0 commit comments