ExclusiveCallbackHelper

class ExclusiveCallbackHelper<T>(coroutineScope: CoroutineScope, mutex: Mutex)

Helper for a re-occurring pattern where we need to suspend to wait for a result of an asynchronous call and during this wait no other call is allowed to be made.

The specified mutex defines the exclusivity of the instance.

Constructors

Link copied to clipboard
fun ExclusiveCallbackHelper(coroutineScope: CoroutineScope, mutex: Mutex)

Functions

Link copied to clipboard
suspend fun lockAndWaitForResult(block: suspend () -> Boolean): T?

Exclusively runs the specified block then suspends until resumeIfSet is called and returns the value that was passed to that. Behavior is undefined if resumeIfSet was called when block returns false.

Link copied to clipboard
suspend fun lockAndWaitForResultCancelable(block: suspend () -> Boolean): T?

Exclusively runs the specified block then suspends until resumeIfSet is called and returns the value that was passed to that. Behavior is undefined if resumeIfSet was called when block returns false.

Link copied to clipboard
fun resumeIfSet(result: T)

Resumes the suspended lockAndWaitForResult with the specified result value.