SauceNaoAPI/src/commonMain/kotlin/dev/inmo/saucenaoapi/utils/TimeManager.kt

102 lines
3.0 KiB
Kotlin
Raw Normal View History

2020-12-02 08:39:54 +00:00
package dev.inmo.saucenaoapi.utils
2019-10-12 06:30:02 +00:00
2022-05-14 07:44:44 +00:00
import com.soywiz.klock.DateTime
2020-12-02 08:39:54 +00:00
import dev.inmo.saucenaoapi.additional.LONG_TIME_RECALCULATING_MILLIS
import dev.inmo.saucenaoapi.additional.SHORT_TIME_RECALCULATING_MILLIS
2022-05-14 07:43:28 +00:00
import kotlinx.coroutines.*
2019-10-12 06:30:02 +00:00
import kotlinx.coroutines.channels.Channel
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(
2022-05-14 07:43:28 +00:00
private val deferred: CompletableDeferred<DateTime?>
2019-10-12 06:30:02 +00:00
) : TimeManagerAction {
override suspend fun makeChangeWith(times: MutableList<DateTime>) {
times.clearTooOldTimes()
2022-05-14 07:43:28 +00:00
deferred.complete(times.minOrNull())
2019-10-12 06:30:02 +00:00
}
}
private data class TimeManagerMostOldestInShortGetter(
2022-05-14 07:43:28 +00:00
private val deferred: CompletableDeferred<DateTime?>
2019-10-12 06:30:02 +00:00
) : 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
2022-05-14 07:43:28 +00:00
deferred.complete(
times.asSequence().filter {
limitTime < it
}.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
2022-05-14 07:43:28 +00:00
) {
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)
}
2022-05-14 07:43:28 +00:00
}.also {
it.invokeOnCompletion {
actionsChannel.close(it)
}
2019-10-12 06:30:02 +00:00
}
suspend fun addTimeAndClear() {
actionsChannel.send(TimeManagerTimeAdder())
}
suspend fun getMostOldestInLongPeriod(): DateTime? {
2022-05-14 07:43:28 +00:00
val deferred = CompletableDeferred<DateTime?>()
return if (actionsChannel.trySend(TimeManagerMostOldestInLongGetter(deferred)).isSuccess) {
deferred.await()
} else {
null
2019-10-12 06:30:02 +00:00
}
}
suspend fun getMostOldestInShortPeriod(): DateTime? {
2022-05-14 07:43:28 +00:00
val deferred = CompletableDeferred<DateTime?>()
return if (actionsChannel.trySend(TimeManagerMostOldestInShortGetter(deferred)).isSuccess) {
deferred.await()
} else {
null
2019-10-12 06:30:02 +00:00
}
}
}