Move shout module to store
This commit is contained in:
parent
aa98e83ff0
commit
aa6c13f9e6
8 changed files with 50 additions and 61 deletions
|
|
@ -2,6 +2,7 @@ import backendInteractorService from '../services/backend_interactor_service/bac
|
|||
import { WSConnectionStatus } from '../services/api/api.service.js'
|
||||
import { maybeShowChatNotification } from '../services/chat_utils/chat_utils.js'
|
||||
import { Socket } from 'phoenix'
|
||||
import { useShoutStore } from '../stores/shout.js'
|
||||
|
||||
const retryTimeout = (multiplier) => 1000 * multiplier
|
||||
|
||||
|
|
@ -283,7 +284,7 @@ const api = {
|
|||
socket.connect()
|
||||
|
||||
commit('setSocket', socket)
|
||||
dispatch('initializeShout', socket)
|
||||
useShoutStore().initializeShout(socket)
|
||||
}
|
||||
},
|
||||
disconnectFromSocket ({ commit, state }) {
|
||||
|
|
|
|||
|
|
@ -1,46 +0,0 @@
|
|||
const shout = {
|
||||
state: {
|
||||
messages: [],
|
||||
channel: { state: '' },
|
||||
joined: false
|
||||
},
|
||||
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)
|
||||
},
|
||||
setJoined (state, joined) {
|
||||
state.joined = joined
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
initializeShout (store, socket) {
|
||||
const channel = socket.channel('chat:public')
|
||||
channel.joinPush.receive('ok', () => {
|
||||
store.commit('setJoined', true)
|
||||
})
|
||||
channel.onClose(() => {
|
||||
store.commit('setJoined', false)
|
||||
})
|
||||
channel.onError(() => {
|
||||
store.commit('setJoined', false)
|
||||
})
|
||||
channel.on('new_msg', (msg) => {
|
||||
store.commit('addMessage', msg)
|
||||
})
|
||||
channel.on('messages', ({ messages }) => {
|
||||
store.commit('setMessages', messages)
|
||||
})
|
||||
channel.join()
|
||||
store.commit('setChannel', channel)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default shout
|
||||
Loading…
Add table
Add a link
Reference in a new issue