Skip to content

Commit 23cb57d

Browse files
committed
Add asyncComponent to jsx
1 parent cc27a5d commit 23cb57d

File tree

6 files changed

+78
-10
lines changed

6 files changed

+78
-10
lines changed

compiler/syntax/src/jsx_common.ml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@ let async_component ~async expr =
5454
let open Ast_helper in
5555
Exp.apply
5656
(Exp.ident
57-
{
58-
loc = Location.none;
59-
txt = Ldot (Lident "JsxPPXReactSupport", "asyncComponent");
60-
})
57+
{loc = Location.none; txt = Ldot (Lident "Jsx", "asyncComponent")})
6158
[(Nolabel, expr)]
6259
else expr

runtime/Jsx.res

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,5 @@ type component<'props> = componentLike<'props, element>
3838

3939
/* this function exists to prepare for making `component` abstract */
4040
external component: componentLike<'props, element> => component<'props> = "%identity"
41+
42+
external asyncComponent: promise<element> => element = "%identity"

tests/syntax_tests/data/ppx/react/expected/asyncAwait.res.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module C0 = {
99
ReactDOM.jsx("div", {children: ?ReactDOM.someElement({React.int(a)})})
1010
}
1111
let make = {
12-
let \"AsyncAwait$C0" = (props: props<_>) => JsxPPXReactSupport.asyncComponent(make(props))
12+
let \"AsyncAwait$C0" = (props: props<_>) => Jsx.asyncComponent(make(props))
1313

1414
\"AsyncAwait$C0"
1515
}
@@ -26,7 +26,7 @@ module C1 = {
2626
}
2727
}
2828
let make = {
29-
let \"AsyncAwait$C1" = (props: props<_>) => JsxPPXReactSupport.asyncComponent(make(props))
29+
let \"AsyncAwait$C1" = (props: props<_>) => Jsx.asyncComponent(make(props))
3030

3131
\"AsyncAwait$C1"
3232
}

tests/syntax_tests/data/ppx/react/expected/sharedPropsWithProps.res.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ module V4A5 = {
4545
ReactDOM.jsx("div", {children: ?ReactDOM.someElement({React.int(a)})})
4646
}
4747
let make = {
48-
let \"SharedPropsWithProps$V4A5" = (props: props<_>) =>
49-
JsxPPXReactSupport.asyncComponent(make(props))
48+
let \"SharedPropsWithProps$V4A5" = (props: props<_>) => Jsx.asyncComponent(make(props))
5049
\"SharedPropsWithProps$V4A5"
5150
}
5251
}
@@ -60,8 +59,7 @@ module V4A6 = {
6059
}
6160
}
6261
let make = {
63-
let \"SharedPropsWithProps$V4A6" = (props: props<_>) =>
64-
JsxPPXReactSupport.asyncComponent(make(props))
62+
let \"SharedPropsWithProps$V4A6" = (props: props<_>) => Jsx.asyncComponent(make(props))
6563
\"SharedPropsWithProps$V4A6"
6664
}
6765
}

tests/tests/src/async_jsx.mjs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
import * as JsxRuntime from "react/jsx-runtime";
4+
5+
function getNow() {
6+
return new Promise((res, param) => {
7+
setTimeout(() => res(new Date()), 1000);
8+
});
9+
}
10+
11+
async function make(param) {
12+
let now = await getNow();
13+
return <div>
14+
<p>
15+
{now.toLocaleString()}
16+
</p>
17+
</div>;
18+
}
19+
20+
let Async_jsx$Foo = make;
21+
22+
let Foo = {
23+
make: Async_jsx$Foo
24+
};
25+
26+
function Async_jsx$Bar(props) {
27+
return <div>
28+
<Async_jsx$Foo />
29+
</div>;
30+
}
31+
32+
let Bar = {
33+
make: Async_jsx$Bar
34+
};
35+
36+
export {
37+
getNow,
38+
Foo,
39+
Bar,
40+
}
41+
/* react/jsx-runtime Not a pure module */

tests/tests/src/async_jsx.res

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
@@config({
2+
flags: ["-bs-jsx", "4", "-bs-jsx-preserve"],
3+
})
4+
5+
let getNow = () => {
6+
Promise.make((res, _) => {
7+
setTimeout(() => {
8+
res(Date.make())
9+
}, 1000)->ignore
10+
})
11+
}
12+
13+
module Foo = {
14+
@react.component
15+
let make = async () => {
16+
let now = await getNow()
17+
<div>
18+
<p> {React.string(now->Date.toLocaleString)} </p>
19+
</div>
20+
}
21+
}
22+
23+
module Bar = {
24+
@react.component
25+
let make = () => {
26+
<div>
27+
<Foo />
28+
</div>
29+
}
30+
}

0 commit comments

Comments
 (0)