https://kotlinlang.org logo
#coroutines
Title
# coroutines
m

myanmarking

01/08/2020, 10:43 AM
is this custom operator correctly implemented ?
d

diesieben07

01/08/2020, 10:46 AM
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

myanmarking

01/08/2020, 10:47 AM
hm, i understand your point, but this is not the same since onStart won't mean the first event. Right ?
d

diesieben07

01/08/2020, 10:48 AM
Your code inserts a delay before the first element. Mine does the same
m

myanmarking

01/08/2020, 10:50 AM
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

diesieben07

01/08/2020, 10:51 AM
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.
2 Views