mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-22 16:23:50 +00:00
now SmartMutex and SmartSemaphore (and SmartRWLocker as a result) works on SpecialMutableStateFlow
This commit is contained in:
parent
4901a8844c
commit
e75125f6df
@ -44,7 +44,7 @@ sealed interface SmartMutex {
|
|||||||
* @param locked Preset state of [isLocked] and its internal [_lockStateFlow]
|
* @param locked Preset state of [isLocked] and its internal [_lockStateFlow]
|
||||||
*/
|
*/
|
||||||
class Mutable(locked: Boolean = false) : SmartMutex {
|
class Mutable(locked: Boolean = false) : SmartMutex {
|
||||||
private val _lockStateFlow = MutableStateFlow<Boolean>(locked)
|
private val _lockStateFlow = SpecialMutableStateFlow<Boolean>(locked)
|
||||||
override val lockStateFlow: StateFlow<Boolean> = _lockStateFlow.asStateFlow()
|
override val lockStateFlow: StateFlow<Boolean> = _lockStateFlow.asStateFlow()
|
||||||
|
|
||||||
private val internalChangesMutex = Mutex()
|
private val internalChangesMutex = Mutex()
|
||||||
|
@ -45,7 +45,7 @@ sealed interface SmartSemaphore {
|
|||||||
* @param locked Preset state of [freePermits] and its internal [_freePermitsStateFlow]
|
* @param locked Preset state of [freePermits] and its internal [_freePermitsStateFlow]
|
||||||
*/
|
*/
|
||||||
class Mutable(private val permits: Int, acquiredPermits: Int = 0) : SmartSemaphore {
|
class Mutable(private val permits: Int, acquiredPermits: Int = 0) : SmartSemaphore {
|
||||||
private val _freePermitsStateFlow = MutableStateFlow<Int>(permits - acquiredPermits)
|
private val _freePermitsStateFlow = SpecialMutableStateFlow<Int>(permits - acquiredPermits)
|
||||||
override val permitsStateFlow: StateFlow<Int> = _freePermitsStateFlow.asStateFlow()
|
override val permitsStateFlow: StateFlow<Int> = _freePermitsStateFlow.asStateFlow()
|
||||||
|
|
||||||
private val internalChangesMutex = Mutex(false)
|
private val internalChangesMutex = Mutex(false)
|
||||||
|
@ -44,6 +44,10 @@ open class SpecialMutableStateFlow<T>(
|
|||||||
override val subscriptionCount: StateFlow<Int>
|
override val subscriptionCount: StateFlow<Int>
|
||||||
get() = sharingFlow.subscriptionCount
|
get() = sharingFlow.subscriptionCount
|
||||||
|
|
||||||
|
init {
|
||||||
|
sharingFlow.tryEmit(initialValue)
|
||||||
|
}
|
||||||
|
|
||||||
override fun compareAndSet(expect: T, update: T): Boolean {
|
override fun compareAndSet(expect: T, update: T): Boolean {
|
||||||
if (expect == value) {
|
if (expect == value) {
|
||||||
value = update
|
value = update
|
||||||
|
Loading…
Reference in New Issue
Block a user