From 835387b2449bfcdb71b0fc13313f574e76ab7c7e Mon Sep 17 00:00:00 2001 From: CranberrySoup <142951702+CranberrySoup@users.noreply.github.com> Date: Thu, 18 Jan 2024 14:54:39 +0000 Subject: [PATCH] Update Event.kt --- .../com/lagradost/cloudstream3/utils/Event.kt | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) 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 fc568d01..a0dfe734 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/utils/Event.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/utils/Event.kt @@ -1,30 +1,26 @@ 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 operator fun plusAssign(observer: (T) -> Unit) { - observers.add(observer) + synchronized(observers) { + observers.add(observer) + } } operator fun minusAssign(observer: (T) -> Unit) { - observers.remove(observer) + synchronized(observers) { + observers.remove(observer) + } } - private val invokeMutex = Mutex() - operator fun invoke(value: T) { - ioSafe { - invokeMutex.withLock { // Can crash otherwise - for (observer in observers) - observer(value) - } + synchronized(observers) { + for (observer in observers) + observer(value) } } }