Skip to content

Commit

Permalink
Updated AtomicExecutor, set default value for task priority, added do…
Browse files Browse the repository at this point in the history
…cumentation

for the same
  • Loading branch information
sacOO7 committed Oct 18, 2024
1 parent e385614 commit 31ca6e3
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions chat-android/src/main/java/com/ably/chat/AtomicExecutor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,17 @@ class AtomicExecutor(private val scope: CoroutineScope) {
private var isRunning = false
private val tasks: PriorityQueue<Task> = PriorityQueue()

suspend fun <T : Any>execute(priority:Int, coroutineBlock: suspend CoroutineScope.() -> T) : Channel<Result<T>> {
/**
* @param priority Defines priority for the operation execution.
* Default=0, means operation will be performed immediately after ongoing operation.
* This can also be set to negative number if operation needs higher priority than existing ones.
* @param coroutineBlock Suspended function that needs to be executed mutually exclusive under given scope.
*/
suspend fun <T : Any>execute(priority:Int = 0, coroutineBlock: suspend CoroutineScope.() -> T) : Channel<Result<T>> {
// Size of resultChannel is set to 1 to keep while loop running.
// i.e. If caller doesn't explicitly receive on the channel, loop will be blocked.
val resultChannel = Channel<Result<Any>>(1)
tasks.add(Task(priority, coroutineBlock, resultChannel))

scope.launch {
if (!isRunning) {
isRunning = true
Expand Down

0 comments on commit 31ca6e3

Please sign in to comment.