mirror of
https://github.com/recloudstream/cloudstream.git
synced 2024-08-15 01:53:11 +00:00
Update Event.kt
This commit is contained in:
parent
dce83437f1
commit
835387b244
1 changed files with 9 additions and 13 deletions
|
@ -1,30 +1,26 @@
|
||||||
package com.lagradost.cloudstream3.utils
|
package com.lagradost.cloudstream3.utils
|
||||||
|
|
||||||
import com.lagradost.cloudstream3.utils.Coroutines.ioSafe
|
|
||||||
import kotlinx.coroutines.sync.Mutex
|
|
||||||
import kotlinx.coroutines.sync.withLock
|
|
||||||
|
|
||||||
class Event<T> {
|
class Event<T> {
|
||||||
private val observers = mutableSetOf<(T) -> Unit>()
|
private val observers = mutableSetOf<(T) -> Unit>()
|
||||||
|
|
||||||
val size: Int get() = observers.size
|
val size: Int get() = observers.size
|
||||||
|
|
||||||
operator fun plusAssign(observer: (T) -> Unit) {
|
operator fun plusAssign(observer: (T) -> Unit) {
|
||||||
|
synchronized(observers) {
|
||||||
observers.add(observer)
|
observers.add(observer)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
operator fun minusAssign(observer: (T) -> Unit) {
|
operator fun minusAssign(observer: (T) -> Unit) {
|
||||||
|
synchronized(observers) {
|
||||||
observers.remove(observer)
|
observers.remove(observer)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
private val invokeMutex = Mutex()
|
|
||||||
|
|
||||||
operator fun invoke(value: T) {
|
operator fun invoke(value: T) {
|
||||||
ioSafe {
|
synchronized(observers) {
|
||||||
invokeMutex.withLock { // Can crash otherwise
|
|
||||||
for (observer in observers)
|
for (observer in observers)
|
||||||
observer(value)
|
observer(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue