()
| 49 | * once and the subscription effect always runs. |
| 50 | */ |
| 51 | export function useDesktopUpdate(): DesktopUpdate | null { |
| 52 | const bridge = useMemo(() => getDesktopUpdateBridge(), []); |
| 53 | const [status, setStatus] = useState<DesktopUpdateStatus>({ state: "idle" }); |
| 54 | |
| 55 | useEffect(() => { |
| 56 | if (!bridge) return; |
| 57 | let active = true; |
| 58 | // The initial snapshot is an async IPC round-trip; a push (e.g. download |
| 59 | // finished) can land first. Never let the stale snapshot overwrite a push |
| 60 | // that already arrived. |
| 61 | let receivedPush = false; |
| 62 | void bridge.getUpdateStatus().then((current) => { |
| 63 | if (active && !receivedPush) setStatus(current); |
| 64 | }); |
| 65 | const unsubscribe = bridge.onUpdateStatus((next) => { |
| 66 | receivedPush = true; |
| 67 | setStatus(next); |
| 68 | }); |
| 69 | return () => { |
| 70 | active = false; |
| 71 | unsubscribe(); |
| 72 | }; |
| 73 | }, [bridge]); |
| 74 | |
| 75 | if (!bridge) return null; |
| 76 | return { status, install: () => void bridge.installUpdate() }; |
| 77 | } |
no test coverage detected