From 31ca6e3cf5720150aa886697b28fbfed855be435 Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Fri, 18 Oct 2024 12:39:15 +0530 Subject: [PATCH] Updated AtomicExecutor, set default value for task priority, added documentation for the same --- .../src/main/java/com/ably/chat/AtomicExecutor.kt | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/chat-android/src/main/java/com/ably/chat/AtomicExecutor.kt b/chat-android/src/main/java/com/ably/chat/AtomicExecutor.kt index 24923e92..95757b67 100644 --- a/chat-android/src/main/java/com/ably/chat/AtomicExecutor.kt +++ b/chat-android/src/main/java/com/ably/chat/AtomicExecutor.kt @@ -24,12 +24,17 @@ class AtomicExecutor(private val scope: CoroutineScope) { private var isRunning = false private val tasks: PriorityQueue = PriorityQueue() - suspend fun execute(priority:Int, coroutineBlock: suspend CoroutineScope.() -> T) : Channel> { + /** + * @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 execute(priority:Int = 0, coroutineBlock: suspend CoroutineScope.() -> T) : Channel> { // 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>(1) tasks.add(Task(priority, coroutineBlock, resultChannel)) - scope.launch { if (!isRunning) { isRunning = true