Repository navigation
Description
What happens
A component can call a slot in template_data() and print the result.
If the result is printed in two places before the components inside it
have rendered, each place would render them. #287 catches this when one
template prints the value twice, and raises a clear error before anything
renders twice:
RuntimeError: This content is printed in more than one place, but the
components inside it have not rendered yet, so each would render twice. ...
It does not catch the value printed once by the component and once by a
child it passes the value to:
from citry import Citry, Component
app = Citry(autodiscover=False)
calls = []
class Row(Component):
citry = app
def template_data(self, kwargs, slots):
calls.append("Row")
return {}
template = """
<li>row</li>
"""
class Show(Component):
citry = app
def template_data(self, kwargs, slots):
return {"body": kwargs["body"]}
template = """
<aside>{{ body }}</aside>
"""
class Twice(Component):
citry = app
def template_data(self, kwargs, slots):
return {"body": slots["default"]()}
template = """
<c-Show c-body="body" />
<div>{{ body }}</div>
"""
class Page(Component):
citry = app
template = """
<c-Twice><c-Row /></c-Twice>
"""
try:
Page().render().serialize(deps_strategy="ignore")
finally:
print(len(calls))2
RuntimeError: deferred part vanished from its .parts list before resolution
Row's template_data() ran twice for one <c-Row />. With the two
lines of Twice's template swapped, Row renders once and
serialization then fails with "The same rendered component id was
encountered more than once during serialization", which is also not the
#287 message. Both runs give the same result on
main and on #287's branch (fix/nested-template-printed-twice).
A render's output is a parts list: strings, plus placeholders for
components that have not rendered yet. The same failure happens when
code copies a render's parts list, so two lists hold the same
placeholders. The test
test_call_run_tasks.py::test_a_row_list_printed_again_after_its_first_row_renders_only_the_unplaced_rows
locks that behavior. That case needs Citry's internal CitryRender
constructor, so users are unlikely to reach it.
Cause
After a component's template runs, the render queue (the loop that renders
child components after their parent's template) scans the component's
output for components still to render, in _scan_deferred_parts
(packages/py/citry/citry/component_render.py:1566 on #287's branch).
#287 makes that scan remember the parts lists it has entered
(walked, :1580) and raise its new error when it reaches one a second
time while components in it are still waiting.
walked lives for one scan, which covers one component's output, and it
compares lists by identity. In the example, Twice's scan queues Row
and Show. From the order dependence above, Show appears to render
before Row, and its own scan reaches the same list again, so it queues
Row a second time. (I did not trace the queue order step by step.)
When the second Row render tries to take its place, the place is
already filled, and
component_render.py:1832 raises the old error. The _scan_deferred
docstring (:1656-1686) states both limits.
Who hits it
Anyone who passes a slot's result, or another value holding components
that have not rendered yet, to a child component and also prints it
themselves. The author sees an internal message, and template_data()
and other callbacks of the inner components run one extra time first.
Fix direction
Build on #287. Keep the set of entered parts lists for the whole render
queue run, not for one scan, so the second component's scan finds the
list and raises #287's error, naming the second component's path.
Release the set when the queue finishes. This alone does not cover the
swapped order: there Row has already rendered when Show's scan
reaches the list, and #287 skips a list whose components have all
rendered, so the serialization error would remain. The second visit from
another component's output needs to raise even then. Copied lists need a different
key: the placeholders themselves. Track the placeholder objects that are
already queued and raise the same error when one is queued again. Add
tests for both orders of the example and update the locked test above to
expect the new error.
Activity
fix: catch content printed by a component and its child before its co…
fix: catch content printed by a component and its child before its co…
fix: say what to do when one rendered output is printed in two places