is this custom operator correctly implemented ?
# coroutines
m
is this custom operator correctly implemented ?
d
A flow is always collected sequentially, therefor you don't need
AtomicReference
and you can just use a normal
var
. You can also further simplify this operator:
Copy code
fun <T> Flow<T>.delayFirst(
    time: Long,
    timeUnit: TimeUnit = TimeUnit.MILLISECONDS
): Flow<T> {
    return this.onStart { delay(timeUnit.toMillis(time)) }
}
m
hm, i understand your point, but this is not the same since onStart won't mean the first event. Right ?
d
Your code inserts a delay before the first element. Mine does the same
m
As i understand it, it doesn't. Mine adds a delay once it receives the first event. your's just adds a delay when subscribed, isn't it ?
hm. i just read onStart. coroutine code is sometimes hard to understand ..
d
On the first element yours calls
getAndSet(false
, which returns true. Therefor you then delay, before emitting the first element. On all subsequent elements
getAndSet(false
returns
false
, meaning no delay.