From 4ff1943baed7660ab85a36079f381d32ad902458 Mon Sep 17 00:00:00 2001 From: Benjamin Helbig Date: Wed, 16 Feb 2022 15:43:11 +0100 Subject: [PATCH] Make activeFlowCounter atomic. It causes an InvalidMutabilityException on iOS otherwise. --- dependencies.gradle | 2 ++ dsl/build.gradle | 1 + .../flowredux/dsl/FlowReduxStateMachine.kt | 29 +++++++------------ 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/dependencies.gradle b/dependencies.gradle index e35d4058..506904f4 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -28,6 +28,7 @@ ext.versions = [ junit : '4.13.2', turbine : '0.7.0', + stately : '1.2.0' ] ext.libraries = [ @@ -41,6 +42,7 @@ ext.libraries = [ rxJava : "io.reactivex.rxjava2:rxjava:$versions.rxJava", rxAndroid : "io.reactivex.rxjava2:rxandroid:$versions.rxAndroid", flMadStateMachine : "com.freeletics.mad:state-machine:0.3.0-alpha26", + stately : "co.touchlab:stately-concurrency:$versions.stately", ] ext.compose = [ diff --git a/dsl/build.gradle b/dsl/build.gradle index 9eb3c1a8..df4c0eb2 100644 --- a/dsl/build.gradle +++ b/dsl/build.gradle @@ -33,6 +33,7 @@ kotlin { dependencies { api project(":flowredux") api libraries.flMadStateMachine + implementation libraries.stately } } diff --git a/dsl/src/commonMain/kotlin/com/freeletics/flowredux/dsl/FlowReduxStateMachine.kt b/dsl/src/commonMain/kotlin/com/freeletics/flowredux/dsl/FlowReduxStateMachine.kt index 773149db..52b4cfdb 100644 --- a/dsl/src/commonMain/kotlin/com/freeletics/flowredux/dsl/FlowReduxStateMachine.kt +++ b/dsl/src/commonMain/kotlin/com/freeletics/flowredux/dsl/FlowReduxStateMachine.kt @@ -1,5 +1,7 @@ package com.freeletics.flowredux.dsl +import co.touchlab.stately.concurrency.AtomicInt +import co.touchlab.stately.concurrency.value import com.freeletics.flowredux.FlowReduxLogger import com.freeletics.mad.statemachine.StateMachine import kotlinx.coroutines.ExperimentalCoroutinesApi @@ -9,8 +11,6 @@ import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.onCompletion import kotlinx.coroutines.flow.onStart import kotlinx.coroutines.flow.receiveAsFlow -import kotlinx.coroutines.sync.Mutex -import kotlinx.coroutines.sync.withLock @FlowPreview @ExperimentalCoroutinesApi @@ -24,8 +24,7 @@ public abstract class FlowReduxStateMachine( private val inputActions = Channel() private lateinit var outputState: Flow - private val activeFlowCounterMutex = Mutex() - private var activeFlowCounter = 0 + private val activeFlowCounter = AtomicInt(0) public constructor(initialState: S, logger: FlowReduxLogger? = null) : this( logger = logger, @@ -43,14 +42,10 @@ public abstract class FlowReduxStateMachine( .receiveAsFlow() .reduxStore(logger, initialState, specBlock) .onStart { - activeFlowCounterMutex.withLock { - activeFlowCounter++ - } + activeFlowCounter.incrementAndGet() } .onCompletion { - activeFlowCounterMutex.withLock { - activeFlowCounter-- - } + activeFlowCounter.decrementAndGet() } } @@ -62,14 +57,12 @@ public abstract class FlowReduxStateMachine( override suspend fun dispatch(action: A) { checkSpecBlockSet() - activeFlowCounterMutex.withLock { - if (activeFlowCounter <= 0) { - throw IllegalStateException( - "Cannot dispatch action $action because state Flow of this " + - "FlowReduxStateMachine is not collected yet. " + - "Start collecting the state Flow before dispatching any action." - ) - } + if (activeFlowCounter.value <= 0) { + throw IllegalStateException( + "Cannot dispatch action $action because state Flow of this " + + "FlowReduxStateMachine is not collected yet. " + + "Start collecting the state Flow before dispatching any action." + ) } inputActions.send(action) }