push update socket data dan token
This commit is contained in:
No files matched your search
@@ -4,6 +4,7 @@ import { ref, computed, watch } from 'vue';
|
||||
import { useClinicStore } from './clinicStore';
|
||||
import { usePenunjangStore } from './penunjangStore';
|
||||
import { useLoketStore } from './loketStore';
|
||||
import { useWebSocket } from '@/composables/useWebSocket';
|
||||
|
||||
export const useQueueStore = defineStore('queue', () => {
|
||||
const clinicStore = useClinicStore();
|
||||
@@ -26,6 +27,77 @@ export const useQueueStore = defineStore('queue', () => {
|
||||
const lastUpdated = ref(Date.now());
|
||||
// synchronization guard (moved lower)
|
||||
|
||||
// ============================================
|
||||
// WEBSOCKET INTEGRATION (CENTRALIZED)
|
||||
// ============================================
|
||||
const wsInstance = ref(null);
|
||||
const isWsConnected = ref(false);
|
||||
const wsClientId = ref(`client-${Math.random().toString(36).substring(7)}`);
|
||||
|
||||
/**
|
||||
* Initialize Global WebSocket
|
||||
*/
|
||||
const initWebSocket = (customClientId = null) => {
|
||||
if (wsInstance.value && isWsConnected.value) {
|
||||
console.log('🔌 [queueStore] WebSocket already connected.');
|
||||
return;
|
||||
}
|
||||
|
||||
if (customClientId) {
|
||||
wsClientId.value = customClientId;
|
||||
}
|
||||
|
||||
const config = useRuntimeConfig();
|
||||
const wsBaseUrl = config.public?.wsBaseUrl || "ws://10.10.150.100:8084/api/v1/ws";
|
||||
|
||||
console.log(`🔌 [queueStore] Connecting to WebSocket: ${wsBaseUrl} as ${wsClientId.value}`);
|
||||
|
||||
wsInstance.value = useWebSocket({
|
||||
url: wsBaseUrl,
|
||||
clientId: wsClientId.value,
|
||||
onOpen: () => {
|
||||
console.log('✅ [queueStore] WebSocket connected');
|
||||
isWsConnected.value = true;
|
||||
},
|
||||
onClose: () => {
|
||||
console.log('❌ [queueStore] WebSocket disconnected');
|
||||
isWsConnected.value = false;
|
||||
},
|
||||
onError: (err) => {
|
||||
console.error('⚠️ [queueStore] WebSocket error:', err);
|
||||
isWsConnected.value = false;
|
||||
},
|
||||
onMessage: (data) => {
|
||||
console.log('📨 [queueStore] Global WS Message:', data);
|
||||
|
||||
// TRIGGER STRATEGIC REFRESHES
|
||||
// 1. Refetch patients for all active lokets in the store
|
||||
Object.keys(apiPatientsPerLoket.value).forEach(loketId => {
|
||||
fetchPatientsForLoket(loketId);
|
||||
});
|
||||
|
||||
// 2. Refetch clinics to update quotas/availability
|
||||
clinicStore.fetchRegulerClinics();
|
||||
|
||||
// 3. Ensure base data is synced
|
||||
ensureInitialData();
|
||||
}
|
||||
});
|
||||
|
||||
wsInstance.value.connect();
|
||||
};
|
||||
|
||||
/**
|
||||
* Disconnect Global WebSocket
|
||||
*/
|
||||
const disconnectWebSocket = () => {
|
||||
if (wsInstance.value) {
|
||||
wsInstance.value.disconnect();
|
||||
wsInstance.value = null;
|
||||
isWsConnected.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Sync patient status to apiPatientsPerLoket for reactivity
|
||||
*/
|
||||
@@ -2485,6 +2557,9 @@ export const useQueueStore = defineStore('queue', () => {
|
||||
checkAndResetDaily,
|
||||
isTodayPatient,
|
||||
getResetThreshold,
|
||||
initWebSocket,
|
||||
disconnectWebSocket,
|
||||
isWsConnected,
|
||||
};
|
||||
|
||||
}, {
|
||||
|
||||
Reference in New Issue
Block a user