Using short-term co-routines

This commit is contained in:
Vitor Pamplona 2023-01-19 17:52:32 -05:00
parent 85c66279b2
commit c1d46dcc2f
6 changed files with 21 additions and 14 deletions

View File

@ -100,13 +100,13 @@ class Note(val idHex: String) {
val live: NoteLiveData = NoteLiveData(this) val live: NoteLiveData = NoteLiveData(this)
// Refreshes observers in batches. // Refreshes observers in batches.
val scope = CoroutineScope(Job() + Dispatchers.Main)
var handlerWaiting = false var handlerWaiting = false
@Synchronized @Synchronized
fun invalidateData() { fun invalidateData() {
if (handlerWaiting) return if (handlerWaiting) return
handlerWaiting = true handlerWaiting = true
val scope = CoroutineScope(Job() + Dispatchers.Main)
scope.launch { scope.launch {
delay(100) delay(100)
live.refresh() live.refresh()

View File

@ -113,13 +113,13 @@ class User(val pubkey: ByteArray) {
val live: UserLiveData = UserLiveData(this) val live: UserLiveData = UserLiveData(this)
// Refreshes observers in batches. // Refreshes observers in batches.
val scope = CoroutineScope(Job() + Dispatchers.Main)
var handlerWaiting = false var handlerWaiting = false
@Synchronized @Synchronized
fun invalidateData() { fun invalidateData() {
if (handlerWaiting) return if (handlerWaiting) return
handlerWaiting = true handlerWaiting = true
val scope = CoroutineScope(Job() + Dispatchers.Main)
scope.launch { scope.launch {
delay(100) delay(100)
live.refresh() live.refresh()

View File

@ -148,21 +148,28 @@ abstract class NostrDataSource<T>(val debugName: String) {
channelIds.remove(channel.id) channelIds.remove(channel.id)
} }
val scope = CoroutineScope(Job() + Dispatchers.IO)
var handlerWaiting = false var handlerWaiting = false
@Synchronized @Synchronized
fun invalidateFilters() { fun invalidateFilters() {
if (handlerWaiting) return if (handlerWaiting) return
handlerWaiting = true handlerWaiting = true
val scope = CoroutineScope(Job() + Dispatchers.IO)
scope.launch { scope.launch {
delay(200) delay(200)
resetFilters() resetFiltersSuspend()
handlerWaiting = false handlerWaiting = false
} }
} }
fun resetFilters() { fun resetFilters() {
val scope = CoroutineScope(Job() + Dispatchers.IO)
scope.launch {
resetFiltersSuspend()
}
}
fun resetFiltersSuspend() {
// saves the channels that are currently active // saves the channels that are currently active
val activeChannels = channels.filter { it.filter != null } val activeChannels = channels.filter { it.filter != null }
// saves the current content to only update if it changes // saves the current content to only update if it changes

View File

@ -3,6 +3,10 @@ package com.vitorpamplona.amethyst.service.relays
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import com.vitorpamplona.amethyst.service.Constants import com.vitorpamplona.amethyst.service.Constants
import java.util.Collections import java.util.Collections
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import nostr.postr.events.Event import nostr.postr.events.Event
/** /**
@ -108,7 +112,10 @@ object RelayPool: Relay.Listener {
val live: RelayPoolLiveData = RelayPoolLiveData(this) val live: RelayPoolLiveData = RelayPoolLiveData(this)
private fun refreshObservers() { private fun refreshObservers() {
live.refresh() val scope = CoroutineScope(Job() + Dispatchers.Main)
scope.launch {
live.refresh()
}
} }
} }

View File

@ -91,13 +91,13 @@ class CardFeedViewModel(val dataSource: NostrDataSource<Note>): ViewModel() {
} }
} }
val scope = CoroutineScope(Job() + Dispatchers.IO)
var handlerWaiting = false var handlerWaiting = false
@Synchronized @Synchronized
fun invalidateData() { fun invalidateData() {
if (handlerWaiting) return if (handlerWaiting) return
handlerWaiting = true handlerWaiting = true
val scope = CoroutineScope(Job() + Dispatchers.IO)
scope.launch { scope.launch {
delay(100) delay(100)
refresh() refresh()

View File

@ -63,20 +63,13 @@ open class UserFeedViewModel(val dataSource: NostrDataSource<User>): ViewModel()
} }
} }
fun refreshCurrentList() {
val state = feedContent.value
if (state is UserFeedState.Loaded) {
_feedContent.update { UserFeedState.Loaded(state.feed) }
}
}
val scope = CoroutineScope(Job() + Dispatchers.IO)
var handlerWaiting = false var handlerWaiting = false
@Synchronized @Synchronized
fun invalidateData() { fun invalidateData() {
if (handlerWaiting) return if (handlerWaiting) return
handlerWaiting = true handlerWaiting = true
val scope = CoroutineScope(Job() + Dispatchers.IO)
scope.launch { scope.launch {
delay(100) delay(100)
refresh() refresh()