Przeglądaj źródła

integrate binding loop

Falko Schindler 2 lat temu
rodzic
commit
3dc004e91d
4 zmienionych plików z 13 dodań i 3 usunięć
  1. 2 1
      nicegui/binding.py
  2. 1 0
      nicegui/globals.py
  3. 3 1
      nicegui/nicegui.py
  4. 7 1
      nicegui/run.py

+ 2 - 1
nicegui/binding.py

@@ -23,7 +23,7 @@ async def loop():
                 propagate(target_obj, target_name, visited)
         if time.time() - t > 0.01:
             logging.warning(f'binding propagation for {len(active_links)} active links took {time.time() - t:.3f} s')
-        await asyncio.sleep(globals.config.binding_refresh_interval)
+        await asyncio.sleep(globals.binding_refresh_interval)
 
 
 def propagate(source_obj: Any, source_name: str, visited: Set[Tuple[int, str]] = None) -> None:
@@ -77,6 +77,7 @@ class BindableProperty:
             return
         setattr(owner, '___' + self.name, value)
         bindable_properties[(id(owner), self.name)] = owner
+        propagate(owner, self.name)
         if value_changed and self.on_change is not None:
             self.on_change(owner, value)
 

+ 1 - 0
nicegui/globals.py

@@ -15,6 +15,7 @@ log: logging.Logger = logging.getLogger('nicegui')
 
 host: str
 port: int
+binding_refresh_interval: float
 
 client_stack: List['Client'] = []
 clients: Dict[int, 'Client'] = {}

+ 3 - 1
nicegui/nicegui.py

@@ -6,8 +6,9 @@ from fastapi import FastAPI
 from fastapi.middleware.gzip import GZipMiddleware
 from fastapi_socketio import SocketManager
 
-from . import globals, vue
+from . import binding, globals, vue
 from .client import Client
+from .task_logger import create_task
 
 globals.app = app = FastAPI(routes=vue.generate_js_routes())
 globals.sio = sio = SocketManager(app=app)._sio
@@ -25,6 +26,7 @@ def index():
 @app.on_event('startup')
 def on_startup() -> None:
     globals.loop = asyncio.get_running_loop()
+    create_task(binding.loop())
 
 
 @sio.on('connect')

+ 7 - 1
nicegui/run.py

@@ -6,9 +6,15 @@ import uvicorn
 from . import globals
 
 
-def run(*, host: str = '0.0.0.0', port: int = 5000, reload: bool = True) -> None:
+def run(*,
+        host: str = '0.0.0.0',
+        port: int = 5000,
+        reload: bool = True,
+        binding_refresh_interval: float = 0.1,
+        ) -> None:
     globals.host = host
     globals.port = port
+    globals.binding_refresh_interval = binding_refresh_interval
 
     if inspect.stack()[-2].filename.endswith('spawn.py'):
         return