|
| 1 | +import { net } from "electron"; |
| 2 | +import { injectable, postConstruct } from "inversify"; |
| 3 | +import { logger } from "../../lib/logger.js"; |
| 4 | +import { TypedEventEmitter } from "../../lib/typed-event-emitter.js"; |
| 5 | +import { |
| 6 | + ConnectivityEvent, |
| 7 | + type ConnectivityEvents, |
| 8 | + type ConnectivityStatusOutput, |
| 9 | +} from "./schemas.js"; |
| 10 | + |
| 11 | +const log = logger.scope("connectivity"); |
| 12 | + |
| 13 | +const CHECK_URL = "https://www.google.com/generate_204"; |
| 14 | +const MIN_POLL_INTERVAL_MS = 3_000; |
| 15 | +const MAX_POLL_INTERVAL_MS = 10_000; |
| 16 | +const ONLINE_POLL_INTERVAL_MS = 3_000; |
| 17 | + |
| 18 | +@injectable() |
| 19 | +export class ConnectivityService extends TypedEventEmitter<ConnectivityEvents> { |
| 20 | + private isOnline = false; |
| 21 | + private pollTimeoutId: ReturnType<typeof setTimeout> | null = null; |
| 22 | + private currentPollInterval = MIN_POLL_INTERVAL_MS; |
| 23 | + |
| 24 | + @postConstruct() |
| 25 | + init(): void { |
| 26 | + this.isOnline = net.isOnline(); |
| 27 | + log.info("Initial connectivity status", { isOnline: this.isOnline }); |
| 28 | + |
| 29 | + this.startPolling(); |
| 30 | + } |
| 31 | + |
| 32 | + getStatus(): ConnectivityStatusOutput { |
| 33 | + return { isOnline: this.isOnline }; |
| 34 | + } |
| 35 | + |
| 36 | + async checkNow(): Promise<ConnectivityStatusOutput> { |
| 37 | + await this.checkConnectivity(); |
| 38 | + return { isOnline: this.isOnline }; |
| 39 | + } |
| 40 | + |
| 41 | + private setOnline(online: boolean): void { |
| 42 | + if (this.isOnline === online) return; |
| 43 | + |
| 44 | + this.isOnline = online; |
| 45 | + log.info("Connectivity status changed", { isOnline: online }); |
| 46 | + this.emit(ConnectivityEvent.StatusChange, { isOnline: online }); |
| 47 | + |
| 48 | + this.currentPollInterval = MIN_POLL_INTERVAL_MS; |
| 49 | + } |
| 50 | + |
| 51 | + private async checkConnectivity(): Promise<void> { |
| 52 | + if (!net.isOnline()) { |
| 53 | + this.setOnline(false); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + if (!this.isOnline) { |
| 58 | + const verified = await this.verifyWithHttp(); |
| 59 | + this.setOnline(verified); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private async verifyWithHttp(): Promise<boolean> { |
| 64 | + try { |
| 65 | + const response = await net.fetch(CHECK_URL, { method: "HEAD" }); |
| 66 | + return response.ok || response.status === 204; |
| 67 | + } catch (error) { |
| 68 | + log.debug("HTTP connectivity check failed", { error }); |
| 69 | + return false; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private startPolling(): void { |
| 74 | + if (this.pollTimeoutId) return; |
| 75 | + |
| 76 | + this.currentPollInterval = MIN_POLL_INTERVAL_MS; |
| 77 | + this.schedulePoll(); |
| 78 | + } |
| 79 | + |
| 80 | + private schedulePoll(): void { |
| 81 | + // when online: just poll net.isOnline periodically |
| 82 | + // when offline: poll more frequently with backoff to detect recovery |
| 83 | + const interval = this.isOnline |
| 84 | + ? ONLINE_POLL_INTERVAL_MS |
| 85 | + : this.currentPollInterval; |
| 86 | + |
| 87 | + this.pollTimeoutId = setTimeout(async () => { |
| 88 | + this.pollTimeoutId = null; |
| 89 | + |
| 90 | + const wasOffline = !this.isOnline; |
| 91 | + await this.checkConnectivity(); |
| 92 | + |
| 93 | + if (!this.isOnline && wasOffline) { |
| 94 | + this.currentPollInterval = Math.min( |
| 95 | + this.currentPollInterval * 1.5, |
| 96 | + MAX_POLL_INTERVAL_MS, |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + this.schedulePoll(); |
| 101 | + }, interval); |
| 102 | + } |
| 103 | +} |
0 commit comments