|
| 1 | +import { addEventListener } from "$lib/internal/utils/event.js"; |
| 2 | +import { Previous } from "$lib/utilities/index.js"; |
| 3 | +import { browser } from "$app/environment"; |
| 4 | + |
| 5 | +/** |
| 6 | + * @desc The `NetworkInformation` interface of the Network Information API |
| 7 | + * @see https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation |
| 8 | + */ |
| 9 | +type NetworkInformation = { |
| 10 | + readonly downlink: number; |
| 11 | + readonly downlinkMax: number; |
| 12 | + readonly effectiveType: "slow-2g" | "2g" | "3g" | "4g"; |
| 13 | + readonly rtt: number; |
| 14 | + readonly saveData: boolean; |
| 15 | + readonly type: |
| 16 | + | "bluetooth" |
| 17 | + | "cellular" |
| 18 | + | "ethernet" |
| 19 | + | "none" |
| 20 | + | "wifi" |
| 21 | + | "wimax" |
| 22 | + | "other" |
| 23 | + | "unknown"; |
| 24 | +} & EventTarget; |
| 25 | + |
| 26 | +type NavigatorWithConnection = Navigator & { connection: NetworkInformation }; |
| 27 | + |
| 28 | +interface NetworkStatus { |
| 29 | + /** |
| 30 | + * @desc Returns the online status of the browser. |
| 31 | + * @see https://developer.mozilla.org/en-US/docs/Web/API/Navigator/onLine |
| 32 | + */ |
| 33 | + online: boolean; |
| 34 | + /** |
| 35 | + * @desc The {Date} object pointing to the moment when state update occurred. |
| 36 | + */ |
| 37 | + updatedAt: Date; |
| 38 | + /** |
| 39 | + * @desc Effective bandwidth estimate in megabits per second, rounded to the |
| 40 | + * nearest multiple of 25 kilobits per seconds. |
| 41 | + * @see https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation/downlink |
| 42 | + */ |
| 43 | + downlink?: NetworkInformation["downlink"]; |
| 44 | + /** |
| 45 | + * @desc Maximum downlink speed, in megabits per second (Mbps), for the |
| 46 | + * underlying connection technology |
| 47 | + * @see https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation/downlinkMax |
| 48 | + */ |
| 49 | + downlinkMax?: NetworkInformation["downlinkMax"]; |
| 50 | + /** |
| 51 | + * @desc Effective type of the connection meaning one of 'slow-2g', '2g', '3g', or '4g'. |
| 52 | + * This value is determined using a combination of recently observed round-trip time |
| 53 | + * and downlink values. |
| 54 | + * @see https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation/effectiveType |
| 55 | + */ |
| 56 | + effectiveType?: NetworkInformation["effectiveType"]; |
| 57 | + /** |
| 58 | + * @desc Estimated effective round-trip time of the current connection, rounded |
| 59 | + * to the nearest multiple of 25 milliseconds |
| 60 | + * @see https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation/rtt |
| 61 | + */ |
| 62 | + rtt?: NetworkInformation["rtt"]; |
| 63 | + /** |
| 64 | + * @desc {true} if the user has set a reduced data usage option on the user agent. |
| 65 | + * @see https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation/saveData |
| 66 | + */ |
| 67 | + saveData?: NetworkInformation["saveData"]; |
| 68 | + /** |
| 69 | + * @desc The type of connection a device is using to communicate with the network. |
| 70 | + * It will be one of the following values: |
| 71 | + * - bluetooth |
| 72 | + * - cellular |
| 73 | + * - ethernet |
| 74 | + * - none |
| 75 | + * - wifi |
| 76 | + * - wimax |
| 77 | + * - other |
| 78 | + * - unknown |
| 79 | + * @see https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation/type |
| 80 | + */ |
| 81 | + type?: NetworkInformation["type"]; |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * Tracks the state of browser's network connection. |
| 86 | + * @returns null if the function is not run in the browser |
| 87 | + */ |
| 88 | +export function useNetworkStatus() { |
| 89 | + if (!browser) { |
| 90 | + return { |
| 91 | + get current() { |
| 92 | + return null; |
| 93 | + }, |
| 94 | + get previous() { |
| 95 | + return null; |
| 96 | + }, |
| 97 | + }; |
| 98 | + } |
| 99 | + const navigator = window.navigator; |
| 100 | + const connection = |
| 101 | + "connection" in navigator ? (navigator as NavigatorWithConnection).connection : undefined; |
| 102 | + const getConnectionStatus = (): NetworkStatus => { |
| 103 | + return { |
| 104 | + online: navigator.onLine, |
| 105 | + updatedAt: new Date(), |
| 106 | + downlink: connection?.downlink, |
| 107 | + downlinkMax: connection?.downlinkMax, |
| 108 | + effectiveType: connection?.effectiveType, |
| 109 | + rtt: connection?.rtt, |
| 110 | + saveData: connection?.saveData, |
| 111 | + type: connection?.type, |
| 112 | + }; |
| 113 | + }; |
| 114 | + |
| 115 | + let current = $state(getConnectionStatus()); |
| 116 | + const previous = new Previous(() => current); |
| 117 | + const handleStatusChange = () => { |
| 118 | + current = getConnectionStatus(); |
| 119 | + }; |
| 120 | + |
| 121 | + $effect(() => { |
| 122 | + // The connection event handler also manages online and offline states. |
| 123 | + if (connection) { |
| 124 | + addEventListener(connection, "change", handleStatusChange, { passive: true }); |
| 125 | + } else { |
| 126 | + addEventListener(window, "online", handleStatusChange, { passive: true }); |
| 127 | + addEventListener(window, "offline", handleStatusChange, { passive: true }); |
| 128 | + } |
| 129 | + |
| 130 | + return () => { |
| 131 | + window.removeEventListener("online", handleStatusChange); |
| 132 | + window.removeEventListener("online", handleStatusChange); |
| 133 | + connection?.removeEventListener("change", handleStatusChange); |
| 134 | + }; |
| 135 | + }); |
| 136 | + |
| 137 | + return { |
| 138 | + get current() { |
| 139 | + return current; |
| 140 | + }, |
| 141 | + get previous() { |
| 142 | + return previous.current; |
| 143 | + }, |
| 144 | + }; |
| 145 | +} |
0 commit comments