2020-08-03 23:44:35 +00:00
|
|
|
const shout = {
|
2017-12-05 10:47:10 +00:00
|
|
|
state: {
|
|
|
|
messages: [],
|
2022-04-06 21:06:41 +00:00
|
|
|
channel: { state: '' },
|
|
|
|
joined: false
|
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)
|
2022-04-06 21:06:41 +00:00
|
|
|
},
|
|
|
|
setJoined (state, joined) {
|
|
|
|
state.joined = joined
|
2017-12-05 10:47:10 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
actions: {
|
2020-08-03 23:44:35 +00:00
|
|
|
initializeShout (store, socket) {
|
2021-06-01 19:31:58 +00:00
|
|
|
const channel = socket.channel('chat:public')
|
2022-04-06 21:06:41 +00:00
|
|
|
channel.joinPush.receive('ok', () => {
|
|
|
|
store.commit('setJoined', true)
|
|
|
|
})
|
|
|
|
channel.onClose(() => {
|
|
|
|
store.commit('setJoined', false)
|
|
|
|
})
|
|
|
|
channel.onError(() => {
|
|
|
|
store.commit('setJoined', false)
|
|
|
|
})
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-03 23:44:35 +00:00
|
|
|
export default shout
|