Add cart module and requests queue helper
This commit is contained in:
parent
048c0a43a2
commit
ec1d6e5e61
|
|
@ -0,0 +1 @@
|
|||
/build
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
apply from: "../android-configs/lib-config.gradle"
|
||||
|
||||
dependencies {
|
||||
def coroutinesVersion = '1.6.4'
|
||||
def junitVersion = '4.13.2'
|
||||
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core")
|
||||
testImplementation("junit:junit")
|
||||
|
||||
constraints {
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core") {
|
||||
version {
|
||||
require(coroutinesVersion)
|
||||
}
|
||||
}
|
||||
testImplementation("junit:junit") {
|
||||
version {
|
||||
require(junitVersion)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
<manifest package="ru.touchin.roboswag.core.cart_utils" />
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
package ru.touchin.roboswag.cart_utils.requests_qeue
|
||||
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.flow.consumeAsFlow
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
|
||||
/**
|
||||
* Queue for abstract requests which will be executed one after another
|
||||
*/
|
||||
typealias Request<TResponse> = suspend () -> TResponse
|
||||
|
||||
class RequestsQueue<TRequest : Request<*>> {
|
||||
|
||||
private val requestChannel = Channel<TRequest>(capacity = Channel.BUFFERED)
|
||||
|
||||
fun initRequestsExecution(
|
||||
coroutineScope: CoroutineScope,
|
||||
executeRequestAction: suspend (TRequest) -> Unit,
|
||||
) {
|
||||
requestChannel
|
||||
.consumeAsFlow()
|
||||
.onEach { executeRequestAction.invoke(it) }
|
||||
.launchIn(coroutineScope)
|
||||
}
|
||||
|
||||
fun addToQueue(request: TRequest) {
|
||||
requestChannel.trySend(request)
|
||||
}
|
||||
|
||||
fun clearQueue() {
|
||||
while (hasPendingRequests()) requestChannel.tryReceive()
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
fun hasPendingRequests() = !requestChannel.isEmpty
|
||||
}
|
||||
Loading…
Reference in New Issue