Skip to content

Commit

Permalink
add renderSyncToString API
Browse files Browse the repository at this point in the history
  • Loading branch information
zth committed Jan 3, 2024
1 parent fce9354 commit ffa11a5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# master

- Add `renderSyncToString` API.

# 0.1.0-beta.2

- Bind basic version of `hxTarget`.
Expand Down
5 changes: 5 additions & 0 deletions src/H.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/H.res
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ external array: array<Jsx.element> => Jsx.element = "%identity"
@module("./vendor/hyperons.js")
external renderToString: Jsx.element => promise<string> = "render"

/** Renders a subtree to content. Throws if the subtree is asynchronous. */
@module("./vendor/hyperons.js")
@raises(Js.Exn.t)
external renderSyncToString: Jsx.element => string = "renderSync"

@module("./vendor/hyperons.js")
external renderToStream: (Jsx.element, ~onChunk: string => unit=?) => promise<unit> = "render"

Expand Down
10 changes: 10 additions & 0 deletions src/vendor/hyperons.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,15 @@ async function render(element, onChunk) {
return res;
}
}
function renderSync(element) {
const controller = makeController();
renderToString(element, {}, controller);
if (controller.hasAsync) {
throw new Error("Tried to render async tree sync.");
} else {
return controller.content.join("");
}
}
function useContext(instance) {
return instance.getChildContext(dispatcher.context);
}
Expand All @@ -299,6 +308,7 @@ export {
createContext,
createElement as h,
render,
renderSync,
useContext,
escapeString,
};

0 comments on commit ffa11a5

Please sign in to comment.