Skip to content

Commit

Permalink
feat: Added bind function and type arguments removed for AQueue
Browse files Browse the repository at this point in the history
  • Loading branch information
y9san9 committed Aug 18, 2024
1 parent ae11342 commit 56be14d
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 13 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
```

Expand Down
21 changes: 17 additions & 4 deletions core/src/commonMain/kotlin/me/y9san9/aqueue/AQueue.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import kotlin.coroutines.EmptyCoroutineContext
/**
* Asynchronous Queue with fine-grained control over concurrency
*/
public interface AQueue<TRequest, TResponse> {
public interface AQueue {

/**
* Executes [request] with fine-grained control over concurrency
Expand All @@ -16,13 +16,26 @@ public interface AQueue<TRequest, TResponse> {
* @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 <TRequest, TResponse> 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 <TRequest, TResponse> bind(
key: (TRequest) -> Any? = { null },
context: CoroutineContext = EmptyCoroutineContext,
queue: AQueue = AQueue(),
action: suspend (TRequest) -> TResponse,
): Bound<TRequest, TResponse> {
return Bound(key, context, queue, action)
}

/**
* Asynchronous Queue that has all parameters provided except
* of the request itself.
Expand All @@ -35,7 +48,7 @@ public interface AQueue<TRequest, TResponse> {
public class Bound<TRequest, TResponse>(
private val key: (TRequest) -> Any? = { null },
private val context: CoroutineContext = EmptyCoroutineContext,
private val queue: AQueue<TRequest, TResponse> = AQueue(),
private val queue: AQueue = AQueue(),
private val action: suspend (TRequest) -> TResponse,
) {
/**
Expand All @@ -52,7 +65,7 @@ public interface AQueue<TRequest, TResponse> {
public fun copy(
key: (TRequest) -> Any? = this.key,
context: CoroutineContext = this.context,
queue: AQueue<TRequest, TResponse> = this.queue,
queue: AQueue = this.queue,
action: suspend (TRequest) -> TResponse = this.action,
): Bound<TRequest, TResponse> {
return Bound(key, context, queue, action)
Expand Down
8 changes: 5 additions & 3 deletions core/src/commonMain/kotlin/me/y9san9/aqueue/LinkedAQueue.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 <TRequest, TResponse> AQueue(): AQueue<TRequest, TResponse> {
@JsName("aQueue")
public fun AQueue(): AQueue {
return LinkedAQueue()
}

Expand All @@ -17,7 +19,7 @@ public fun <TRequest, TResponse> AQueue(): AQueue<TRequest, TResponse> {
* It is kind of like LinkedList works, because every job saves reference
* to the previous job.
*/
public class LinkedAQueue<TRequest, TResponse> : AQueue<TRequest, TResponse> {
public class LinkedAQueue : AQueue {
private val pendingMap = PendingMap()

/**
Expand All @@ -28,7 +30,7 @@ public class LinkedAQueue<TRequest, TResponse> : AQueue<TRequest, TResponse> {
* @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 <TRequest, TResponse> execute(
request: TRequest,
key: Any?,
context: CoroutineContext,
Expand Down
2 changes: 1 addition & 1 deletion core/src/commonMain/kotlin/me/y9san9/aqueue/flow/AQueue.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public fun <TRequest, TResponse> Flow<TRequest>.mapInAQueue(
public fun <TRequest, TResponse> Flow<TRequest>.mapInAQueue(
key: (TRequest) -> Any? = { null },
context: CoroutineContext = EmptyCoroutineContext,
queue: AQueue<TRequest, TResponse> = AQueue(),
queue: AQueue = AQueue(),
action: suspend (TRequest) -> TResponse,
): Flow<TResponse> {
val bound = AQueue.Bound(key, context, queue, action)
Expand Down
4 changes: 2 additions & 2 deletions core/src/jvmMain/kotlin/me/y9san9/aqueue/AQueue.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public fun <TRequest, TResponse> AQueue.Bound.Companion.fixedThreadPool(
name: String,
key: (TRequest) -> Any? = { null },
context: CoroutineContext = EmptyCoroutineContext,
queue: AQueue<TRequest, TResponse> = AQueue(),
queue: AQueue = AQueue(),
action: suspend (TRequest) -> TResponse,
): AQueue.Bound<TRequest, TResponse> {
val fixedContext = newFixedThreadPoolContext(numberOfThreads, name)
Expand All @@ -47,7 +47,7 @@ public fun <TRequest, TResponse> AQueue.Bound.Companion.fixedThreadPool(
public fun <TRequest, TResponse> AQueue.Bound.Companion.io(
key: (TRequest) -> Any? = { null },
context: CoroutineContext = EmptyCoroutineContext,
queue: AQueue<TRequest, TResponse> = AQueue(),
queue: AQueue = AQueue(),
action: suspend (TRequest) -> TResponse,
): AQueue.Bound<TRequest, TResponse> {
return AQueue.Bound(
Expand Down
2 changes: 1 addition & 1 deletion example/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ plugins {

dependencies {
implementation(libs.coroutines)
implementation(projects.aqueue)
implementation(projects.core)
}
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down

0 comments on commit 56be14d

Please sign in to comment.