Move shout module to store

This commit is contained in:
Sean King 2023-04-04 21:17:54 -06:00
commit aa6c13f9e6
No known key found for this signature in database
GPG key ID: 510C52BACD6E7257
8 changed files with 50 additions and 61 deletions

View file

@ -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 }) {

View file

@ -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