Skip to content

Commit

Permalink
Merge pull request #5 from abhimuktheeswarar/develop
Browse files Browse the repository at this point in the history
Fix duplicate action emission
  • Loading branch information
abhimuktheeswarar authored Oct 29, 2021
2 parents 7e0de1a + f0d0f0c commit 81fb641
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Flywheel.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = 'Flywheel'
spec.version = '1.1.3-RC'
spec.version = '1.1.4-RC'
spec.homepage = 'https://github.com/abhimuktheeswarar/Flywheel'
spec.source = { :git => "https://github.com/abhimuktheeswarar/Flywheel.git", :tag => "v#{spec.version}" }
spec.authors = 'Abhi Muktheeswarar'
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ kotlin {
sourceSets {
val commonMain by getting {
dependencies {
implementation("com.msabhi:flywheel:1.1.3-RC")
implementation("com.msabhi:flywheel:1.1.4-RC")
}
}
}
Expand All @@ -41,7 +41,7 @@ kotlin {
```Kotlin
dependencies {

implementation("com.msabhi:flywheel-android:1.1.3-RC")
implementation("com.msabhi:flywheel-android:1.1.4-RC")
}
```

Expand All @@ -56,7 +56,7 @@ use_frameworks!
target 'MyApp' do
pod 'Flywheel', '~> 1.1.3-RC'
pod 'Flywheel', '~> 1.1.4-RC'
end
```
Expand All @@ -70,7 +70,7 @@ import PackageDescription
let package = Package(
name: "YOUR_PROJECT_NAME",
dependencies: [
.package(url: "https://github.com/abhimuktheeswarar/Flywheel.git", from: "1.1.3-RC"),
.package(url: "https://github.com/abhimuktheeswarar/Flywheel.git", from: "1.1.4-RC"),
]
)
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,24 @@ import com.msabhi.flywheel.ActionState
import com.msabhi.flywheel.StateReserve
import com.msabhi.flywheel.attachments.DispatcherProvider
import com.msabhi.flywheel.attachments.SideEffect
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch

class CounterSideEffect(stateReserve: StateReserve<CounterState>, dispatchers: DispatcherProvider) :
SideEffect<CounterState>(stateReserve, dispatchers) {

init {
//Used start = CoroutineStart.UNDISPATCHED to ensure the actions are collected, especially, the very first actions
scope.launch(start = CoroutineStart.UNDISPATCHED) { actions.collect(::handle) }
actionStates.onEach(::handle).launchIn(scope)
}

private fun handle(action: Action) {
}

private fun handle(actionState: ActionState<Action, CounterState>) {
when (actionState.action) {

Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ object Versions {
const val minSdk = 21
const val targetSdk = 29
const val buildTools = "30.0.3"
const val androidGradlePlugin = "7.0.2"
const val androidGradlePlugin = "7.0.3"

const val appCompat = "1.3.0"
const val core = "1.6.0"
Expand Down
33 changes: 20 additions & 13 deletions flywheel/src/commonMain/kotlin/com/msabhi/flywheel/Flywheel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ import com.msabhi.flywheel.utilities.MutableStateChecker
import com.msabhi.flywheel.utilities.assertStateValues
import com.msabhi.flywheel.utilities.name
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.channels.SendChannel
import kotlinx.coroutines.channels.*
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.selects.select

Expand Down Expand Up @@ -304,6 +301,9 @@ class StateReserve<S : State>(
middlewares: List<Middleware<S>>?,
) {

private val actionsChannel: Channel<Action> =
Channel(capacity = Channel.UNLIMITED, onBufferOverflow = BufferOverflow.SUSPEND)

private val inputActionsChannel: Channel<Action> =
Channel(capacity = Channel.UNLIMITED, onBufferOverflow = BufferOverflow.SUSPEND)

Expand Down Expand Up @@ -338,6 +338,11 @@ class StateReserve<S : State>(
*/
val states: Flow<S> = setStates

/**
* Returns a [Flow] of actions that are passed through reducer.
*/
val actionStates: Flow<ActionState.Always<Action, S>> = transitionsMutable.filterIsInstance()

/**
* Returns a [Flow] of actions that are passed through middleware and before reaching reducer.
* This also captures actions modified by middlewares.
Expand All @@ -346,11 +351,6 @@ class StateReserve<S : State>(
val actions: Flow<Action> =
mutableActions.distinctUntilChanged().filterNot { it is ForceDistinctAction }

/**
* Returns a [Flow] of actions that are passed through reducer.
*/
val actionStates: Flow<ActionState.Always<Action, S>> = transitionsMutable.filterIsInstance()

/**
* Enable `enhancedStateMachine` config to listen for `transitions`.
* Use this along with provided extension functions to collect state transitions to get a StateMachine like behaviour.
Expand All @@ -367,6 +367,16 @@ class StateReserve<S : State>(
}

init {

config.scope.launch {
actionsChannel.consumeEach { action ->
if (isActive) {
mutableActions.tryEmit(action)
this@StateReserve.middlewares?.invoke(action) ?: dispatcher(action)
}
}
}

config.scope.stateMachine(
initialState = initialState,
inputActions = inputActionsChannel,
Expand All @@ -378,8 +388,6 @@ class StateReserve<S : State>(
ignoreDuplicateState = config.ignoreDuplicateState,
enhancedStateMachine = config.enhancedStateMachine
)

initialState.deferredState?.invokeOnCompletion { }
}

private fun dispatcher(action: Action) {
Expand All @@ -395,8 +403,7 @@ class StateReserve<S : State>(
* It is the entry point for actions to update the StateReserve's state.
*/
fun dispatch(action: Action) {
mutableActions.tryEmit(action)
middlewares?.invoke(action) ?: dispatcher(action)
actionsChannel.trySend(action)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ kotlin.native.enableDependencyPropagation=false
org.gradle.daemon=true
org.gradle.jvmargs=-Xmx2560m
GROUP=com.msabhi
VERSION_NAME=1.1.3-RC
VERSION_NAME=1.1.4-RC
POM_NAME=Flywheel
POM_DESCRIPTION=Kotlin-Multiplatform state management library
POM_URL=https://github.com/abhimuktheeswarar/Flywheel
Expand Down

0 comments on commit 81fb641

Please sign in to comment.