Przeglądaj źródła

introduce clear() method on group elements that remove related binding links

Falko Schindler 3 lat temu
rodzic
commit
a16296e862
3 zmienionych plików z 12 dodań i 11 usunięć
  1. 1 1
      main.py
  2. 0 9
      nicegui/binding.py
  3. 11 1
      nicegui/elements/group.py

+ 1 - 1
main.py

@@ -9,7 +9,7 @@ import re
 import asyncio
 from nicegui.elements.markdown import Markdown
 from nicegui.elements.element import Element
-from nicegui.events import ClickEventArguments, KeyEventArguments
+from nicegui.events import KeyEventArguments
 from nicegui.globals import page_stack
 
 # add docutils css to webpage

+ 0 - 9
nicegui/binding.py

@@ -2,19 +2,15 @@
 import asyncio
 from collections import defaultdict
 from justpy.htmlcomponents import HTMLBaseComponent
-import time
-import logging
 
 bindings = defaultdict(list)
 bindable_properties = set()
 active_links = []
 
 async def loop():
-    global active_links
     while True:
         visited = set()
         invalidated_views = []
-        to_delete = []
         for link in active_links:
             (source_obj, source_name, target_obj, target_name, transform) = link
             value = transform(getattr(source_obj, source_name))
@@ -23,11 +19,6 @@ async def loop():
                 propagate(target_obj, target_name, visited)
                 if hasattr(target_obj, 'view') and isinstance(target_obj.view, HTMLBaseComponent):
                     invalidated_views.append(target_obj.view)
-            # remove links if the justpy element has been deleted
-            if getattr(target_obj, 'delete_flag', False) or getattr(source_obj, 'delete_flag', False):
-                to_delete.append(link)
-        active_links = [l for l in active_links if l not in to_delete]
-
         for view in invalidated_views:
             await view.update()
         await asyncio.sleep(0.1)

+ 11 - 1
nicegui/elements/group.py

@@ -1,6 +1,8 @@
 from __future__ import annotations
-from .element import Element
+import justpy as jp
 from ..globals import view_stack
+from ..binding import active_links
+from .element import Element
 
 class Group(Element):
 
@@ -13,3 +15,11 @@ class Group(Element):
 
     def tight(self) -> Group:
         return self.classes(replace='').style(replace='')
+
+    def clear(self):
+        def collect_components(view: jp.HTMLBaseComponent) -> list[jp.HTMLBaseComponent]:
+            return view.components + [view for child in view.components for view in collect_components(child)]
+        components = collect_components(self.view)
+        obsolete_links = [link for link in active_links if link[0] in components or link[2] in components]
+        active_links[:] = [l for l in active_links if l not in obsolete_links]
+        self.view.delete_components()