Skip to content

Instantly share code, notes, and snippets.

@nparsons08 nparsons08/17.js Secret
Last active May 28, 2019

Embed
What would you like to do?
// ./src/App.vue
// [...]
setActiveChannel (channel) {
// Before changing the active channel,
// Stop listening to event from the previous one
if (this.activeChannel) {
this.activeChannel['listeners']['message.new'] = []
}
// Reset the messsages array
this.messages = []
// loop through messages in this channel and translate them
channel.state.messages.forEach( async message => {
try {
const { data, status} =
await axios.post(`${process.env.VUE_APP_SERVER}/translate`, {
targetLanguage: this.language,
text: message.text
})
this.messages.push({...message, text: data.TranslatedText})
} catch (e) {
this.messages.push(message)
}
})
// Set the active channel
this.activeChannel = channel
// Listen for new messages
channel.on('message.new', async event => {
try {
const { data, status } =
await axios.post(`${process.env.VUE_APP_SERVER}/translate`, {
targetLanguage: this.language,
text: event.message.text
})
this.messages.push({...event.message, text: data.TranslatedText})
} catch (e) {
this.messages.push(event.message)
}
});
},
// [...]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.