add opportunity to subscribe on quota changes

This commit is contained in:
InsanusMokrassar 2022-12-11 11:13:38 +06:00
parent 962d079f46
commit 9c25b08296
2 changed files with 23 additions and 4 deletions

View File

@ -1,5 +1,7 @@
# SauceNaoAPI Changelog
## 0.13.0
## 0.12.2
* Versions:

View File

@ -9,17 +9,34 @@ import dev.inmo.saucenaoapi.models.Header
import dev.inmo.saucenaoapi.models.LimitsState
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.merge
import kotlin.math.max
import kotlin.math.min
internal class RequestQuotaManager (
scope: CoroutineScope
) {
private var longQuota = 1
private var shortQuota = 1
private var longMaxQuota = 1
private var shortMaxQuota = 1
private val longQuotaFlow = MutableStateFlow(1)
private val shortQuotaFlow = MutableStateFlow(1)
private val longMaxQuotaFlow = MutableStateFlow(1)
private val shortMaxQuotaFlow = MutableStateFlow(1)
private var longQuota by longQuotaFlow::value
private var shortQuota by shortQuotaFlow::value
private var longMaxQuota by longMaxQuotaFlow::value
private var shortMaxQuota by shortMaxQuotaFlow::value
val limitsStateFlow = merge(
longQuotaFlow, shortQuotaFlow, longMaxQuotaFlow, shortMaxQuotaFlow
).map { _ ->
LimitsState(
shortMaxQuota,
longMaxQuota,
shortQuota,
longQuota
)
}
val limitsState: LimitsState
get() = LimitsState(
shortMaxQuota,