Add SSE triggers for all modules and central listener

This commit is contained in:
josedario87
2025-06-09 15:26:35 -06:00
parent 031314d662
commit bec28b74ab
5 changed files with 89 additions and 24 deletions

View File

@@ -1,7 +1,7 @@
import { createApp } from 'vue'
import { createPinia } from 'pinia'
import router from './router'
import { usePlanillasStore } from './stores/usePlanillas'
import { useRealtimeStore } from './stores/useRealtime'
import App from './App.vue'
import './style.css' // Tailwind o tus estilos globales
@@ -16,7 +16,7 @@ app.use(pinia)
app.use(router)
// iniciar suscripción SSE cuando la app monte
const store = usePlanillasStore(pinia)
store.subscribeToEvents()
const realtime = useRealtimeStore(pinia)
realtime.init()
app.mount('#app')

View File

@@ -17,7 +17,6 @@ export const usePlanillasStore = defineStore('planillas', {
state: () => ({
planillas: [],
currentPlanilla: getDefaultCurrentPlanilla(),
_sse: null,
}),
actions: {
@@ -83,23 +82,5 @@ export const usePlanillasStore = defineStore('planillas', {
clearCurrentPlanilla() {
this.currentPlanilla = getDefaultCurrentPlanilla()
},
subscribeToEvents() {
if (this._sse) return
this._sse = new EventSource('/events')
this._sse.onmessage = (e) => {
try {
const payload = JSON.parse(e.data)
if (payload.table === 'Planilla') {
this.fetchPlanillas()
}
} catch (err) {
console.error('Error procesando SSE', err)
}
}
this._sse.onerror = () => {
console.warn('SSE connection lost, reloading...')
}
},
},
})

View File

@@ -0,0 +1,41 @@
import { defineStore } from 'pinia'
import { usePlanillasStore } from './usePlanillas'
import { useEmpleadosStore } from './useEmpleados'
import { useTareasStore } from './useTareas'
import { useAsistenciasStore } from './useAsistencias'
export const useRealtimeStore = defineStore('realtime', {
state: () => ({
_sse: null,
}),
actions: {
init() {
if (this._sse) return
this._sse = new EventSource('/events')
this._sse.onmessage = (e) => {
try {
const payload = JSON.parse(e.data)
switch (payload.table) {
case 'Planilla':
usePlanillasStore().fetchPlanillas()
break
case 'Cliente':
useEmpleadosStore().fetchEmpleados()
break
case 'TareaRealizada':
useTareasStore().fetchTareas()
break
case 'Asistencia':
useAsistenciasStore().fetchAsistencias()
break
}
} catch (err) {
console.error('Error procesando SSE', err)
}
}
this._sse.onerror = () => {
console.warn('SSE connection lost, reloading...')
}
},
},
})