Skip to content

Commit

Permalink
#810 remove legacy internal methods and safely iterate over observabl…
Browse files Browse the repository at this point in the history
…e listeners
  • Loading branch information
thoutbeckers committed Sep 23, 2024
1 parent 63eed61 commit ff60fe1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 14 deletions.
10 changes: 0 additions & 10 deletions architecture/src/commonMain/kotlin/observable/Disposable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,6 @@ import kotlin.jvm.JvmName

typealias DisposeHandler = () -> Unit

internal fun <R : T, T, OO : ObservableOptional<R>> addObserver(observation: Observation<R, T, OO>, observer: (R) -> Unit) {
observation.observers.add(observer)
}

internal fun <R : T, T, OO : ObservableOptional<R>> removeObserver(observation: Observation<R, T, OO>, observer: (R) -> Unit) {
observation.observers.remove(observer)
}

internal fun <R : T, T, OO : ObservableOptional<R>> observers(observation: Observation<R, T, OO>): List<(R) -> Unit> = observation.observers

/**
* Reference to an object that should be disposed in time
*/
Expand Down
10 changes: 6 additions & 4 deletions architecture/src/commonMain/kotlin/observable/Observation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,10 @@ open class Observation<R : T, T, OO : ObservableOptional<R>>(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<T>
}
Expand Down Expand Up @@ -139,7 +141,7 @@ open class Observation<R : T, T, OO : ObservableOptional<R>>(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
Expand All @@ -148,7 +150,7 @@ open class Observation<R : T, T, OO : ObservableOptional<R>>(override val initia
}
SimpleDisposable {
handleOnMain {
removeObserver(this@Observation, onNext)
this@Observation.observers.remove(onNext)
}
}
}
Expand Down

0 comments on commit ff60fe1

Please sign in to comment.