Florian Walther (live streaming)
01/03/2022, 3:11 PMFlorian Walther (live streaming)
01/03/2022, 3:11 PMclass FakeTimer(private val scope: CoroutineScope) : Timer {
private var millisUntilFinished = 0L
private var timerJob: Job? = null
override fun startTimer(
millisInFuture: Long,
countDownInterval: Long,
onTick: (Long) -> Unit,
onFinish: () -> Unit
) {
millisUntilFinished = millisInFuture
timerJob = scope.launch {
while (true) {
if (millisUntilFinished > countDownInterval) {
delay(countDownInterval)
onTick(millisUntilFinished)
millisUntilFinished -= countDownInterval
} else {
delay(millisUntilFinished)
millisUntilFinished = 0L
onTick(millisUntilFinished)
break
}
}
onFinish()
}
}
override fun cancelTimer() {
timerJob?.cancel()
}
}
dewildte
01/03/2022, 4:26 PMNick Allen
01/03/2022, 5:29 PMonTick
takes. If you wait 10 millis and then onTick
takes 3 millis, the second onTick
will happen after 23 millis, not after 20 millis.Nick Allen
01/03/2022, 5:30 PMNick Allen
01/03/2022, 5:35 PMNick Allen
01/03/2022, 5:35 PMdelay
may take time to resume also because of dispatch.corneil
01/03/2022, 8:07 PMFlorian Walther (live streaming)
01/04/2022, 11:06 AMFlorian Walther (live streaming)
01/04/2022, 11:06 AMFlorian Walther (live streaming)
01/04/2022, 1:10 PMfun startTimer(
millisInFuture: Long,
countDownInterval: Long,
onTick: (Long) -> Unit,
onFinish: () -> Unit
) {
millisUntilFinished = millisInFuture
timerJob = scope.launch {
val startTime = timeSource.elapsedRealTime
val targetTime = startTime + millisInFuture
while (true) {
if (timeSource.elapsedRealTime < targetTime) {
delay(countDownInterval)
millisUntilFinished = targetTime - timeSource.elapsedRealTime
onTick(millisUntilFinished)
} else {
onFinish()
}
}
}
}