|
| 1 | +type infoType = string; |
| 2 | + |
| 3 | +type infoEffectiveType = string; |
| 4 | + |
| 5 | +type connectionType = |
| 6 | + | None |
| 7 | + | WiFi |
| 8 | + | Cellular |
| 9 | + | Unknown |
| 10 | + | Bluetooth |
| 11 | + | Ethernet |
| 12 | + | WiMax; |
| 13 | + |
| 14 | +type effectiveConnectionType = |
| 15 | + | Net2G |
| 16 | + | Net3G |
| 17 | + | Net4G |
| 18 | + | Unknown; |
| 19 | + |
| 20 | +type info = { |
| 21 | + . |
| 22 | + "effectiveType": infoEffectiveType, |
| 23 | + "_type": infoType |
| 24 | +}; |
| 25 | + |
| 26 | +let connectionType = connectionType => |
| 27 | + switch connectionType { |
| 28 | + | "none" => None |
| 29 | + | "wifi" => WiFi |
| 30 | + | "cellular" => Cellular |
| 31 | + | "unknown" => Unknown |
| 32 | + | "bluetooth" => Bluetooth |
| 33 | + | "ethernet" => Ethernet |
| 34 | + | "wimax" => WiMax |
| 35 | + | connection => |
| 36 | + failwith("NetInfo, unexpected `connection` type: " ++ connection) |
| 37 | + }; |
| 38 | + |
| 39 | +let effectiveConnectionType = effectiveConnectionType => |
| 40 | + switch effectiveConnectionType { |
| 41 | + | "2g" => Net2G |
| 42 | + | "3g" => Net3G |
| 43 | + | "4g" => Net4G |
| 44 | + | "unknown" => Unknown |
| 45 | + | connection => |
| 46 | + failwith("NetInfo, unexpected `effectiveConnection` type: " ++ connection) |
| 47 | + }; |
| 48 | + |
| 49 | +[@bs.module "react-native"] [@bs.scope "NetInfo"] |
| 50 | +external addEventListener : |
| 51 | + ([@bs.as "connectionChange"] _, info => unit) => unit = |
| 52 | + ""; |
| 53 | + |
| 54 | +[@bs.module "react-native"] [@bs.scope "NetInfo"] |
| 55 | +external removeEventListener : |
| 56 | + ([@bs.as "connectionChange"] _, info => unit) => unit = |
| 57 | + ""; |
| 58 | + |
| 59 | +[@bs.module "react-native"] [@bs.scope "NetInfo"] |
| 60 | +external isConnectionExpensive : unit => Js.Promise.t(bool) = ""; |
| 61 | + |
| 62 | +[@bs.module "react-native"] [@bs.scope "NetInfo"] |
| 63 | +external getConnectionInfo : unit => Js.Promise.t(info) = ""; |
| 64 | + |
| 65 | +module IsConnected = { |
| 66 | + type t; |
| 67 | + [@bs.module "react-native"] [@bs.scope "NetInfo"] [@bs.val] |
| 68 | + external isConnected : t = ""; |
| 69 | + [@bs.send.pipe : t] |
| 70 | + external _addEventListener : |
| 71 | + ([@bs.as "connectionChange"] _, bool => unit) => unit = |
| 72 | + "addEventListener"; |
| 73 | + [@bs.send.pipe : t] |
| 74 | + external _removeEventListener : |
| 75 | + ([@bs.as "connectionChange"] _, bool => unit) => unit = |
| 76 | + "removeEventListener"; |
| 77 | + [@bs.send.pipe : t] external _fetch : unit => Js.Promise.t(bool) = "fetch"; |
| 78 | + let addEventListener = listener => |
| 79 | + isConnected |> _addEventListener(listener); |
| 80 | + let removeEventListener = listener => |
| 81 | + isConnected |> _removeEventListener(listener); |
| 82 | + let fetch = () => isConnected |> _fetch(); |
| 83 | +}; |
0 commit comments