Sam Stone
03/22/2023, 4:43 PMclass ObservableList<T>: MutableList<T> by Collections.synchronizedList(ArrayList()) {
But when I override a function, it gives an error: Abstract member cannot be accessed directly
override fun add(element: T): Boolean {
val add = super.add(element)
mObserver.postValue(this)
return add
}
Sam
03/22/2023, 4:45 PMephemient
03/22/2023, 4:46 PMCollections.synchronizedList()
value, not super
Sam Stone
03/22/2023, 4:55 PMthis
?ephemient
03/22/2023, 4:56 PMclass ObservableList<T>(private val delegate = Collections.synchronizedList(ArrayList())) : MutableList<T> by delegate {
override fun add(element: T): Boolean {
delegate.add(element)
}
}
Sam Stone
03/22/2023, 4:57 PMephemient
03/22/2023, 4:59 PMSam Stone
03/22/2023, 5:02 PMclass Observable<T>() {
constructor(initialValue: T) : this() {
state.value = initialValue
}
private val state = MutableStateFlow<T?>(null)
var value: T
get() = state.value!!
set(value) { state.value = value }
private val observers = Collections.synchronizedList(mutableListOf<suspend (T) -> Unit>())
fun observe(observer: suspend (T) -> Unit) {
val suspendNullable: suspend (T?) -> Unit = { it?.let { it1 -> observer(it1) } } //don't want to expose that we use nulls internally
observers.add(suspendNullable)
appScope.launch {
state.collect(suspendNullable)
}
}
fun removeObserver(observer: suspend (T) -> Unit) {
observers.remove(observer)
}
}
ephemient
03/22/2023, 5:04 PMSam Stone
03/22/2023, 5:05 PMephemient
03/22/2023, 5:08 PMthread 1: thread 2:
observe(f) { observe(g) {
observers.add(f)
observers.add(g)
scope.launch()
scope.launch()
} }
that can happen, the same as in your previous version (just the other way around)Sam Stone
03/22/2023, 5:10 PMephemient
03/22/2023, 5:11 PMSam Stone
03/22/2023, 5:13 PMsciack
03/23/2023, 5:24 AMCopyOnWriteArrayList