Skip to content

Commit 76cac3f

Browse files
committed
Fixed compareAndSwap
1 parent 107ee58 commit 76cac3f

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* Removed `ValueFlow`, `MutableValueFlow` and `SuspendMutableValueFlow`.
1212
* Renamed `launcherScope` to `scope` on `CoroutineLauncher` and all subtypes like `BaseReactiveState`.
1313
* Renamed modules: `reactivestate` -> `reactivestate-android`, `reactivestate-test` -> `reactivestate-android-test`. You might only want to use `reactivestate-compose` in case you don't need to add ViewModels to Activities/Fragments.
14+
* Fixed `toMutable` and other interceptors' `compareAndSwap`.
1415

1516
## 5.13.0
1617

reactivestate-core/src/commonMain/kotlin/com/ensody/reactivestate/StateFlowInterceptor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private class StateFlowInterceptor<T>(
8787

8888
override fun compareAndSet(expect: T, update: T): Boolean =
8989
mutex.withSpinLock {
90-
if (value != expect) {
90+
if (value == expect) {
9191
value = update
9292
true
9393
} else {

reactivestate-core/src/commonTest/kotlin/com/ensody/reactivestate/InterceptMutableStateFlowTest.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.ensody.reactivestate.test.CoroutineTest
44
import kotlinx.coroutines.flow.MutableStateFlow
55
import kotlinx.coroutines.flow.StateFlow
66
import kotlinx.coroutines.flow.asStateFlow
7+
import kotlinx.coroutines.flow.update
78
import kotlin.test.Test
89
import kotlin.test.assertEquals
910

@@ -17,6 +18,10 @@ internal class InterceptMutableStateFlowTest : CoroutineTest() {
1718
intercepted.value = 1
1819
assertEquals(1, base.value)
1920
assertEquals(1, intercepted.value)
21+
// Tests compareAndSwap
22+
intercepted.update { 2 }
23+
assertEquals(2, base.value)
24+
assertEquals(2, intercepted.value)
2025
}
2126

2227
@Test

0 commit comments

Comments
 (0)