Skip to content

Commit 6855d50

Browse files
committed
Support non-standard Platform.OS identifiers & iOS idioms
1 parent b1721fc commit 6855d50

File tree

3 files changed

+129
-38
lines changed

3 files changed

+129
-38
lines changed

lib/js/src/platform.js

Lines changed: 70 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/platform.re

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,53 @@
1+
type iosIdiom =
2+
| Phone
3+
| Pad
4+
| TV;
5+
6+
[@bs.module "react-native"] [@bs.scope "Platform"]
7+
external _ios_isPad : bool = "isPad";
8+
9+
[@bs.module "react-native"] [@bs.scope "Platform"]
10+
external _ios_isTVOS : bool = "isTVOS";
11+
112
type os =
2-
| IOS
13+
| IOS(iosIdiom)
314
| Android;
415

5-
[@bs.scope "Platform"] [@bs.module "react-native"] external _os : string = "OS";
16+
[@bs.module "react-native"] [@bs.scope "Platform"]
17+
external _os : string = "OS";
618

7-
let os =
19+
exception UnknownPlatform(string);
20+
21+
let os = () =>
822
switch _os {
9-
| "ios" => IOS
10-
| _ => Android
23+
| "ios" =>
24+
switch _ios_isPad {
25+
| true => IOS(Pad)
26+
| _ =>
27+
switch _ios_isTVOS {
28+
| true => IOS(TV)
29+
| _ => IOS(Phone)
30+
}
31+
}
32+
| "android" => Android
33+
| x => raise(UnknownPlatform(x))
1134
};
1235

13-
let equals = (targetOs) =>
14-
switch (os, targetOs) {
15-
| (IOS, IOS) => true
36+
let equals = targetOs =>
37+
switch (os(), targetOs) {
38+
| (IOS(_), IOS(_)) => true
1639
| (Android, Android) => true
17-
| (IOS, Android) => false
18-
| (Android, IOS) => false
40+
| exception (UnknownPlatform(_)) => false
41+
| _ => false
1942
};
2043

21-
[@bs.scope "Platform"] [@bs.module "react-native"] external version : int = "Version";
44+
[@bs.module "react-native"] [@bs.scope "Platform"]
45+
external _version : Js.undefined(int) = "Version";
2246

23-
[@bs.scope "Platform"] [@bs.module "react-native"]
24-
external _select : {. "ios": 'a, "android": 'a} => 'a =
25-
"select";
47+
exception UnknownVersion;
2648

27-
let select = (~ios, ~android) => _select({"ios": ios, "android": android});
49+
let version = () =>
50+
switch (Js.Undefined.to_opt(_version)) {
51+
| Some(v) => v
52+
| None => raise(UnknownVersion)
53+
};

src/platform.rei

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
1+
type iosIdiom =
2+
| Phone
3+
| Pad
4+
| TV;
5+
16
type os =
2-
| IOS
7+
| IOS(iosIdiom)
38
| Android;
49

5-
let os: os;
10+
exception UnknownPlatform(string);
11+
12+
/**
13+
Raises UnknownPlatform for non-standard platforms such as "web"
14+
from react-native-web
15+
*/
16+
let os: unit => os;
617

718
let equals: os => bool;
819

9-
[@bs.scope "Platform"] [@bs.module "react-native"] external version : int = "Version";
20+
exception UnknownVersion;
1021

11-
let select: (~ios: 'a, ~android: 'a) => 'a;
22+
/**
23+
Raises UnknownVersion if version is undefined, i.e. in react-native-web
24+
*/
25+
let version: unit => int;

0 commit comments

Comments
 (0)