"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var streaming_exports = {};
__export(streaming_exports, {
Suspense: () => Suspense,
renderToReadableStream: () => renderToReadableStream
});
module.exports = __toCommonJS(streaming_exports);
var import_html = require("../helper/html");
var import_html2 = require("../utils/html");
var import_components = require("./components");
var import_constants = require("./constants");
var import_components2 = require("./dom/components");
var import_render = require("./dom/render");
let suspenseCounter = 0;
const Suspense = async ({
children,
fallback
}) => {
if (!children) {
return fallback.toString();
}
if (!Array.isArray(children)) {
children = [children];
}
let resArray = [];
const stackNode = { [import_constants.DOM_STASH]: [0, []] };
const popNodeStack = (value) => {
import_render.buildDataStack.pop();
return value;
};
try {
stackNode[import_constants.DOM_STASH][0] = 0;
import_render.buildDataStack.push([[], stackNode]);
resArray = children.map(
(c) => c == null || typeof c === "boolean" ? "" : c.toString()
);
} catch (e) {
if (e instanceof Promise) {
resArray = [
e.then(() => {
stackNode[import_constants.DOM_STASH][0] = 0;
import_render.buildDataStack.push([[], stackNode]);
return (0, import_components.childrenToString)(children).then(popNodeStack);
})
];
} else {
throw e;
}
} finally {
popNodeStack();
}
if (resArray.some((res) => res instanceof Promise)) {
const index = suspenseCounter++;
const fallbackStr = await fallback.toString();
return (0, import_html.raw)(`${fallbackStr}`, [
...fallbackStr.callbacks || [],
({ phase, buffer, context }) => {
if (phase === import_html2.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(`.*?`),
content
);
}
let html = buffer ? "" : `${content}