push perbaikan
This commit is contained in:
No files matched your search
+62
-2
@@ -266,6 +266,8 @@ export const useQueueStore = defineStore('queue', () => {
|
||||
allPatients.value.push(...newPatientMap.values());
|
||||
|
||||
// console.log(`✅ [queueStore] Successfully fetched ${mappedClinicPatients.length} patients for clinic ${kodeKlinik}`);
|
||||
// SYNC COUNTERS WITH API DATA SO NEW TICKETS DONT RESET
|
||||
syncCountersWithState();
|
||||
return { success: true, message: `${mappedClinicPatients.length} pasien dimuat` };
|
||||
|
||||
} catch (error) {
|
||||
@@ -420,6 +422,8 @@ export const useQueueStore = defineStore('queue', () => {
|
||||
url: wsBaseUrl,
|
||||
clientId: wsClientId,
|
||||
fallbackPostUrl: '/stats-api/ws',
|
||||
reconnectInterval: 5000, // 5 seconds between reconnect attempts
|
||||
maxReconnectAttempts: 9999, // Effectively infinite — never give up on remote machines
|
||||
onOpen: () => {
|
||||
console.log('✅ [queueStore] WebSocket connected');
|
||||
isWsConnected.value = true;
|
||||
@@ -435,12 +439,60 @@ export const useQueueStore = defineStore('queue', () => {
|
||||
onMessage: onWsMessage
|
||||
});
|
||||
|
||||
// ============================================
|
||||
// STORE-LEVEL AUTO-POLLING (cross-device sync fallback)
|
||||
// ============================================
|
||||
// Runs on every browser instance every 30 seconds.
|
||||
// Fetches data based on whatever active interests are registered
|
||||
// (lokets, clinics, or global). This ensures any device always
|
||||
// has fresh data regardless of WS delivery reliability.
|
||||
let _autoSyncInterval = null;
|
||||
|
||||
const startAutoSync = () => {
|
||||
// Guard: only run on client, and only start once
|
||||
if (typeof window === 'undefined') return;
|
||||
if (_autoSyncInterval) return; // Already running
|
||||
|
||||
console.log('🔄 [queueStore] Starting store-level auto-sync (30s interval)');
|
||||
|
||||
_autoSyncInterval = setInterval(async () => {
|
||||
const hasLoketInterest = Object.keys(activeLoketInterest.value).length > 0;
|
||||
const hasClinicInterest = Object.keys(activeClinicInterest.value).length > 0;
|
||||
const hasGlobalInterest = globalInterestCount.value > 0;
|
||||
|
||||
if (hasGlobalInterest) {
|
||||
fetchAllPatients();
|
||||
} else {
|
||||
if (hasLoketInterest) {
|
||||
Object.keys(activeLoketInterest.value).forEach(loketId => {
|
||||
fetchPatientsForLoket(loketId, true);
|
||||
});
|
||||
}
|
||||
if (hasClinicInterest) {
|
||||
Object.keys(activeClinicInterest.value).forEach(kodeKlinik => {
|
||||
fetchPatientsForClinic(kodeKlinik, true);
|
||||
});
|
||||
}
|
||||
}
|
||||
}, 30000); // 30 seconds
|
||||
};
|
||||
|
||||
const stopAutoSync = () => {
|
||||
if (_autoSyncInterval) {
|
||||
clearInterval(_autoSyncInterval);
|
||||
_autoSyncInterval = null;
|
||||
console.log('⏹️ [queueStore] Store-level auto-sync stopped');
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Initialize Global WebSocket
|
||||
*/
|
||||
const initWebSocket = (customClientId = null) => {
|
||||
if (isConnected.value && customClientId === wsClientId.value) {
|
||||
console.log('🔌 [queueStore] WebSocket already connected with same ID.');
|
||||
// Auto-sync should still start even if WS is already connected
|
||||
startAutoSync();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -448,11 +500,14 @@ export const useQueueStore = defineStore('queue', () => {
|
||||
wsClientId.value = customClientId;
|
||||
// Re-connect with new ID if changed
|
||||
disconnect();
|
||||
// useWebSocket will use the new wsClientId.value if it's reactive
|
||||
}
|
||||
|
||||
console.log(`🔌 [queueStore] Connecting to WebSocket: ${wsBaseUrl} as ${wsClientId.value}`);
|
||||
connect();
|
||||
|
||||
// Start store-level auto-polling if not already running (client-side only).
|
||||
// This guarantees cross-device sync even when WS messages are missed.
|
||||
startAutoSync();
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -925,6 +980,9 @@ const fetchPatientsForLoket = async (loketId, force = false) => {
|
||||
// console.log(`✅ [queueStore] Successfully fetched ${finalPatients.length} unique patients for loket ${loketId} (Original: ${mappedPatients.length})`);
|
||||
// console.log(`📊 [queueStore] Total patients in allPatients: ${allPatients.value.length}`);
|
||||
|
||||
// SYNC COUNTERS WITH API DATA SO NEW TICKETS DONT RESET
|
||||
syncCountersWithState();
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: `${mappedPatients.length} pasien berhasil dimuat`,
|
||||
@@ -3366,13 +3424,15 @@ const fetchPatientsForLoket = async (loketId, force = false) => {
|
||||
activeClinicInterest,
|
||||
registerGlobalInterest,
|
||||
unregisterGlobalInterest,
|
||||
startAutoSync,
|
||||
stopAutoSync,
|
||||
};
|
||||
|
||||
}, {
|
||||
persist: {
|
||||
key: 'queue-store-state',
|
||||
storage: typeof window !== 'undefined' ? localStorage : undefined,
|
||||
paths: ['allPatients', 'quotaUsed', 'currentProcessingPatient', 'apiPatientsPerLoket', 'lastUpdated'],
|
||||
paths: ['quotaUsed', 'lastUpdated'],
|
||||
serializer: {
|
||||
deserialize: JSON.parse,
|
||||
serialize: JSON.stringify,
|
||||
|
||||
Reference in New Issue
Block a user