Activating the Atomic Boolean instead of Synchronized sections

This commit is contained in:
Vitor Pamplona
2023-02-28 20:10:52 -05:00
parent df43e730ca
commit b6f5cc9ae5
13 changed files with 11 additions and 27 deletions
@@ -44,12 +44,10 @@ class NotificationLiveData(val cache: NotificationCache): LiveData<NotificationS
// Refreshes observers in batches. // Refreshes observers in batches.
var handlerWaiting = AtomicBoolean() var handlerWaiting = AtomicBoolean()
@Synchronized
fun invalidateData() { fun invalidateData() {
if (!hasActiveObservers()) return if (!hasActiveObservers()) return
if (handlerWaiting.getAndSet(true)) return if (handlerWaiting.getAndSet(true)) return
handlerWaiting.set(true)
val scope = CoroutineScope(Job() + Dispatchers.Main) val scope = CoroutineScope(Job() + Dispatchers.Main)
scope.launch { scope.launch {
try { try {
@@ -605,11 +605,9 @@ class Account(
class AccountLiveData(private val account: Account): LiveData<AccountState>(AccountState(account)) { class AccountLiveData(private val account: Account): LiveData<AccountState>(AccountState(account)) {
var handlerWaiting = AtomicBoolean() var handlerWaiting = AtomicBoolean()
@Synchronized
fun invalidateData() { fun invalidateData() {
if (handlerWaiting.getAndSet(true)) return if (handlerWaiting.getAndSet(true)) return
handlerWaiting.set(true)
val scope = CoroutineScope(Job() + Dispatchers.Default) val scope = CoroutineScope(Job() + Dispatchers.Default)
scope.launch { scope.launch {
try { try {
@@ -67,12 +67,10 @@ class AntiSpamLiveData(val cache: AntiSpamFilter): LiveData<AntiSpamState>(AntiS
// Refreshes observers in batches. // Refreshes observers in batches.
var handlerWaiting = AtomicBoolean() var handlerWaiting = AtomicBoolean()
@Synchronized
fun invalidateData() { fun invalidateData() {
if (!hasActiveObservers()) return if (!hasActiveObservers()) return
if (handlerWaiting.getAndSet(true)) return if (handlerWaiting.getAndSet(true)) return
handlerWaiting.set(true)
val scope = CoroutineScope(Job() + Dispatchers.Main) val scope = CoroutineScope(Job() + Dispatchers.Main)
scope.launch { scope.launch {
try { try {
@@ -23,7 +23,6 @@ class Channel(val idHex: String) {
return info.name ?: idDisplayNote() return info.name ?: idDisplayNote()
} }
@Synchronized
fun addNote(note: Note) { fun addNote(note: Note) {
notes[note.idHex] = note notes[note.idHex] = note
} }
@@ -735,12 +735,10 @@ class LocalCacheLiveData(val cache: LocalCache): LiveData<LocalCacheState>(Local
// Refreshes observers in batches. // Refreshes observers in batches.
var handlerWaiting = AtomicBoolean() var handlerWaiting = AtomicBoolean()
@Synchronized
fun invalidateData() { fun invalidateData() {
if (!hasActiveObservers()) return if (!hasActiveObservers()) return
if (handlerWaiting.getAndSet(true)) return if (handlerWaiting.getAndSet(true)) return
handlerWaiting.set(true)
val scope = CoroutineScope(Job() + Dispatchers.Main) val scope = CoroutineScope(Job() + Dispatchers.Main)
scope.launch { scope.launch {
try { try {
@@ -18,8 +18,10 @@ import java.util.regex.Pattern
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import nostr.postr.events.Event import nostr.postr.events.Event
val tagSearch = Pattern.compile("(?:\\s|\\A)\\#\\[([0-9]+)\\]") val tagSearch = Pattern.compile("(?:\\s|\\A)\\#\\[([0-9]+)\\]")
@@ -318,19 +320,22 @@ class NoteLiveData(val note: Note): LiveData<NoteState>(NoteState(note)) {
// Refreshes observers in batches. // Refreshes observers in batches.
var handlerWaiting = AtomicBoolean() var handlerWaiting = AtomicBoolean()
@Synchronized
fun invalidateData() { fun invalidateData() {
if (!hasActiveObservers()) return if (!hasActiveObservers()) return
if (handlerWaiting.getAndSet(true)) return if (handlerWaiting.getAndSet(true)) return
handlerWaiting.set(true)
val scope = CoroutineScope(Job() + Dispatchers.Main) val scope = CoroutineScope(Job() + Dispatchers.Main)
scope.launch { scope.launch {
try {
delay(100) delay(100)
refresh() refresh()
} finally {
withContext(NonCancellable) {
handlerWaiting.set(false) handlerWaiting.set(false)
} }
} }
}
}
fun refresh() { fun refresh() {
postValue(NoteState(note)) postValue(NoteState(note))
@@ -379,11 +379,9 @@ class UserLiveData(val user: User): LiveData<UserState>(UserState(user)) {
// Refreshes observers in batches. // Refreshes observers in batches.
var handlerWaiting = AtomicBoolean() var handlerWaiting = AtomicBoolean()
@Synchronized
fun invalidateData() { fun invalidateData() {
if (handlerWaiting.getAndSet(true)) return if (handlerWaiting.getAndSet(true)) return
handlerWaiting.set(true)
val scope = CoroutineScope(Job() + Dispatchers.Main) val scope = CoroutineScope(Job() + Dispatchers.Main)
scope.launch { scope.launch {
try { try {
@@ -94,7 +94,6 @@ object RelayPool: Relay.Listener {
fun onSendResponse(eventId: String, success: Boolean, message: String, relay: Relay) fun onSendResponse(eventId: String, success: Boolean, message: String, relay: Relay)
} }
@Synchronized
override fun onEvent(relay: Relay, subscriptionId: String, event: Event) { override fun onEvent(relay: Relay, subscriptionId: String, event: Event) {
listeners.forEach { it.onEvent(event, subscriptionId, relay) } listeners.forEach { it.onEvent(event, subscriptionId, relay) }
} }
@@ -129,11 +129,9 @@ open class CardFeedViewModel(val dataSource: FeedFilter<Note>): ViewModel() {
var handlerWaiting = AtomicBoolean() var handlerWaiting = AtomicBoolean()
@Synchronized
private fun invalidateData() { private fun invalidateData() {
if (handlerWaiting.getAndSet(true)) return if (handlerWaiting.getAndSet(true)) return
handlerWaiting.set(true)
val scope = CoroutineScope(Job() + Dispatchers.Default) val scope = CoroutineScope(Job() + Dispatchers.Default)
scope.launch { scope.launch {
try { try {
@@ -87,11 +87,10 @@ abstract class FeedViewModel(val localFilter: FeedFilter<Note>): ViewModel() {
} }
private var handlerWaiting = AtomicBoolean() private var handlerWaiting = AtomicBoolean()
@Synchronized
fun invalidateData() { fun invalidateData() {
if (handlerWaiting.getAndSet(true)) return if (handlerWaiting.getAndSet(true)) return
handlerWaiting.set(true)
val scope = CoroutineScope(Job() + Dispatchers.Default) val scope = CoroutineScope(Job() + Dispatchers.Default)
scope.launch { scope.launch {
try { try {
@@ -61,11 +61,9 @@ open class LnZapFeedViewModel(val dataSource: FeedFilter<Pair<Note, Note>>): Vie
var handlerWaiting = AtomicBoolean() var handlerWaiting = AtomicBoolean()
@Synchronized
private fun invalidateData() { private fun invalidateData() {
if (handlerWaiting.getAndSet(true)) return if (handlerWaiting.getAndSet(true)) return
handlerWaiting.set(true)
val scope = CoroutineScope(Job() + Dispatchers.Default) val scope = CoroutineScope(Job() + Dispatchers.Default)
scope.launch { scope.launch {
try { try {
@@ -83,11 +83,9 @@ class RelayFeedViewModel: ViewModel() {
var handlerWaiting = AtomicBoolean() var handlerWaiting = AtomicBoolean()
@Synchronized
private fun invalidateData() { private fun invalidateData() {
if (handlerWaiting.getAndSet(true)) return if (handlerWaiting.getAndSet(true)) return
handlerWaiting.set(true)
val scope = CoroutineScope(Job() + Dispatchers.Default) val scope = CoroutineScope(Job() + Dispatchers.Default)
scope.launch { scope.launch {
try { try {
@@ -65,11 +65,9 @@ open class UserFeedViewModel(val dataSource: FeedFilter<User>): ViewModel() {
var handlerWaiting = AtomicBoolean() var handlerWaiting = AtomicBoolean()
@Synchronized
private fun invalidateData() { private fun invalidateData() {
if (handlerWaiting.getAndSet(true)) return if (handlerWaiting.getAndSet(true)) return
handlerWaiting.set(true)
val scope = CoroutineScope(Job() + Dispatchers.Default) val scope = CoroutineScope(Job() + Dispatchers.Default)
scope.launch { scope.launch {
try { try {