Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy before iterating observables #843

Merged
merged 4 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion architecture/src/commonMain/kotlin/observable/Observation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ open class Observation<R : T, T, OO : ObservableOptional<R>>(override val initia
val result = (v as? ObservableOptional.Value<*>)?.value as R

[email protected] {
forEach {
// iterate on a copy to prevent concurrent modification
toTypedArray().forEach {
it(result)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Copyright 2024 Splendo Consulting B.V. The Netherlands

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/

package com.splendo.kaluga.architecture.observable

import com.splendo.kaluga.base.runBlocking
import com.splendo.kaluga.test.base.BaseTest
import kotlinx.coroutines.Dispatchers
import kotlin.test.Test
import kotlin.test.assertEquals

class ConcurrentModificationTest : BaseTest() {

@Test
fun testConcurrentModification() = runBlocking(Dispatchers.Main) {
val subject = subjectOf(0)
var observer3: Disposable? = null
var observer2: Disposable? = null
var dispose = false

val observer1 = subject.observe {
if (dispose) {
observer2!!.dispose()
}
}
observer2 = subject.observe {
if (dispose) {
observer1.dispose()
observer3!!.dispose()
}
}

observer3 = subject.observe {
// noop
}

dispose = true
// due to the weird order of observers disposing each other while they are iterated this will crash if we remove the `toTypedArray()` before `forEach()` in `Observation.kt::setValueUnconfined`
subject.post(1)

assertEquals(1, subject.current)
}
}
16 changes: 6 additions & 10 deletions resources-compose/src/main/kotlin/KalugaButton.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonColors
import androidx.compose.material3.ButtonDefaults
Expand All @@ -41,16 +39,12 @@ import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat
import com.splendo.kaluga.resources.DefaultColors
import com.splendo.kaluga.resources.KalugaColor
import com.splendo.kaluga.resources.KalugaImage
import com.splendo.kaluga.resources.StringStyleAttribute
import com.splendo.kaluga.resources.StyledStringBuilder
Expand Down Expand Up @@ -80,7 +74,9 @@ fun KalugaButton.Composable(modifier: Modifier = Modifier, elevation: ButtonElev

Button(
action,
Modifier.backgroundStyle(stateStyle.backgroundStyle).then(modifier),
Modifier
.backgroundStyle(stateStyle.backgroundStyle)
.then(modifier),
isEnabled,
shape = stateStyle.backgroundStyle.shape.shape,
ButtonColors(Color.Transparent, Color.Transparent, Color.Transparent, Color.Transparent),
Expand Down Expand Up @@ -127,14 +123,14 @@ private fun KalugaButtonStyle.WithImageAndText.Composable(modifier: Modifier, is
val isRightToLeft = LocalLayoutDirection.current == LayoutDirection.Rtl

when (imageGravity) {

ImageGravity.START, ImageGravity.LEFT, ImageGravity.END, ImageGravity.RIGHT ->
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(spacing.dp),
) {
if (imageGravity == ImageGravity.START && !isRightToLeft ||
imageGravity == ImageGravity.END && isRightToLeft ||
if (
(imageGravity == ImageGravity.START && !isRightToLeft) ||
(imageGravity == ImageGravity.END && isRightToLeft) ||
imageGravity == ImageGravity.LEFT
) {
imageComposable()
Expand Down
2 changes: 1 addition & 1 deletion resources-compose/src/main/kotlin/KalugaImage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fun TintedImage.Composable(
@Composable
private fun TintedImagePreview() {
val image = KalugaImage(AppCompatResources.getDrawable(LocalContext.current, android.R.drawable.ic_media_play)!!).tinted(
DefaultColors.blue
DefaultColors.blue,
)

image.Composable(contentDescription = "Play")
Expand Down
Loading