Skip to content

Commit 367ecac

Browse files
committed
Add NetInfo bindings
1 parent 640f3b9 commit 367ecac

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

src/netInfo.re

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
};

src/netInfo.rei

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
type infoType;
2+
3+
type infoEffectiveType;
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+
"_type": infoType,
23+
"effectiveType": infoEffectiveType
24+
};
25+
26+
let connectionType: infoType => connectionType;
27+
28+
let effectiveConnectionType: infoEffectiveType => effectiveConnectionType;
29+
30+
let addEventListener: (info => unit) => unit;
31+
32+
let removeEventListener: (info => unit) => unit;
33+
34+
let isConnectionExpensive: unit => Js.Promise.t(bool);
35+
36+
let getConnectionInfo: unit => Js.Promise.t(info);
37+
38+
module IsConnected: {
39+
let addEventListener: (bool => unit) => unit;
40+
let removeEventListener: (bool => unit) => unit;
41+
let fetch: unit => Js.Promise.t(bool);
42+
};

0 commit comments

Comments
 (0)