Moves coroutines of BundledUpdate and BundledInsert to a managed model.
This commit is contained in:
@@ -615,6 +615,7 @@ open class Note(val idHex: String) {
|
|||||||
|
|
||||||
fun clearLive() {
|
fun clearLive() {
|
||||||
if (liveSet != null && liveSet?.isInUse() == false) {
|
if (liveSet != null && liveSet?.isInUse() == false) {
|
||||||
|
liveSet?.destroy()
|
||||||
liveSet = null
|
liveSet = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -656,12 +657,26 @@ class NoteLiveSet(u: Note) {
|
|||||||
relays.hasObservers() ||
|
relays.hasObservers() ||
|
||||||
zaps.hasObservers()
|
zaps.hasObservers()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun destroy() {
|
||||||
|
metadata.destroy()
|
||||||
|
reactions.destroy()
|
||||||
|
boosts.destroy()
|
||||||
|
replies.destroy()
|
||||||
|
reports.destroy()
|
||||||
|
relays.destroy()
|
||||||
|
zaps.destroy()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class NoteLiveData(val note: Note) : LiveData<NoteState>(NoteState(note)) {
|
class NoteLiveData(val note: Note) : LiveData<NoteState>(NoteState(note)) {
|
||||||
// Refreshes observers in batches.
|
// Refreshes observers in batches.
|
||||||
private val bundler = BundledUpdate(500, Dispatchers.IO)
|
private val bundler = BundledUpdate(500, Dispatchers.IO)
|
||||||
|
|
||||||
|
fun destroy() {
|
||||||
|
bundler.cancel()
|
||||||
|
}
|
||||||
|
|
||||||
fun invalidateData() {
|
fun invalidateData() {
|
||||||
if (!hasObservers()) return
|
if (!hasObservers()) return
|
||||||
|
|
||||||
|
|||||||
@@ -372,6 +372,7 @@ class User(val pubkeyHex: String) {
|
|||||||
|
|
||||||
fun clearLive() {
|
fun clearLive() {
|
||||||
if (liveSet != null && liveSet?.isInUse() == false) {
|
if (liveSet != null && liveSet?.isInUse() == false) {
|
||||||
|
liveSet?.destroy()
|
||||||
liveSet = null
|
liveSet = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -400,6 +401,18 @@ class UserLiveSet(u: User) {
|
|||||||
zaps.hasObservers() ||
|
zaps.hasObservers() ||
|
||||||
bookmarks.hasObservers()
|
bookmarks.hasObservers()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun destroy() {
|
||||||
|
follows.destroy()
|
||||||
|
followers.destroy()
|
||||||
|
reports.destroy()
|
||||||
|
messages.destroy()
|
||||||
|
relays.destroy()
|
||||||
|
relayInfo.destroy()
|
||||||
|
metadata.destroy()
|
||||||
|
zaps.destroy()
|
||||||
|
bookmarks.destroy()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Immutable
|
@Immutable
|
||||||
@@ -470,6 +483,10 @@ class UserLiveData(val user: User) : LiveData<UserState>(UserState(user)) {
|
|||||||
// Refreshes observers in batches.
|
// Refreshes observers in batches.
|
||||||
private val bundler = BundledUpdate(500, Dispatchers.IO)
|
private val bundler = BundledUpdate(500, Dispatchers.IO)
|
||||||
|
|
||||||
|
fun destroy() {
|
||||||
|
bundler.cancel()
|
||||||
|
}
|
||||||
|
|
||||||
fun invalidateData() {
|
fun invalidateData() {
|
||||||
if (!hasObservers()) return
|
if (!hasObservers()) return
|
||||||
checkNotInMainThread()
|
checkNotInMainThread()
|
||||||
|
|||||||
@@ -76,6 +76,7 @@ abstract class NostrDataSource(val debugName: String) {
|
|||||||
fun destroy() {
|
fun destroy() {
|
||||||
stop()
|
stop()
|
||||||
Client.unsubscribe(clientListener)
|
Client.unsubscribe(clientListener)
|
||||||
|
bundler.cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
open fun start() {
|
open fun start() {
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ 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.NonCancellable
|
||||||
|
import kotlinx.coroutines.SupervisorJob
|
||||||
|
import kotlinx.coroutines.cancel
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
@@ -21,6 +23,8 @@ class BundledUpdate(
|
|||||||
val delay: Long,
|
val delay: Long,
|
||||||
val dispatcher: CoroutineDispatcher = Dispatchers.Default
|
val dispatcher: CoroutineDispatcher = Dispatchers.Default
|
||||||
) {
|
) {
|
||||||
|
val scope = CoroutineScope(dispatcher + SupervisorJob())
|
||||||
|
|
||||||
private var onlyOneInBlock = AtomicBoolean()
|
private var onlyOneInBlock = AtomicBoolean()
|
||||||
private var invalidatesAgain = false
|
private var invalidatesAgain = false
|
||||||
|
|
||||||
@@ -32,7 +36,6 @@ class BundledUpdate(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
val scope = CoroutineScope(Job() + dispatcher)
|
|
||||||
scope.launch {
|
scope.launch {
|
||||||
try {
|
try {
|
||||||
onUpdate()
|
onUpdate()
|
||||||
@@ -48,6 +51,10 @@ class BundledUpdate(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun cancel() {
|
||||||
|
scope.cancel()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -58,6 +65,8 @@ class BundledInsert<T>(
|
|||||||
val delay: Long,
|
val delay: Long,
|
||||||
val dispatcher: CoroutineDispatcher = Dispatchers.Default
|
val dispatcher: CoroutineDispatcher = Dispatchers.Default
|
||||||
) {
|
) {
|
||||||
|
val scope = CoroutineScope(dispatcher + SupervisorJob())
|
||||||
|
|
||||||
private var onlyOneInBlock = AtomicBoolean()
|
private var onlyOneInBlock = AtomicBoolean()
|
||||||
private var queue = LinkedBlockingQueue<T>()
|
private var queue = LinkedBlockingQueue<T>()
|
||||||
|
|
||||||
@@ -88,4 +97,8 @@ class BundledInsert<T>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun cancel() {
|
||||||
|
scope.cancel()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -326,6 +326,7 @@ class UserReactionsViewModel(val account: Account) : ViewModel() {
|
|||||||
|
|
||||||
override fun onCleared() {
|
override fun onCleared() {
|
||||||
collectorJob?.cancel()
|
collectorJob?.cancel()
|
||||||
|
bundlerInsert.cancel()
|
||||||
super.onCleared()
|
super.onCleared()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -353,6 +353,8 @@ open class CardFeedViewModel(val localFilter: FeedFilter<Note>) : ViewModel() {
|
|||||||
|
|
||||||
override fun onCleared() {
|
override fun onCleared() {
|
||||||
clear()
|
clear()
|
||||||
|
bundlerInsert.cancel()
|
||||||
|
bundler.cancel()
|
||||||
collectorJob?.cancel()
|
collectorJob?.cancel()
|
||||||
super.onCleared()
|
super.onCleared()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -340,6 +340,8 @@ abstract class FeedViewModel(val localFilter: FeedFilter<Note>) : ViewModel(), I
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onCleared() {
|
override fun onCleared() {
|
||||||
|
bundlerInsert.cancel()
|
||||||
|
bundler.cancel()
|
||||||
collectorJob?.cancel()
|
collectorJob?.cancel()
|
||||||
super.onCleared()
|
super.onCleared()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -96,6 +96,7 @@ open class LnZapFeedViewModel(val dataSource: FeedFilter<ZapReqResponse>) : View
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onCleared() {
|
override fun onCleared() {
|
||||||
|
bundler.cancel()
|
||||||
collectorJob?.cancel()
|
collectorJob?.cancel()
|
||||||
super.onCleared()
|
super.onCleared()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -91,6 +91,11 @@ class RelayFeedViewModel : ViewModel() {
|
|||||||
refreshSuspended()
|
refreshSuspended()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onCleared() {
|
||||||
|
bundler.cancel()
|
||||||
|
super.onCleared()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@OptIn(ExperimentalMaterialApi::class)
|
@OptIn(ExperimentalMaterialApi::class)
|
||||||
|
|||||||
@@ -125,6 +125,7 @@ open class UserFeedViewModel(val dataSource: FeedFilter<User>) : ViewModel(), In
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onCleared() {
|
override fun onCleared() {
|
||||||
|
bundler.cancel()
|
||||||
collectorJob?.cancel()
|
collectorJob?.cancel()
|
||||||
super.onCleared()
|
super.onCleared()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -195,6 +195,11 @@ class SearchBarViewModel(val account: Account) : ViewModel() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onCleared() {
|
||||||
|
bundler.cancel()
|
||||||
|
super.onCleared()
|
||||||
|
}
|
||||||
|
|
||||||
fun isSearchingFun() = searchValue.isNotBlank()
|
fun isSearchingFun() = searchValue.isNotBlank()
|
||||||
|
|
||||||
class Factory(val account: Account) : ViewModelProvider.Factory {
|
class Factory(val account: Account) : ViewModelProvider.Factory {
|
||||||
|
|||||||
Reference in New Issue
Block a user