https://kotlinlang.org logo
Title
p

Paul Woitaschek

09/24/2019, 6:40 AM
How would I model this in flow? I have a
Flow<Int>
. If it doesn't emit anything within 200ms, emit
0
?
d
p

Paul Woitaschek

09/24/2019, 7:17 AM
I found a solution which is not typesafe
It basically builds on having a private object
Uninitialized
, emitting that in
onStart
and do a
transformLatest
which does a delay if it's an
Uninitialized
private object Uninitialized

fun Flow<Int>.blubb(): Flow<Int> {
  return onStart<Any?> { emit(Uninitialized) }.transformLatest { value ->
    if (value == Uninitialized) {
      delay(200)
      emit(0)
    } else {
      emit(value as Int)
    }
  }
}
If someone has a better solution tell me 🙂
g

gildor

09/24/2019, 7:23 AM
It’s fine solution if you implement it as universal operator
p

Paul Woitaschek

09/24/2019, 7:26 AM
Yeah, it's tested and lives in its own file
Actually it does something different than transforming ints but this was easier for an example 😉
g

gildor

09/24/2019, 7:28 AM
Not sure about corresponding RxJava operator, you can do something like timeout() + onErrorReturnNext() but it’s not exactly the same (maybe only with onErrorReturn + check exception type)
p

Paul Woitaschek

09/24/2019, 7:28 AM
It was a port from rx java
In rxjava it was actually way more complicated involving connected observables
g

gildor

09/24/2019, 7:30 AM
Port of which operator? Or your own?
p

Paul Woitaschek

09/24/2019, 7:33 AM
My own. It was transforming an observable<T> into an observable<LoadingState<T>>