SauceNaoAPI/src/commonMain/kotlin/com/insanusmokrassar/SauceNaoAPI/utils/TimeManager.kt

104 lines
3.0 KiB
Kotlin
Raw Normal View History

2020-08-29 13:09:54 +00:00
package com.insanusmokrassar.SauceNaoAPI.utils
2019-10-12 06:30:02 +00:00
2020-08-29 13:09:54 +00:00
import com.insanusmokrassar.SauceNaoAPI.additional.LONG_TIME_RECALCULATING_MILLIS
import com.insanusmokrassar.SauceNaoAPI.additional.SHORT_TIME_RECALCULATING_MILLIS
2019-12-12 18:47:52 +00:00
import com.soywiz.klock.DateTime
2019-10-12 06:30:02 +00:00
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.launch
import kotlin.coroutines.Continuation
import kotlin.coroutines.suspendCoroutine
private fun MutableList<DateTime>.clearTooOldTimes(relatedTo: DateTime = DateTime.now()) {
2019-12-13 16:45:10 +00:00
val limitValue = relatedTo - LONG_TIME_RECALCULATING_MILLIS
2019-10-12 06:30:02 +00:00
removeAll {
it < limitValue
}
}
private interface TimeManagerAction {
suspend fun makeChangeWith(times: MutableList<DateTime>)
suspend operator fun invoke(times: MutableList<DateTime>) = makeChangeWith(times)
}
private data class TimeManagerClean(private val relatedTo: DateTime = DateTime.now()) : TimeManagerAction {
override suspend fun makeChangeWith(times: MutableList<DateTime>) {
times.clearTooOldTimes(relatedTo)
}
}
private data class TimeManagerTimeAdder(
private val time: DateTime = DateTime.now()
) : TimeManagerAction {
override suspend fun makeChangeWith(times: MutableList<DateTime>) {
times.add(time)
times.clearTooOldTimes()
}
}
private data class TimeManagerMostOldestInLongGetter(
private val continuation: Continuation<DateTime?>
) : TimeManagerAction {
override suspend fun makeChangeWith(times: MutableList<DateTime>) {
times.clearTooOldTimes()
2020-08-22 17:00:07 +00:00
continuation.resumeWith(Result.success(times.minOrNull()))
2019-10-12 06:30:02 +00:00
}
}
private data class TimeManagerMostOldestInShortGetter(
private val continuation: Continuation<DateTime?>
) : TimeManagerAction {
override suspend fun makeChangeWith(times: MutableList<DateTime>) {
times.clearTooOldTimes()
val now = DateTime.now()
2019-12-13 16:45:10 +00:00
val limitTime = now - SHORT_TIME_RECALCULATING_MILLIS
2019-10-12 06:30:02 +00:00
continuation.resumeWith(
Result.success(
times.asSequence().filter {
limitTime < it
2020-08-22 17:00:07 +00:00
}.minOrNull()
2019-10-12 06:30:02 +00:00
)
)
}
}
2019-12-13 18:29:12 +00:00
internal class TimeManager(
2019-10-12 06:30:02 +00:00
scope: CoroutineScope
2020-08-29 13:09:54 +00:00
) : SauceCloseable {
2019-10-12 06:30:02 +00:00
private val actionsChannel = Channel<TimeManagerAction>(Channel.UNLIMITED)
private val timeUpdateJob = scope.launch {
val times = mutableListOf<DateTime>()
for (action in actionsChannel) {
action(times)
}
}
suspend fun addTimeAndClear() {
actionsChannel.send(TimeManagerTimeAdder())
}
suspend fun getMostOldestInLongPeriod(): DateTime? {
return suspendCoroutine {
actionsChannel.offer(
TimeManagerMostOldestInLongGetter(it)
)
}
}
suspend fun getMostOldestInShortPeriod(): DateTime? {
return suspendCoroutine {
actionsChannel.offer(TimeManagerMostOldestInShortGetter(it))
}
}
override fun close() {
actionsChannel.close()
timeUpdateJob.cancel()
}
}