Move shout module to store
This commit is contained in:
parent
aa98e83ff0
commit
aa6c13f9e6
8 changed files with 50 additions and 61 deletions
32
src/stores/shout.js
Normal file
32
src/stores/shout.js
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
import { defineStore } from 'pinia'
|
||||
|
||||
export const useShoutStore = defineStore('shout', {
|
||||
state: () => ({
|
||||
messages: [],
|
||||
channel: { state: '' },
|
||||
joined: false
|
||||
}),
|
||||
actions: {
|
||||
initializeShout (socket) {
|
||||
const channel = socket.channel('chat:public')
|
||||
channel.joinPush.receive('ok', () => {
|
||||
this.joined = true
|
||||
})
|
||||
channel.onClose(() => {
|
||||
this.joined = false
|
||||
})
|
||||
channel.onError(() => {
|
||||
this.joined = false
|
||||
})
|
||||
channel.on('new_msg', (msg) => {
|
||||
this.messages.push(msg)
|
||||
this.messages = this.messages.slice(-19, 20)
|
||||
})
|
||||
channel.on('messages', ({ messages }) => {
|
||||
this.messages = messages.slice(-19, 20)
|
||||
})
|
||||
channel.join()
|
||||
this.channel = channel
|
||||
}
|
||||
}
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue