@@ -22,17 +22,23 @@ def __init__(self, uuid):
22
22
23
23
@staticmethod
24
24
def apply_update (update , root_model ):
25
+ """Apply an update ReactPy's internal DOM model."""
25
26
if update ["path" ]:
26
27
set_pointer (root_model , update ["path" ], update ["model" ])
27
28
else :
28
29
root_model .update (update ["model" ])
29
30
30
31
def render (self , layout , model ):
32
+ """Submit ReactPy's internal DOM model into the HTML DOM."""
31
33
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.
32
37
container .innerHTML = ""
33
38
self .build_element_tree (layout , container , model )
34
39
35
40
def build_element_tree (self , layout , parent , model ):
41
+ """Recursively build an element tree, starting from the root component."""
36
42
if isinstance (model , str ):
37
43
parent .appendChild (js .document .createTextNode (model ))
38
44
elif isinstance (model , dict ):
@@ -66,6 +72,8 @@ def build_element_tree(self, layout, parent, model):
66
72
67
73
@staticmethod
68
74
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."""
69
77
target = event_handler_model ["target" ]
70
78
71
79
def event_handler (* args ):
@@ -78,6 +86,9 @@ def event_handler(*args):
78
86
79
87
@staticmethod
80
88
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."""
81
92
dom_workspaces = js .document .querySelectorAll (".pyscript" )
82
93
dom_uuids = {element .dataset .uuid for element in dom_workspaces }
83
94
python_uuids = {
@@ -105,6 +116,7 @@ def delete_old_workspaces():
105
116
)
106
117
107
118
async def run (self , workspace_function : Callable [[], ComponentType ]):
119
+ """Run the layout handler. This function is main executor for all user generated code."""
108
120
self .delete_old_workspaces ()
109
121
root_model : dict = {}
110
122
0 commit comments