Skip to content

Commit 1f562b6

Browse files
MoOxcknitt
authored andcommitted
Add bindings for requestAnimationFrame/cancelAnimationFrame (#399)
Closes #393
1 parent 8e1dfba commit 1f562b6

7 files changed

Lines changed: 78 additions & 5 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/* This output is empty. Its source's type definitions, externals and/or unused code got optimized away. */
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
id: apis/AnimationFrame
3+
title: AnimationFrame
4+
autoLinkToOfficialDoc: false
5+
---
6+
7+
## `request`
8+
9+
`AnimationFrame.request` is just the well known
10+
[`requestAnimationFrame`](https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame).
11+
12+
[In React Native, `requestAnimationFrame` has been polyfilled](https://facebook.github.io/react-native/docs/timers#timers)
13+
from the browser. It accepts a function as its only argument and calls that
14+
function before the next repaint.
15+
16+
Note that calback doesn't receive an argument unlike the
17+
`window.requestAnimationFrame` implementation.
18+
19+
This method returns a `requestID` so you can cancel if needed. If you don't need
20+
it, you can just `ignore` the result.
21+
22+
```reason
23+
AnimationFrame.request(() => {
24+
// do whatever you need on next paint
25+
})->ignore;
26+
```
27+
28+
## `cancel`
29+
30+
`AnimationFrame.cancel` method cancels an animation frame request previously
31+
scheduled. It's just calling
32+
[`cancelAnimationFrame`](https://developer.mozilla.org/en-US/docs/Web/API/window/cancelAnimationFrame).
33+
34+
```reason
35+
let requestID = AnimationFrame.request(() => {
36+
// do whatever you need on next paint
37+
});
38+
39+
// elsewhere or later
40+
if (whatever) {
41+
AnimationFrame.cancel(requestID);
42+
};
43+
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type requestID;
2+
3+
[@bs.val]
4+
external request: (unit => unit) => requestID = "requestAnimationFrame";
5+
[@bs.val] external cancel: requestID => unit = "cancelAnimationFrame";

‎website/scripts/PrepareMdToJson.re‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ Path.join([|sourcePath, "**/*.md"|])
148148
"id": front##attributes##id,
149149
"title": front##attributes##title,
150150
"wip": front##attributes##wip,
151+
"autoLinkToOfficialDoc":
152+
front##attributes##autoLinkToOfficialDoc,
151153
"body": Remarkable.render(remarkable, front##body),
152154
}
153155
->Js.Json.stringifyAny;

‎website/scripts/Prerender.re‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ let file = (file: FsUtils.t, modulesIndex: array(string)) => {
7676
}
7777
| None => None // failwith("Expected an `wip` property")
7878
};
79+
let autoLinkToOfficialDoc =
80+
switch (Js.Dict.get(value, "autoLinkToOfficialDoc")) {
81+
| Some(v) =>
82+
switch (Js.Json.classify(v)) {
83+
| Js.Json.JSONTrue => Some(true)
84+
| Js.Json.JSONFalse => Some(false)
85+
| _ => failwith("Expected a boolean for `autoLinkToOfficialDoc`")
86+
}
87+
| None => None // failwith("Expected an `autoLinkToOfficialDoc` property")
88+
};
7989
let body =
8090
switch (Js.Dict.get(value, "body")) {
8191
| Some(v) =>
@@ -85,7 +95,14 @@ let file = (file: FsUtils.t, modulesIndex: array(string)) => {
8595
}
8696
| None => failwith("Expected an `body` property")
8797
};
88-
let p: PageContent.pageData = {id, title, wip, body, modulesIndex};
98+
let p: PageContent.pageData = {
99+
id,
100+
title,
101+
wip,
102+
autoLinkToOfficialDoc,
103+
body,
104+
modulesIndex,
105+
};
89106
p;
90107
| _ => failwith("Expected an object for pageData")
91108
};

‎website/src/Consts.re‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ let sections = [
133133
("Alert", api ++ "Alert/"),
134134
("AlertIOS", api ++ "AlertIOS/"),
135135
("Animated", api ++ "Animated/"),
136+
("AnimationFrame", api ++ "AnimationFrame/"),
136137
("AppRegistry", api ++ "AppRegistry/"),
137138
("AppState", api ++ "AppState/"),
138139
("AsyncStorage", api ++ "AsyncStorage/"),

‎website/src/components/PageContent.re‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
open Belt;
22
open ReactNative;
33

4+
// ⚠️ if you update this, also update PrepareMdToJson if needed
45
type pageData = {
56
id: string,
67
title: string,
78
wip: option(bool),
9+
autoLinkToOfficialDoc: option(bool),
810
body: string,
911
modulesIndex: array(string),
1012
};
@@ -68,10 +70,12 @@ let make = (~pageData) => {
6870
Js.log3("@todo", pageData.title, "docs is still WIP");
6971
};
7072
let officialDocHref =
71-
if (pageData.id
72-
|> Js.String.startsWith("apis/")
73-
|| pageData.id
74-
|> Js.String.startsWith("components/")) {
73+
if (!pageData.autoLinkToOfficialDoc->Option.getWithDefault(true)) {
74+
None;
75+
} else if (pageData.id
76+
|> Js.String.startsWith("apis/")
77+
|| pageData.id
78+
|> Js.String.startsWith("components/")) {
7579
Some(
7680
"http://facebook.github.io/react-native/docs/"
7781
++ pageData.title->Js.String.toLocaleLowerCase

0 commit comments

Comments
 (0)