diff --git a/app/src/main/java/com/lagradost/cloudstream3/utils/Event.kt b/app/src/main/java/com/lagradost/cloudstream3/utils/Event.kt index 57a5f8e4..fc568d01 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/utils/Event.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/utils/Event.kt @@ -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 { 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 { 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) + } + } } -} \ No newline at end of file +}