only render once

This commit is contained in:
Khaleel Al-Adhami 2024-10-22 19:31:38 -07:00
parent 67b8eb9b15
commit c7a234b94d

View File

@ -2365,24 +2365,15 @@ def empty_component() -> Component:
return Bare.create("") return Bare.create("")
@dataclasses.dataclass( def render_dict_to_var(tag: dict) -> Var:
eq=False, """Convert a render dict to a Var.
frozen=True,
)
class LiteralComponentVar(CachedVarOperation, LiteralVar, ComponentVar):
"""A Var that represents a Component."""
_var_value: Component = dataclasses.field(default_factory=empty_component) Args:
tag: The render dict.
@cached_property_no_lock
def _cached_var_name(self) -> str:
"""Get the name of the var.
Returns: Returns:
The name of the var. The Var.
""" """
tag = self._var_value.render()
props = {} props = {}
special_props = [] special_props = []
@ -2403,18 +2394,35 @@ class LiteralComponentVar(CachedVarOperation, LiteralVar, ComponentVar):
contents = tag["contents"][1:-1] if tag["contents"] else None contents = tag["contents"][1:-1] if tag["contents"] else None
tag_name = Var(tag.get("name", None) or "Fragment") tag_name = Var(tag.get("name") or "Fragment")
return str( return FunctionStringVar.create(
FunctionStringVar.create(
"jsx", "jsx",
).call( ).call(
tag_name, tag_name,
props, props,
*([Var(contents)] if contents is not None else []), *([Var(contents)] if contents is not None else []),
*[Var.create(child) for child in self._var_value.children], *[render_dict_to_var(child) for child in tag["children"]],
) )
@dataclasses.dataclass(
eq=False,
frozen=True,
) )
class LiteralComponentVar(CachedVarOperation, LiteralVar, ComponentVar):
"""A Var that represents a Component."""
_var_value: Component = dataclasses.field(default_factory=empty_component)
@cached_property_no_lock
def _cached_var_name(self) -> str:
"""Get the name of the var.
Returns:
The name of the var.
"""
return str(render_dict_to_var(self._var_value.render()))
@cached_property_no_lock @cached_property_no_lock
def _cached_get_all_var_data(self) -> VarData | None: def _cached_get_all_var_data(self) -> VarData | None: