littlelightcz
04/28/2019, 8:58 AMrunBlocking(<http://Dispatchers.IO|Dispatchers.IO>) {
val myList = mutableListOf(1).asObservable()
launch {
myList.asFlow().collect { println("A $it") }
}
delay(1000)
myList += 2
launch {
myList.asFlow().collect { println("B $it") }
}
delay(1000)
}
Which produces:
A 1
B 1
B 2
Is there some sort of non-terminal collect
yet that would listen for further changes, so that I would see A 2
in the output when myList
gets updated?Zach Klippenstein (he/him) [MOD]
04/28/2019, 4:16 PMMutableList
that sends change notifications.
One thing you could do is use a BehaviorSubject<List<Int>>
and push new lists in when you add/remove/change items.littlelightcz
04/29/2019, 11:39 AM.asObservable()
will do exactly this. Or at least for JavaFx components it works as expected - they update automatically when the list changes (if they are bound to it).Zach Klippenstein (he/him) [MOD]
04/29/2019, 3:39 PMasObservable
turned a list of [1]
into an Observable
that just emitted 1
and then completed. Where does asObservable
come from?littlelightcz
04/29/2019, 9:07 PMZach Klippenstein (he/him) [MOD]
04/29/2019, 10:35 PMObservableList
docs and I think I understand what you meant now. It seems like the simplest thing to do would be to create a Flow<ListChangeListener.Change<T>>
and then handle changes manually. I'm not sure how you would convert that into a simple flow of elements though - you can emit added elements, but how would you handle changed or removed elements?
E.g.
val changes = flowViaChannel(bufferSize = UNLIMITED) { ch ->
val listener = ListChangeListener {
ch.offer(it)
}
myList.addListener(listener)
ch.invokeOnClose {
myList.removeListener(listener)
}
}
littlelightcz
04/30/2019, 12:39 PM