refactor: isolate application state per Discord server

This commit is contained in:
mmacierzanka committed 2026-08-21 16:14:22 +02:00
1 parent 94be2c7c79
commit 64b428555c
30 files changed
+330 -471

No files matched your search

+27 -6
View File
@@ -1,6 +1,7 @@
import { onUnmounted, ref } from 'vue'
import type { BotStatus } from '@/types/status'
import { useToasts } from '@/composables/useToasts'
import { getGuildId, setGuildId } from '@/api/client'
const RECONNECT_DELAY_MS = 3000
@@ -20,9 +21,13 @@ export function useBotStatus() {
function open() {
const scheme = window.location.protocol === 'https:' ? 'wss:' : 'ws:'
socket = new WebSocket(`${scheme}//${window.location.host}/ws`)
const guild = getGuildId()
const currentSocket = new WebSocket(
`${scheme}//${window.location.host}/ws${guild ? `?guild_id=${guild}` : ''}`,
)
socket = currentSocket
socket.onopen = () => {
currentSocket.onopen = () => {
connected.value = true
notify(
connectedOnce ? 'Połączenie WebSocket przywrócone (Live)!' : 'WebSocket zsynchronizowany (Live)!',
@@ -31,7 +36,8 @@ export function useBotStatus() {
connectedOnce = true
}
socket.onmessage = (event) => {
currentSocket.onmessage = (event) => {
if (socket !== currentSocket) return
if (paused.value) return
try {
status.value = JSON.parse(event.data) as BotStatus
@@ -40,7 +46,8 @@ export function useBotStatus() {
}
}
socket.onclose = () => {
currentSocket.onclose = () => {
if (socket !== currentSocket) return
connected.value = false
socket = null
if (subscribers > 0) {
@@ -48,7 +55,7 @@ export function useBotStatus() {
}
}
socket.onerror = () => socket?.close()
currentSocket.onerror = () => currentSocket.close()
}
function connect() {
@@ -67,7 +74,21 @@ export function useBotStatus() {
socket = null
}
function selectGuild(guildId: string) {
setGuildId(guildId)
if (reconnectTimer !== null) {
window.clearTimeout(reconnectTimer)
reconnectTimer = null
}
if (socket) {
const previousSocket = socket
socket = null
previousSocket.close()
open()
}
}
onUnmounted(disconnect)
return { status, connected, paused, connect, disconnect }
return { status, connected, paused, connect, disconnect, selectGuild }
}