diff --git a/architecture/src/commonMain/kotlin/observable/Disposable.kt b/architecture/src/commonMain/kotlin/observable/Disposable.kt index 72f07a763..9f780927c 100644 --- a/architecture/src/commonMain/kotlin/observable/Disposable.kt +++ b/architecture/src/commonMain/kotlin/observable/Disposable.kt @@ -27,16 +27,6 @@ import kotlin.jvm.JvmName typealias DisposeHandler = () -> Unit -internal fun > addObserver(observation: Observation, observer: (R) -> Unit) { - observation.observers.add(observer) -} - -internal fun > removeObserver(observation: Observation, observer: (R) -> Unit) { - observation.observers.remove(observer) -} - -internal fun > observers(observation: Observation): List<(R) -> Unit> = observation.observers - /** * Reference to an object that should be disposed in time */ diff --git a/architecture/src/commonMain/kotlin/observable/Observation.kt b/architecture/src/commonMain/kotlin/observable/Observation.kt index f2df5bcc3..c072c7a9a 100644 --- a/architecture/src/commonMain/kotlin/observable/Observation.kt +++ b/architecture/src/commonMain/kotlin/observable/Observation.kt @@ -108,8 +108,10 @@ open class Observation>(override val initia val result = (v as? ObservableOptional.Value<*>)?.value as R - observers(this@Observation).forEach { - it(result) + this@Observation.observers.synchronized { + forEach { + it(result) + } } return v as ObservableOptional } @@ -139,7 +141,7 @@ open class Observation>(override val initia // send the value before adding val lastResult = currentObserved onNext((lastResult as? ObservableOptional.Value<*>)?.value as R) - addObserver(this@Observation, onNext) + this@Observation.observers.add(onNext) // adding an observer often happens concurrently with initialization, // if we detect a change in the current observed value we re-send it to the added observer val newResult = currentObserved @@ -148,7 +150,7 @@ open class Observation>(override val initia } SimpleDisposable { handleOnMain { - removeObserver(this@Observation, onNext) + this@Observation.observers.remove(onNext) } } }