Correctly handling Atomic Boolean

This commit is contained in:
Vitor Pamplona
2023-02-06 18:15:37 -05:00
parent d252297ba4
commit 12c330f8f6
3 changed files with 20 additions and 20 deletions

View File

@@ -13,6 +13,7 @@ import com.vitorpamplona.amethyst.service.model.ChannelCreateEvent
import com.vitorpamplona.amethyst.service.model.ChannelMetadataEvent import com.vitorpamplona.amethyst.service.model.ChannelMetadataEvent
import com.vitorpamplona.amethyst.service.model.ReactionEvent import com.vitorpamplona.amethyst.service.model.ReactionEvent
import com.vitorpamplona.amethyst.service.model.RepostEvent import com.vitorpamplona.amethyst.service.model.RepostEvent
import java.util.concurrent.atomic.AtomicBoolean
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
@@ -99,19 +100,18 @@ class CardFeedViewModel(val dataSource: NostrDataSource<Note>): ViewModel() {
} }
} }
var handlerWaiting = AtomicBoolean()
var handlerWaiting = false @Synchronized
fun invalidateData() { private fun invalidateData() {
synchronized(handlerWaiting) { if (handlerWaiting.getAndSet(true)) return
if (handlerWaiting) return
handlerWaiting = true handlerWaiting.set(true)
val scope = CoroutineScope(Job() + Dispatchers.Default) val scope = CoroutineScope(Job() + Dispatchers.Default)
scope.launch { scope.launch {
delay(100) delay(100)
refresh() refresh()
handlerWaiting = false handlerWaiting.set(false)
}
} }
} }

View File

@@ -117,7 +117,7 @@ abstract class FeedViewModel(val dataSource: NostrDataSource<Note>): ViewModel()
private var handlerWaiting = AtomicBoolean() private var handlerWaiting = AtomicBoolean()
@Synchronized @Synchronized
private fun invalidateData() { private fun invalidateData() {
if (handlerWaiting.get()) return if (handlerWaiting.getAndSet(true)) return
handlerWaiting.set(true) handlerWaiting.set(true)
val scope = CoroutineScope(Job() + Dispatchers.Default) val scope = CoroutineScope(Job() + Dispatchers.Default)

View File

@@ -81,7 +81,7 @@ open class UserFeedViewModel(val dataSource: NostrDataSource<User>): ViewModel()
@Synchronized @Synchronized
private fun invalidateData() { private fun invalidateData() {
if (handlerWaiting.get()) return if (handlerWaiting.getAndSet(true)) return
handlerWaiting.set(true) handlerWaiting.set(true)
val scope = CoroutineScope(Job() + Dispatchers.Default) val scope = CoroutineScope(Job() + Dispatchers.Default)