Moves LocalCache's Synchronized block to Concurrent implementations

This commit is contained in:
Vitor Pamplona
2023-06-25 12:40:56 -04:00
parent 54a95e6a4e
commit 383e761372
@@ -5,7 +5,6 @@ import androidx.compose.runtime.Stable
import com.vitorpamplona.amethyst.Amethyst import com.vitorpamplona.amethyst.Amethyst
import com.vitorpamplona.amethyst.service.checkNotInMainThread import com.vitorpamplona.amethyst.service.checkNotInMainThread
import com.vitorpamplona.amethyst.service.model.* import com.vitorpamplona.amethyst.service.model.*
import com.vitorpamplona.amethyst.service.model.ATag.Companion.isATag
import com.vitorpamplona.amethyst.service.nip19.Nip19 import com.vitorpamplona.amethyst.service.nip19.Nip19
import com.vitorpamplona.amethyst.service.relays.Relay import com.vitorpamplona.amethyst.service.relays.Relay
import com.vitorpamplona.amethyst.ui.components.BundledInsert import com.vitorpamplona.amethyst.ui.components.BundledInsert
@@ -42,14 +41,12 @@ object LocalCache {
return null return null
} }
@Synchronized
fun getOrCreateUser(key: HexKey): User { fun getOrCreateUser(key: HexKey): User {
// checkNotInMainThread() // checkNotInMainThread()
return users[key] ?: run { return users[key] ?: run {
val answer = User(key) val newObject = User(key)
users.put(key, answer) users.putIfAbsent(key, newObject) ?: newObject
answer
} }
} }
@@ -84,14 +81,13 @@ object LocalCache {
return null return null
} }
@Synchronized
fun getOrCreateNote(idHex: String): Note { fun getOrCreateNote(idHex: String): Note {
checkNotInMainThread() checkNotInMainThread()
return notes[idHex] ?: run { return notes.get(idHex) ?: run {
val answer = Note(idHex) val newObject = Note(idHex)
notes.put(idHex, answer) notes.putIfAbsent(idHex, newObject) ?: newObject
answer newObject
} }
} }
@@ -124,14 +120,12 @@ object LocalCache {
} }
} }
@Synchronized
fun getOrCreateChannel(key: String, channelFactory: (String) -> Channel): Channel { fun getOrCreateChannel(key: String, channelFactory: (String) -> Channel): Channel {
checkNotInMainThread() checkNotInMainThread()
return channels[key] ?: run { return channels[key] ?: run {
val answer = channelFactory(key) val newObject = channelFactory(key)
channels.put(key, answer) channels.putIfAbsent(key, newObject) ?: newObject
answer
} }
} }
@@ -149,16 +143,14 @@ object LocalCache {
} }
} }
@Synchronized
fun getOrCreateAddressableNoteInternal(key: ATag): AddressableNote { fun getOrCreateAddressableNoteInternal(key: ATag): AddressableNote {
checkNotInMainThread() checkNotInMainThread()
// we can't use naddr here because naddr might include relay info and // we can't use naddr here because naddr might include relay info and
// the preferred relay should not be part of the index. // the preferred relay should not be part of the index.
return addressables[key.toTag()] ?: run { return addressables[key.toTag()] ?: run {
val answer = AddressableNote(key) val newObject = AddressableNote(key)
addressables.put(key.toTag(), answer) addressables.putIfAbsent(key.toTag(), newObject) ?: newObject
answer
} }
} }
@@ -547,8 +539,7 @@ object LocalCache {
// Log.d("PM", "${author.toBestDisplayName()} to ${recipient?.toBestDisplayName()}") // Log.d("PM", "${author.toBestDisplayName()} to ${recipient?.toBestDisplayName()}")
val repliesTo = event.tags.filter { it.firstOrNull() == "e" }.mapNotNull { it.getOrNull(1) } val repliesTo = event.taggedEvents().mapNotNull { checkGetOrCreateNote(it) }
.mapNotNull { checkGetOrCreateNote(it) }
note.loadEvent(event, author, repliesTo) note.loadEvent(event, author, repliesTo)