Skip to content

Commit

Permalink
perf(conversations): update in store only changed conversation on fetch
Browse files Browse the repository at this point in the history
Signed-off-by: Grigorii K. Shartsev <me@shgk.me>
  • Loading branch information
ShGKme committed Jun 22, 2023
1 parent 3f0868c commit 5ddf7d7
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/store/conversationsStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,43 @@ const mutations = {

const actions = {
/**
* Add a conversation to the store and index the displayName.
* Add a conversation to the store and index the displayName
*
* @param {object} context default store context;
* @param {object} conversation the conversation;
*/
addConversation(context, conversation) {
context.commit('addConversation', conversation)

context.dispatch('postAddConversation', conversation)
},

/**
* Add conversation to the store only, if it was changed
*
* @param {object} context dispatch context
* @param {object} payload mutation payload
* @param {object} payload.conversation the conversation
* @param {number|0} payload.modifiedSince timestamp of last state or 0 if unknown
*/
addConversationIfChanged(context, { conversation, modifiedSince }) {
// console.log(conversation.lastActivity, modifiedSince, modifiedSince && conversation.lastActivity >= modifiedSince)
if (conversation.lastActivity >= modifiedSince) {
context.commit('addConversation', conversation)
}

context.dispatch('postAddConversation', conversation)
},

/**
* Post-actions after adding a conversation:
* - Get user status from 1-1 conversations
* - Add current user to the new conversation's participants
*
* @param {object} context dispatch context
* @param {object} conversation the conversation
*/
postAddConversation(context, conversation) {
if (conversation.type === CONVERSATION.TYPE.ONE_TO_ONE && conversation.status) {
emit('user_status:status.updated', {
status: conversation.status,
Expand Down Expand Up @@ -649,7 +678,7 @@ const actions = {
dispatch('purgeConversationsStore')
}
response.data.ocs.data.forEach(conversation => {
dispatch('addConversation', conversation)
dispatch('addConversationIfChanged', { conversation, modifiedSince })
})
return response
} catch (error) {
Expand Down

0 comments on commit 5ddf7d7

Please sign in to comment.