Skip to content

Commit

Permalink
convert async component type
Browse files Browse the repository at this point in the history
  • Loading branch information
mununki committed Sep 12, 2023
1 parent 6b69ccd commit b0a1504
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 1 deletion.
4 changes: 4 additions & 0 deletions jscomp/others/jsxPPXReactSupportC.res
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@ let createElementWithKey = (~key=?, component, props) =>

let createElementVariadicWithKey = (~key=?, component, props, elements) =>
createElementVariadic(component, addKeyProp(~key?, props), elements)

external asyncComponent: ('props => promise<Jsx.element>) => Jsx.component<
'props,
> = "%identity"
4 changes: 4 additions & 0 deletions jscomp/others/jsxPPXReactSupportU.res
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,7 @@ let createElementWithKey = (~key=?, component, props) =>

let createElementVariadicWithKey = (~key=?, component, props, elements) =>
createElementVariadic(component, addKeyProp(~key?, props), elements)

external asyncComponent: ('props => promise<Jsx.element>) => Jsx.component<
'props,
> = "%identity"
10 changes: 10 additions & 0 deletions jscomp/syntax/src/react_jsx_common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,13 @@ let add_async_attribute ~async (body : Parsetree.expression) =

let is_async : Parsetree.attribute -> bool =
fun ({txt}, _) -> txt = "async" || txt = "res.async"

let async_component expr =
let open Ast_helper in
Exp.apply
(Exp.ident
{
loc = Location.none;
txt = Ldot (Lident "JsxPPXReactSupport", "asyncComponent");
})
[(Nolabel, expr)]
11 changes: 11 additions & 0 deletions jscomp/syntax/src/reactjs_jsx_v4.ml
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,17 @@ let mapBinding ~config ~emptyLoc ~pstr_loc ~fileName ~recFlag binding =
|> Ast_uncurried.uncurriedFun ~loc:fullExpression.pexp_loc ~arity:1
else fullExpression
in
let fullExpression =
if isAsync then
Exp.apply
(Exp.ident
{
loc = Location.none;
txt = Ldot (Lident "JsxPPXReactSupport", "asyncComponent");
})
[(Nolabel, fullExpression)]
else fullExpression
in
let fullExpression =
match fullModuleName with
| "" -> fullExpression
Expand Down
2 changes: 1 addition & 1 deletion jscomp/syntax/tests/ppx/react/expected/asyncAwait.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ let make = async ({a, _}: props<_>) => {
ReactDOM.jsx("div", {children: ?ReactDOM.someElement({React.int(a)})})
}
let make = {
let \"AsyncAwait" = (props: props<_>) => make(props)
let \"AsyncAwait" = JsxPPXReactSupport.asyncComponent((props: props<_>) => make(props))

\"AsyncAwait"
}

0 comments on commit b0a1504

Please sign in to comment.