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
5dfc08aabb
commit
dce83437f1
1 changed files with 14 additions and 4 deletions
|
@ -1,9 +1,13 @@
|
|||
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> {
|
||||
private val observers = mutableSetOf<(T) -> Unit>()
|
||||
|
||||
val size : Int get() = observers.size
|
||||
val size: Int get() = observers.size
|
||||
|
||||
operator fun plusAssign(observer: (T) -> Unit) {
|
||||
observers.add(observer)
|
||||
|
@ -13,8 +17,14 @@ class Event<T> {
|
|||
observers.remove(observer)
|
||||
}
|
||||
|
||||
private val invokeMutex = Mutex()
|
||||
|
||||
operator fun invoke(value: T) {
|
||||
for (observer in observers)
|
||||
observer(value)
|
||||
ioSafe {
|
||||
invokeMutex.withLock { // Can crash otherwise
|
||||
for (observer in observers)
|
||||
observer(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue