fix: PWA icon, robust socket.io reconnect, faster real-time sync
This commit is contained in:
@@ -20,7 +20,7 @@ import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/u
|
||||
import { JournalView } from '@/components/journal/journal-view'
|
||||
import { jsPDF } from 'jspdf'
|
||||
import { Lock, Unlock, Eye, AlertTriangle, WifiOff } from 'lucide-react'
|
||||
import { getSocket } from '@/lib/socket'
|
||||
import { getSocket, setSocketRoom } from '@/lib/socket'
|
||||
import { CustomDragLayer } from '@/components/map/custom-drag-layer'
|
||||
import { addToSyncQueue, flushSyncQueue, getSyncQueue, isOnline as checkOnline } from '@/lib/offline-sync'
|
||||
|
||||
@@ -507,27 +507,31 @@ export default function AppPage() {
|
||||
const socketRef = useRef<any>(null)
|
||||
const prevProjectIdRef = useRef<string | null>(null)
|
||||
|
||||
// Throttled socket broadcast for near-real-time sync (1.5s instead of 10s auto-save)
|
||||
// Throttled socket broadcast for near-real-time sync
|
||||
const lastEmitRef = useRef(0)
|
||||
const emitTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
|
||||
const currentProjectRef = useRef(currentProject)
|
||||
useEffect(() => { currentProjectRef.current = currentProject }, [currentProject])
|
||||
|
||||
const broadcastFeatures = useCallback((feats: DrawFeature[]) => {
|
||||
if (!socketRef.current || !currentProject?.id || !isEditingByMe) return
|
||||
const proj = currentProjectRef.current
|
||||
if (!socketRef.current || !proj?.id || !isEditingByMeRef.current) return
|
||||
const now = Date.now()
|
||||
const emit = () => {
|
||||
socketRef.current?.emit('features-updated', {
|
||||
projectId: currentProject!.id,
|
||||
projectId: proj!.id,
|
||||
features: feats,
|
||||
})
|
||||
lastEmitRef.current = Date.now()
|
||||
}
|
||||
// Throttle: emit at most every 1.5 seconds
|
||||
if (now - lastEmitRef.current > 1500) {
|
||||
// Throttle: emit at most every 800ms for snappier sync
|
||||
if (now - lastEmitRef.current > 800) {
|
||||
emit()
|
||||
} else {
|
||||
if (emitTimerRef.current) clearTimeout(emitTimerRef.current)
|
||||
emitTimerRef.current = setTimeout(emit, 1500 - (now - lastEmitRef.current))
|
||||
emitTimerRef.current = setTimeout(emit, 800 - (now - lastEmitRef.current))
|
||||
}
|
||||
}, [currentProject?.id, isEditingByMe])
|
||||
}, [])
|
||||
const isEditingByMeRef = useRef(false)
|
||||
|
||||
// Keep ref in sync with state
|
||||
@@ -546,6 +550,7 @@ export default function AppPage() {
|
||||
socket.emit('leave-project', prevProjectIdRef.current)
|
||||
}
|
||||
socket.emit('join-project', currentProject.id)
|
||||
setSocketRoom(currentProject.id)
|
||||
prevProjectIdRef.current = currentProject.id
|
||||
|
||||
// Listen for features changes from other clients (only apply if NOT the editor)
|
||||
|
||||
Reference in New Issue
Block a user