Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d3c76d20 authored by Abhishek Aggarwal's avatar Abhishek Aggarwal
Browse files

advanced-privacy: run everything in same scope to prevent race conditions

parent 69f501bc
Loading
Loading
Loading
Loading
Loading
+6 −4
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import foundation.e.flowmvi.Reducer
import foundation.e.flowmvi.SingleEventProducer
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
@@ -104,11 +105,12 @@ open class BaseFeature<State : Any, in Action : Any, in Effect : Any, SingleEven
    }

    private suspend fun Flow<Action>.collectIntoHandler(
        callerCoroutineScope: CoroutineScope,
        @Suppress("UNUSED_PARAMETER")callerCoroutineScope: CoroutineScope,
        @Suppress("UNUSED_PARAMETER") logger: Logger
    ) {
        val scope = CoroutineScope(Dispatchers.IO + Job())
        onEach { action ->
            callerCoroutineScope.launch(Dispatchers.IO) {
            scope.launch {
                actor.invoke(_state.value, action)
                    .onEach { effect ->
                        mutex.withLock {
@@ -123,9 +125,9 @@ open class BaseFeature<State : Any, in Action : Any, in Effect : Any, SingleEven
                            }
                        }
                    }
                    .launchIn(coroutineScope)
                    .launchIn(scope)
            }
        }
            .launchIn(callerCoroutineScope)
            .launchIn(scope)
    }
}