aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorShpuld Shpludson <shp@cock.li>2021-02-22 15:01:05 +0000
committerShpuld Shpludson <shp@cock.li>2021-02-22 15:01:05 +0000
commit589ab6510c1effc909ee9f6b64cd7791d3f55ee7 (patch)
tree0018c6e6dc200dbe9c09f331b5af7c70873431d1 /src
parente8b8c3cc49171a7372bf49ee29930fd55c95ce98 (diff)
parent67f3532ac95b2a8740ccbde581e74ac65eb20a9a (diff)
Merge branch 'feat/keep-chat-perf-up' into 'develop'
Optimize chat perf in long run See merge request pleroma/pleroma-fe!1350
Diffstat (limited to 'src')
-rw-r--r--src/components/chat/chat.js8
-rw-r--r--src/modules/chats.js6
-rw-r--r--src/services/chat_service/chat_service.js17
3 files changed, 31 insertions, 0 deletions
diff --git a/src/components/chat/chat.js b/src/components/chat/chat.js
index e57fcb91..2780be75 100644
--- a/src/components/chat/chat.js
+++ b/src/components/chat/chat.js
@@ -234,6 +234,13 @@ const Chat = {
const scrollable = this.$refs.scrollable
return scrollable && scrollable.scrollTop <= 0
},
+ cullOlderCheck () {
+ window.setTimeout(() => {
+ if (this.bottomedOut(JUMP_TO_BOTTOM_BUTTON_VISIBILITY_OFFSET)) {
+ this.$store.dispatch('cullOlderMessages', this.currentChatMessageService.chatId)
+ }
+ }, 5000)
+ },
handleScroll: _.throttle(function () {
if (!this.currentChat) { return }
@@ -241,6 +248,7 @@ const Chat = {
this.fetchChat({ maxId: this.currentChatMessageService.minId })
} else if (this.bottomedOut(JUMP_TO_BOTTOM_BUTTON_VISIBILITY_OFFSET)) {
this.jumpToBottomButtonVisible = false
+ this.cullOlderCheck()
if (this.newMessageCount > 0) {
// Use a delay before marking as read to prevent situation where new messages
// arrive just as you're leaving the view and messages that you didn't actually
diff --git a/src/modules/chats.js b/src/modules/chats.js
index 0a373d88..69d683bd 100644
--- a/src/modules/chats.js
+++ b/src/modules/chats.js
@@ -115,6 +115,9 @@ const chats = {
},
handleMessageError ({ commit }, value) {
commit('handleMessageError', { commit, ...value })
+ },
+ cullOlderMessages ({ commit }, chatId) {
+ commit('cullOlderMessages', chatId)
}
},
mutations: {
@@ -227,6 +230,9 @@ const chats = {
handleMessageError (state, { chatId, fakeId, isRetry }) {
const chatMessageService = state.openedChatMessageServices[chatId]
chatService.handleMessageError(chatMessageService, fakeId, isRetry)
+ },
+ cullOlderMessages (state, chatId) {
+ chatService.cullOlderMessages(state.openedChatMessageServices[chatId])
}
}
}
diff --git a/src/services/chat_service/chat_service.js b/src/services/chat_service/chat_service.js
index e653ebc1..92ff689d 100644
--- a/src/services/chat_service/chat_service.js
+++ b/src/services/chat_service/chat_service.js
@@ -48,6 +48,22 @@ const deleteMessage = (storage, messageId) => {
}
}
+const cullOlderMessages = (storage) => {
+ const maxIndex = storage.messages.length
+ const minIndex = maxIndex - 50
+ if (maxIndex <= 50) return
+
+ storage.messages = _.sortBy(storage.messages, ['id'])
+ storage.minId = storage.messages[minIndex].id
+ for (const message of storage.messages) {
+ if (message.id < storage.minId) {
+ delete storage.idIndex[message.id]
+ delete storage.idempotencyKeyIndex[message.idempotency_key]
+ }
+ }
+ storage.messages = storage.messages.slice(minIndex, maxIndex)
+}
+
const handleMessageError = (storage, fakeId, isRetry) => {
if (!storage) { return }
const fakeMessage = storage.idIndex[fakeId]
@@ -201,6 +217,7 @@ const ChatService = {
empty,
getView,
deleteMessage,
+ cullOlderMessages,
resetNewMessageCount,
clear,
handleMessageError