Skip to content

Commit

Permalink
Merge pull request #1 from conhealth/feature/atomics_usage
Browse files Browse the repository at this point in the history
Add usage of atomic variables
  • Loading branch information
BenjaminLifeTime authored Feb 16, 2022
2 parents 5bbfe50 + 4ff1943 commit bfda08b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
2 changes: 2 additions & 0 deletions dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ ext.versions = [

junit : '4.13.2',
turbine : '0.7.0',
stately : '1.2.0'
]

ext.libraries = [
Expand All @@ -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 = [
Expand Down
1 change: 1 addition & 0 deletions dsl/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ kotlin {
dependencies {
api project(":flowredux")
api libraries.flMadStateMachine
implementation libraries.stately
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -24,8 +24,7 @@ public abstract class FlowReduxStateMachine<S : Any, A : Any>(
private val inputActions = Channel<A>()
private lateinit var outputState: Flow<S>

private val activeFlowCounterMutex = Mutex()
private var activeFlowCounter = 0
private val activeFlowCounter = AtomicInt(0)

public constructor(initialState: S, logger: FlowReduxLogger? = null) : this(
logger = logger,
Expand All @@ -43,14 +42,10 @@ public abstract class FlowReduxStateMachine<S : Any, A : Any>(
.receiveAsFlow()
.reduxStore(logger, initialState, specBlock)
.onStart {
activeFlowCounterMutex.withLock {
activeFlowCounter++
}
activeFlowCounter.incrementAndGet()
}
.onCompletion {
activeFlowCounterMutex.withLock {
activeFlowCounter--
}
activeFlowCounter.decrementAndGet()
}
}

Expand All @@ -62,14 +57,12 @@ public abstract class FlowReduxStateMachine<S : Any, A : Any>(

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)
}
Expand Down

0 comments on commit bfda08b

Please sign in to comment.