Merge pull request #6096 from vector-im/feature/bma/use_clock

Fix usage of System.currentTimeMillis().
This commit is contained in:
Adam Brown 2022-05-19 10:47:10 +01:00 committed by GitHub
commit 6bc97df0bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 3 additions and 2 deletions

View File

@ -16,6 +16,7 @@
package im.vector.lib.core.utils.flow package im.vector.lib.core.utils.flow
import android.os.SystemClock
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
@ -68,10 +69,10 @@ fun <T> Flow<T>.chunk(durationInMillis: Long): Flow<List<T>> {
@ExperimentalCoroutinesApi @ExperimentalCoroutinesApi
fun <T> Flow<T>.throttleFirst(windowDuration: Long): Flow<T> = flow { fun <T> Flow<T>.throttleFirst(windowDuration: Long): Flow<T> = flow {
var windowStartTime = System.currentTimeMillis() var windowStartTime = SystemClock.elapsedRealtime()
var emitted = false var emitted = false
collect { value -> collect { value ->
val currentTime = System.currentTimeMillis() val currentTime = SystemClock.elapsedRealtime()
val delta = currentTime - windowStartTime val delta = currentTime - windowStartTime
if (delta >= windowDuration) { if (delta >= windowDuration) {
windowStartTime += delta / windowDuration * windowDuration windowStartTime += delta / windowDuration * windowDuration