Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const require_utils_html = require("../utils/html.js");
const require_jsx_constants = require("./constants.js");
const require_jsx_context = require("./context.js");
const require_jsx_base = require("./base.js");
const require_jsx_dom_render = require("./dom/render.js");
const require_jsx_dom_components = require("./dom/components.js");
const require_jsx_components = require("./components.js");
//#region src/jsx/streaming.ts
/**
* @module
* This module enables JSX to supports streaming Response.
*/
/**
* Used to specify nonce for scripts generated by `Suspense` and `ErrorBoundary`.
*
* @example
* ```tsx
*
* Loading...
}>
*
*
*
* ```
*/
const StreamingContext = require_jsx_context.createContext(null);
let suspenseCounter = 0;
const serializeBoundaryChild = (child) => typeof child === "string" || Array.isArray(child) ? require_jsx_base.renderChildren([child]) : child == null || typeof child === "boolean" ? "" : require_jsx_base.isUntrustedObject(child) ? require_jsx_base.renderUntrustedObject(child) : child.toString();
/**
* @experimental
* `Suspense` is an experimental feature.
* The API might be changed.
*/
const Suspense = async ({ children, fallback }) => {
if (!Array.isArray(children)) children = [children];
const nonce = require_jsx_context.useContext(StreamingContext)?.scriptNonce;
let resArray = [];
const stackNode = { [require_jsx_constants.DOM_STASH]: [0, []] };
const popNodeStack = () => {
require_jsx_dom_render.buildDataStack.pop();
};
try {
stackNode[require_jsx_constants.DOM_STASH][0] = 0;
require_jsx_dom_render.buildDataStack.push([[], stackNode]);
resArray = children.map(serializeBoundaryChild);
} catch (e) {
if (e instanceof Promise) {
const resume = require_jsx_context.captureRenderContext();
resArray = [e.then(() => resume(() => {
stackNode[require_jsx_constants.DOM_STASH][0] = 0;
require_jsx_dom_render.buildDataStack.push([[], stackNode]);
return require_jsx_components.childrenToString(children).finally(popNodeStack);
}))];
} else throw e;
} finally {
popNodeStack();
}
if (resArray.some((res) => res instanceof Promise)) {
const index = suspenseCounter++;
const fallbackStr = await (typeof fallback === "string" || Array.isArray(fallback) ? require_jsx_base.renderChildren([fallback]) : require_jsx_base.isUntrustedObject(fallback) ? require_jsx_base.renderUntrustedObject(fallback) : fallback.toString());
return require_utils_html.raw(`${fallbackStr}`, [...fallbackStr.callbacks || [], ({ phase, buffer, context }) => {
if (phase === require_utils_html.HtmlEscapedCallbackPhase.BeforeStream) return;
return Promise.all(resArray).then(async (htmlArray) => {
htmlArray = htmlArray.flat();
const content = htmlArray.join("");
if (buffer) buffer[0] = buffer[0].replace(new RegExp(`.*?`, "s"), () => content);
let html = buffer ? "" : `${content}