Skip to content

Commit

Permalink
fix(NewConversationDialog): merge all secondary requests under a sing…
Browse files Browse the repository at this point in the history
…le try-catch block, run in parallel

Signed-off-by: Maksim Sukharev <antreesy.web@gmail.com>
  • Loading branch information
Antreesy committed May 28, 2024
1 parent 7d8fc39 commit 67aca89
Showing 1 changed file with 20 additions and 29 deletions.
49 changes: 20 additions & 29 deletions src/components/NewConversationDialog/NewConversationDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -290,53 +290,44 @@ export default {
isPublic: this.isPublic,
})

// Gather all secondary requests to run in parallel
const promises = []

if (this.isPublic && this.password && this.newConversation.hasPassword) {
await setConversationPassword(this.newConversation.token, this.password)
promises.push(setConversationPassword(this.newConversation.token, this.password))
}

if (this.isAvatarEdited) {
await this.$refs.setupPage.$refs.conversationAvatar.saveAvatar()
promises.push(this.$refs.setupPage.$refs.conversationAvatar.saveAvatar)
}

if (this.newConversation.description) {
await this.$store.dispatch('setConversationDescription', {
promises.push(this.$store.dispatch('setConversationDescription', {
token: this.newConversation.token,
description: this.newConversation.description,
})
}))
}
} catch (exception) {
console.error(exception)
this.isLoading = false
this.error = true
// Stop the execution of the method on exceptions.
return
}

try {
await this.$store.dispatch('setListable', {
token: this.newConversation.token,
listable: this.listable,
})
if (this.listable !== CONVERSATION.LISTABLE.NONE) {
promises.push(this.$store.dispatch('setListable', {
token: this.newConversation.token,
listable: this.listable,
}))
}

for (const participant of this.selectedParticipants) {
promises.push(addParticipant(this.newConversation.token, participant.id, participant.source))
}

await Promise.all(promises)
} catch (exception) {
console.error(exception)
console.error('Error creating new conversation: ', exception)
this.isLoading = false
this.error = true
// Stop the execution of the method on exceptions.
return
}

for (const participant of this.selectedParticipants) {
try {
await addParticipant(this.newConversation.token, participant.id, participant.source)
} catch (exception) {
console.error(exception)
this.isLoading = false
this.error = true
// Stop the execution of the method on exceptions.
return
}
}

this.success = true
this.isLoading = false

Expand Down

0 comments on commit 67aca89

Please sign in to comment.