Skip to content

Commit fc71864

Browse files
committed
Add docstrings to the layout handler
1 parent b5b8862 commit fc71864

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/reactpy_django/pyscript/layout_handler.py

+12
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,23 @@ def __init__(self, uuid):
2222

2323
@staticmethod
2424
def apply_update(update, root_model):
25+
"""Apply an update ReactPy's internal DOM model."""
2526
if update["path"]:
2627
set_pointer(root_model, update["path"], update["model"])
2728
else:
2829
root_model.update(update["model"])
2930

3031
def render(self, layout, model):
32+
"""Submit ReactPy's internal DOM model into the HTML DOM."""
3133
container = js.document.getElementById(f"pyscript-{self.uuid}")
34+
35+
# FIXME: The current implementation completely recreates the DOM on every render.
36+
# This is not ideal, and should be optimized in the future.
3237
container.innerHTML = ""
3338
self.build_element_tree(layout, container, model)
3439

3540
def build_element_tree(self, layout, parent, model):
41+
"""Recursively build an element tree, starting from the root component."""
3642
if isinstance(model, str):
3743
parent.appendChild(js.document.createTextNode(model))
3844
elif isinstance(model, dict):
@@ -66,6 +72,8 @@ def build_element_tree(self, layout, parent, model):
6672

6773
@staticmethod
6874
def create_event_handler(layout, element, event_name, event_handler_model):
75+
"""Create an event handler for an element. This function is used as an
76+
adapter between ReactPy and browser events."""
6977
target = event_handler_model["target"]
7078

7179
def event_handler(*args):
@@ -78,6 +86,9 @@ def event_handler(*args):
7886

7987
@staticmethod
8088
def delete_old_workspaces():
89+
"""To prevent memory leaks, we must delete all user generated Python code
90+
whe it is no longer on the page. To do this, we compare what UUIDs exist on
91+
the DOM, versus what UUIDs exist within the PyScript global interpreter."""
8192
dom_workspaces = js.document.querySelectorAll(".pyscript")
8293
dom_uuids = {element.dataset.uuid for element in dom_workspaces}
8394
python_uuids = {
@@ -105,6 +116,7 @@ def delete_old_workspaces():
105116
)
106117

107118
async def run(self, workspace_function: Callable[[], ComponentType]):
119+
"""Run the layout handler. This function is main executor for all user generated code."""
108120
self.delete_old_workspaces()
109121
root_model: dict = {}
110122

0 commit comments

Comments
 (0)