Updated on 2026-08-14
This commit is contained in:
parent
64a40de825
commit
4a25e38ccc
12 changed files with 151 additions and 28 deletions
|
|
@ -46,14 +46,36 @@ abstract class Model : InstanceKeeper.Instance {
|
|||
progressFlow: MutableSharedFlow<Boolean>,
|
||||
dispatcher: CoroutineDispatcher = dispatchers.mainImmediate,
|
||||
crossinline block: suspend () -> Unit,
|
||||
): Job = resource(
|
||||
acquire = { progressFlow.emit(true) },
|
||||
release = { progressFlow.emit(false) },
|
||||
dispatcher = dispatcher,
|
||||
block = block,
|
||||
)
|
||||
|
||||
/**
|
||||
* Launches [block] in the model's scope and acquires a resource before executing the block and releases it after.
|
||||
*
|
||||
* @param acquire The block of code to acquire the resource.
|
||||
* @param release The block of code to release the resource.
|
||||
* @param dispatcher The [CoroutineDispatcher] to launch the coroutine. Default is [Dispatchers.Main.immediate].
|
||||
* @param block The block of code to execute.
|
||||
*
|
||||
* @return The [Job] of the launched coroutine.
|
||||
* */
|
||||
protected inline fun resource(
|
||||
crossinline acquire: suspend () -> Unit,
|
||||
crossinline release: suspend () -> Unit,
|
||||
dispatcher: CoroutineDispatcher = dispatchers.mainImmediate,
|
||||
crossinline block: suspend () -> Unit,
|
||||
): Job = modelScope.launch(dispatcher) {
|
||||
progressFlow.emit(value = true)
|
||||
acquire()
|
||||
|
||||
try {
|
||||
block()
|
||||
} finally {
|
||||
withContext(NonCancellable) {
|
||||
progressFlow.emit(value = false)
|
||||
release()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue