From 56be14d4e0ee4c35404bbead55fc3ca6a5b66f67 Mon Sep 17 00:00:00 2001 From: y9san9 Date: Sun, 18 Aug 2024 20:41:27 +0300 Subject: [PATCH] feat: Added `bind` function and type arguments removed for AQueue --- README.md | 4 +++- .../kotlin/me/y9san9/aqueue/AQueue.kt | 21 +++++++++++++++---- .../kotlin/me/y9san9/aqueue/LinkedAQueue.kt | 8 ++++--- .../kotlin/me/y9san9/aqueue/flow/AQueue.kt | 2 +- .../jvmMain/kotlin/me/y9san9/aqueue/AQueue.kt | 4 ++-- example/build.gradle.kts | 2 +- gradle/libs.versions.toml | 2 +- 7 files changed, 30 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 8982830..9ea2622 100644 --- a/README.md +++ b/README.md @@ -15,9 +15,11 @@ I am happy to review pull requests, but I don't plan any further development. ## Install +Replace $version with the latest version from `Releases` Tab. + ```kotlin dependencies { - implementation("me.y9san9.aqueue:core:1.0.0") + implementation("me.y9san9.aqueue:core:$version") } ``` diff --git a/core/src/commonMain/kotlin/me/y9san9/aqueue/AQueue.kt b/core/src/commonMain/kotlin/me/y9san9/aqueue/AQueue.kt index 9eee0f3..e0adf77 100644 --- a/core/src/commonMain/kotlin/me/y9san9/aqueue/AQueue.kt +++ b/core/src/commonMain/kotlin/me/y9san9/aqueue/AQueue.kt @@ -6,7 +6,7 @@ import kotlin.coroutines.EmptyCoroutineContext /** * Asynchronous Queue with fine-grained control over concurrency */ -public interface AQueue { +public interface AQueue { /** * Executes [request] with fine-grained control over concurrency @@ -16,13 +16,26 @@ public interface AQueue { * @param context The context that is used to launch new coroutines. You may limit parallelism using context * @param action The action to perform with [request] */ - public suspend fun execute( + public suspend fun execute( request: TRequest, key: Any? = null, context: CoroutineContext = EmptyCoroutineContext, action: suspend (TRequest) -> TResponse ): TResponse + /** + * Creates an Asynchronous Queue that has all parameters provided except + * of the request itself. + */ + public fun bind( + key: (TRequest) -> Any? = { null }, + context: CoroutineContext = EmptyCoroutineContext, + queue: AQueue = AQueue(), + action: suspend (TRequest) -> TResponse, + ): Bound { + return Bound(key, context, queue, action) + } + /** * Asynchronous Queue that has all parameters provided except * of the request itself. @@ -35,7 +48,7 @@ public interface AQueue { public class Bound( private val key: (TRequest) -> Any? = { null }, private val context: CoroutineContext = EmptyCoroutineContext, - private val queue: AQueue = AQueue(), + private val queue: AQueue = AQueue(), private val action: suspend (TRequest) -> TResponse, ) { /** @@ -52,7 +65,7 @@ public interface AQueue { public fun copy( key: (TRequest) -> Any? = this.key, context: CoroutineContext = this.context, - queue: AQueue = this.queue, + queue: AQueue = this.queue, action: suspend (TRequest) -> TResponse = this.action, ): Bound { return Bound(key, context, queue, action) diff --git a/core/src/commonMain/kotlin/me/y9san9/aqueue/LinkedAQueue.kt b/core/src/commonMain/kotlin/me/y9san9/aqueue/LinkedAQueue.kt index 10e43dd..15bfeb5 100644 --- a/core/src/commonMain/kotlin/me/y9san9/aqueue/LinkedAQueue.kt +++ b/core/src/commonMain/kotlin/me/y9san9/aqueue/LinkedAQueue.kt @@ -4,11 +4,13 @@ import kotlinx.coroutines.* import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock import kotlin.coroutines.CoroutineContext +import kotlin.js.JsName /** * Creates a default implementation of [AQueue] */ -public fun AQueue(): AQueue { +@JsName("aQueue") +public fun AQueue(): AQueue { return LinkedAQueue() } @@ -17,7 +19,7 @@ public fun AQueue(): AQueue { * It is kind of like LinkedList works, because every job saves reference * to the previous job. */ -public class LinkedAQueue : AQueue { +public class LinkedAQueue : AQueue { private val pendingMap = PendingMap() /** @@ -28,7 +30,7 @@ public class LinkedAQueue : AQueue { * @param context The context that is used to launch new coroutines. You may limit parallelism using context * @param action The action to perform with [request] */ - override suspend fun execute( + override suspend fun execute( request: TRequest, key: Any?, context: CoroutineContext, diff --git a/core/src/commonMain/kotlin/me/y9san9/aqueue/flow/AQueue.kt b/core/src/commonMain/kotlin/me/y9san9/aqueue/flow/AQueue.kt index 803fd21..031bec4 100644 --- a/core/src/commonMain/kotlin/me/y9san9/aqueue/flow/AQueue.kt +++ b/core/src/commonMain/kotlin/me/y9san9/aqueue/flow/AQueue.kt @@ -37,7 +37,7 @@ public fun Flow.mapInAQueue( public fun Flow.mapInAQueue( key: (TRequest) -> Any? = { null }, context: CoroutineContext = EmptyCoroutineContext, - queue: AQueue = AQueue(), + queue: AQueue = AQueue(), action: suspend (TRequest) -> TResponse, ): Flow { val bound = AQueue.Bound(key, context, queue, action) diff --git a/core/src/jvmMain/kotlin/me/y9san9/aqueue/AQueue.kt b/core/src/jvmMain/kotlin/me/y9san9/aqueue/AQueue.kt index 141b9f1..a4b71b5 100644 --- a/core/src/jvmMain/kotlin/me/y9san9/aqueue/AQueue.kt +++ b/core/src/jvmMain/kotlin/me/y9san9/aqueue/AQueue.kt @@ -22,7 +22,7 @@ public fun AQueue.Bound.Companion.fixedThreadPool( name: String, key: (TRequest) -> Any? = { null }, context: CoroutineContext = EmptyCoroutineContext, - queue: AQueue = AQueue(), + queue: AQueue = AQueue(), action: suspend (TRequest) -> TResponse, ): AQueue.Bound { val fixedContext = newFixedThreadPoolContext(numberOfThreads, name) @@ -47,7 +47,7 @@ public fun AQueue.Bound.Companion.fixedThreadPool( public fun AQueue.Bound.Companion.io( key: (TRequest) -> Any? = { null }, context: CoroutineContext = EmptyCoroutineContext, - queue: AQueue = AQueue(), + queue: AQueue = AQueue(), action: suspend (TRequest) -> TResponse, ): AQueue.Bound { return AQueue.Bound( diff --git a/example/build.gradle.kts b/example/build.gradle.kts index e9541f2..0e024b5 100644 --- a/example/build.gradle.kts +++ b/example/build.gradle.kts @@ -4,5 +4,5 @@ plugins { dependencies { implementation(libs.coroutines) - implementation(projects.aqueue) + implementation(projects.core) } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index f9f1ba6..dc1ec3f 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -4,7 +4,7 @@ kotlin = "2.0.0" coroutines = "1.8.0" maven-publish = "0.29.0" -aqueue = "1.0.0" +aqueue = "1.0.1" [libraries]