import { hasEditedFields, hasRegisteredUnsavedChanges } from "@suphark/ui/lib/unsaved-registry"; const UPDATE_CHECK_INTERVAL_MS = 15 * 60_000; /** หลังออกจากช่องกรอก รอให้นิ่งก่อน reload — ระหว่างย้ายช่อง/กดปุ่มบันทึก activeElement เป็น body ชั่วขณะ */ const IDLE_RELOAD_MS = 2000; const isEditableElement = (element: Element | null) => { if (!element) return false; const tag = element.tagName; if (tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT") return true; return (element as HTMLElement).isContentEditable; }; /** * reload ตอนนี้ได้โดยไม่ขัดผู้ใช้: ไม่มีช่องที่กำลังพิมพ์ (หรือแท็บซ่อนอยู่) และไม่มีการแก้ค้าง * (UnsavedChangesGuard ที่ dirty / ช่องกรอกที่ค่าต่างจากค่าเริ่มต้น) — เดิมกลับมาที่แท็บแล้ว reload ทับฟอร์มที่กรอกค้าง */ export const canReloadNow = (doc: Document) => (doc.visibilityState === "hidden" || !isEditableElement(doc.activeElement)) && !hasRegisteredUnsavedChanges() && !hasEditedFields(doc); /** * ทำให้ service worker ที่ติดตั้งอยู่ตาม deployment ล่าสุดเสมอ * * - ขอให้ browser เช็ค script ใหม่ตอนโหลด, ทุกครั้งที่กลับมาที่แท็บ, และทุก 15 นาที * - worker ฝั่งแอปต้อง `skipWaiting()` + `clients.claim()` (ดู README) → ตัวใหม่คุมหน้าเมื่อติดตั้งเสร็จ * แล้ว `controllerchange` จะ reload หน้าให้ bundle ใหม่ทำงานทันที — ขณะผู้ใช้กำลังพิมพ์เลื่อนไปจนออกจากช่องและนิ่ง 2 วินาที * และไม่ reload ตอนติดตั้งครั้งแรก * * คืนฟังก์ชัน cleanup */ export function watchServiceWorkerUpdates( container: ServiceWorkerContainer, registration: ServiceWorkerRegistration, doc: Document = document, win: Window = window, ) { const hadController = container.controller !== null; let reloadPending = false; let reloading = false; const reload = () => { if (reloading) return; reloading = true; win.location.reload(); }; const tryReload = () => { if (!reloadPending) return; if (canReloadNow(doc)) reload(); }; // ออกจากช่องกรอก: ไม่ reload ทันที (focusout เกิดก่อน focus ไปช่องถัดไป และก่อน click ของปุ่มบันทึก // → reload กลางการกรอก/ทิ้งการบันทึก) — รอ 2 วินาทีที่ไม่มีการกด/พิมพ์/โฟกัสใหม่ แล้วค่อยลอง let idleTimer: number | null = null; const cancelIdleReload = () => { if (idleTimer !== null) win.clearTimeout(idleTimer); idleTimer = null; }; const scheduleIdleReload = () => { cancelIdleReload(); if (!reloadPending) return; idleTimer = win.setTimeout(() => { idleTimer = null; tryReload(); }, IDLE_RELOAD_MS); }; const handleControllerChange = () => { // ติดตั้งครั้งแรก: หน้านี้รัน bundle ปัจจุบันอยู่แล้ว if (!hadController) return; reloadPending = true; tryReload(); }; const checkForUpdate = () => { if (doc.visibilityState !== "visible") return; void registration.update().catch(() => undefined); }; const handleVisibilityChange = () => { if (doc.visibilityState === "visible") { checkForUpdate(); tryReload(); } }; container.addEventListener("controllerchange", handleControllerChange); doc.addEventListener("visibilitychange", handleVisibilityChange); doc.addEventListener("focusout", scheduleIdleReload); // ผู้ใช้ยังทำอะไรอยู่ = เลื่อนออกไป (focusout ครั้งถัดไปจะนับใหม่) const activity = ["focusin", "pointerdown", "keydown"] as const; for (const type of activity) doc.addEventListener(type, cancelIdleReload, true); const interval = win.setInterval(checkForUpdate, UPDATE_CHECK_INTERVAL_MS); checkForUpdate(); return () => { container.removeEventListener("controllerchange", handleControllerChange); doc.removeEventListener("visibilitychange", handleVisibilityChange); doc.removeEventListener("focusout", scheduleIdleReload); for (const type of activity) doc.removeEventListener(type, cancelIdleReload, true); cancelIdleReload(); win.clearInterval(interval); }; }