Limit live chat to 200 messages to prevent lags

This commit is contained in:
TobiGr authored and Tobi committed 2026-11-03 23:04:48 +01:00
1 parent 77739f0597
commit ad7094ce4f
1 file changed
+10 -2
@@ -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"
}
}