From ad7094ce4fed6c0f02e565dfe212915324f6f69a Mon Sep 17 00:00:00 2001 From: TobiGr Date: Tue, 3 Nov 2026 15:14:12 +0100 Subject: [PATCH] Limit live chat to 200 messages to prevent lags --- .../schabi/newpipe/viewmodels/LiveChatViewModel.kt | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/viewmodels/LiveChatViewModel.kt b/app/src/main/java/org/schabi/newpipe/viewmodels/LiveChatViewModel.kt index d78ad8549..e6a7d35da 100644 --- a/app/src/main/java/org/schabi/newpipe/viewmodels/LiveChatViewModel.kt +++ b/app/src/main/java/org/schabi/newpipe/viewmodels/LiveChatViewModel.kt @@ -40,14 +40,21 @@ class LiveChatViewModel(savedStateHandle: SavedStateHandle) : ViewModel() { val extractor = service.getCommentsExtractor(url) extractor.setLiveChatContinuation(liveChatContinuation) val info = CommentsInfo.getInfo(extractor) - _liveChatItems.value = info.relatedItems + _liveChatItems.value = info.relatedItems.take(MAX_LIVE_CHAT_ITEMS) var nextPage = info.nextPage while (isActive) { delay(3.seconds) if (nextPage == null) continue val result = CommentsInfo.getMoreItems(service, url, nextPage) if (result.items.isNotEmpty()) { - _liveChatItems.value = result.items + _liveChatItems.value + _liveChatItems.value = buildList(MAX_LIVE_CHAT_ITEMS) { + addAll(result.items.take(MAX_LIVE_CHAT_ITEMS)) + if (size < MAX_LIVE_CHAT_ITEMS) { + addAll( + _liveChatItems.value.take(MAX_LIVE_CHAT_ITEMS - size) + ) + } + } } nextPage = result.nextPage } @@ -59,6 +66,7 @@ class LiveChatViewModel(savedStateHandle: SavedStateHandle) : ViewModel() { } companion object { + private const val MAX_LIVE_CHAT_ITEMS = 200 const val KEY_LIVE_CHAT_CONTINUATION = "live_chat_continuation" } }