2020-08-03 16:44:35 -07:00
|
|
|
const shout = {
|
2017-12-05 02:47:10 -08:00
|
|
|
state: {
|
|
|
|
messages: [],
|
2019-08-17 01:18:42 -07:00
|
|
|
channel: { state: '' }
|
2017-12-05 02:47:10 -08: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: {
|
2020-08-03 16:44:35 -07:00
|
|
|
initializeShout (store, socket) {
|
|
|
|
const channel = socket.channel('shout:public')
|
2017-12-05 02:47:10 -08:00
|
|
|
channel.on('new_msg', (msg) => {
|
|
|
|
store.commit('addMessage', msg)
|
|
|
|
})
|
2019-07-05 00:02:14 -07:00
|
|
|
channel.on('messages', ({ messages }) => {
|
2017-12-05 02:47:10 -08:00
|
|
|
store.commit('setMessages', messages)
|
|
|
|
})
|
|
|
|
channel.join()
|
|
|
|
store.commit('setChannel', channel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-03 16:44:35 -07:00
|
|
|
export default shout
|