MCPcopy Create free account
hub / github.com/ethanniser/the-beth-stack / createElement

Function createElement

packages/beth-stack/src/jsx/index.ts:35–119  ·  view source on GitHub ↗
(
  name: string | Component | typeof Fragment,
  attributes: PropsWithChildren<unknown> | null,
  ...children: Children[]
)

Source from the content-addressed store, hash-verified

33const Fragment: unique symbol = Symbol.for("beth-stack-fragment");
34
35async function createElement(
36 name: string | Component | typeof Fragment,
37 attributes: PropsWithChildren<unknown> | null,
38 ...children: Children[]
39): Promise<string> {
40 children = deepFlatten(children);
41
42 const hasAnyPromiseChildren = children.reduce(
43 (acc, child) => acc || child instanceof Promise,
44 false,
45 );
46 const hasAnyUnresolvedPromiseChildren = children.reduce(
47 (acc, child) => acc || Bun.peek.status(child) !== "fulfilled",
48 false,
49 );
50
51 const insideStreamCall =
52 BETH_GLOBAL_RENDER_CACHE.streamController !== undefined;
53
54 if (
55 name === Suspense &&
56 hasAnyUnresolvedPromiseChildren &&
57 insideStreamCall
58 ) {
59 const id = BETH_GLOBAL_RENDER_CACHE.registerChild(children);
60
61 if (attributes !== null && "fallback" in attributes) {
62 attributes.fallback = `
63 <div id="B:${id}" data-fallback>
64 ${await attributes.fallback}
65 </div>
66 `;
67 }
68 } else if (name !== ErrorBoundary) {
69 if (hasAnyPromiseChildren) {
70 // Converts children to a string if they are promises.
71 children = await Promise.all(children);
72 }
73 }
74
75 // Adds the children to the attributes if it is not present.
76 if (attributes === null) {
77 attributes = { children };
78 }
79
80 // Calls the element creator function if the name is a function
81 if (typeof name === "function") {
82 // In case the children attributes is not present, add it as a property.
83 if (attributes.children === undefined) {
84 attributes.children = children;
85 }
86
87 return name(attributes);
88 }
89
90 if (name === Fragment) {
91 return contentsToString(children);
92 }

Callers

nothing calls this directly

Calls 5

deepFlattenFunction · 0.90
contentsToStringFunction · 0.90
isVoidElementFunction · 0.90
attributesToStringFunction · 0.90
registerChildMethod · 0.80

Tested by

no test coverage detected