2017-12-05 10:47:10 +00:00
|
|
|
const chat = {
|
|
|
|
state: {
|
|
|
|
messages: [],
|
2019-08-17 08:18:42 +00:00
|
|
|
channel: { state: '' }
|
2017-12-05 10:47:10 +00:00
|
|
|
},
|
|
|
|
mutations: {
|
|
|
|
setChannel (state, channel) {
|
|
|
|
state.channel = channel
|
|
|
|
},
|
|
|
|
addMessage (state, message) {
|
|
|
|
state.messages.push(message)
|
|
|
|
state.messages = state.messages.slice(-19, 20)
|
|
|
|
},
|
|
|
|
setMessages (state, messages) {
|
|
|
|
state.messages = messages.slice(-19, 20)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
initializeChat (store, socket) {
|
|
|
|
const channel = socket.channel('chat:public')
|
2021-03-03 14:46:53 +00:00
|
|
|
|
2017-12-05 10:47:10 +00:00
|
|
|
channel.on('new_msg', (msg) => {
|
|
|
|
store.commit('addMessage', msg)
|
|
|
|
})
|
2019-07-05 07:02:14 +00:00
|
|
|
channel.on('messages', ({ messages }) => {
|
2017-12-05 10:47:10 +00:00
|
|
|
store.commit('setMessages', messages)
|
|
|
|
})
|
|
|
|
channel.join()
|
|
|
|
store.commit('setChannel', channel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default chat
|