Coverage Summary for Class: InitializeLock (cloud.mindbox.mobile_sdk)
| Class |
Method, %
|
Branch, %
|
Line, %
|
Instruction, %
|
| InitializeLock |
100%
(4/4)
|
66.7%
(4/6)
|
100%
(14/14)
|
97.8%
(89/91)
|
| InitializeLock$await$$inlined$sortedBy$1 |
0%
(0/1)
|
|
| InitializeLock$await$1 |
|
| InitializeLock$State |
100%
(1/1)
|
|
100%
(3/3)
|
100%
(24/24)
|
| Total |
83.3%
(5/6)
|
66.7%
(4/6)
|
100%
(17/17)
|
98.3%
(113/115)
|
package cloud.mindbox.mobile_sdk
import androidx.annotation.WorkerThread
import cloud.mindbox.mobile_sdk.logger.mindboxLogI
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.Job
internal object InitializeLock {
private val map: MutableMap<State, CompletableDeferred<Unit>> =
State.values()
.associateWith<State, CompletableDeferred<Unit>> { CompletableDeferred() }
.toMutableMap()
@WorkerThread
internal suspend fun await(state: State) {
State.values().filter { state >= it }
.sortedBy { it.ordinal }
.mapNotNull { map[it] }
.onEach {
it.await()
}
}
internal fun complete(state: State) {
map[state]?.complete(Unit)
mindboxLogI("State $state completed")
}
internal fun reset(state: State) {
map[state]?.complete(Unit)
map[state] = CompletableDeferred()
mindboxLogI("State $state is reset")
}
internal enum class State {
MIGRATION,
SAVE_MINDBOX_CONFIG,
APP_STARTED
}
}
internal fun Job.initState(state: InitializeLock.State): Job {
return this.apply {
invokeOnCompletion {
InitializeLock.complete(state)
}
}
}