浏览代码

Merge branch 'memory_debug'

Falko Schindler 2 年之前
父节点
当前提交
9fabb42ac4
共有 3 个文件被更改,包括 34 次插入34 次删除
  1. 8 32
      nicegui/elements/table.js
  2. 8 2
      nicegui/functions/timer.py
  3. 18 0
      nicegui/nicegui.py

+ 8 - 32
nicegui/elements/table.js

@@ -1,17 +1,17 @@
 export default {
   template: "<div></div>",
   mounted() {
-    this.gridOptions = {
-      ...this.options,
-      onGridReady: (params) => params.api.sizeColumnsToFit(),
-    };
-    this.grid = new agGrid.Grid(this.$el, this.gridOptions);
-    this.gridOptions.api.addGlobalListener(this.handle_event);
+    this.update_grid();
   },
   methods: {
     update_grid() {
-      replaceObject(this.options, this.gridOptions);
-      this.gridOptions.api.refreshCells();
+      this.$el.textContent = "";
+      this.gridOptions = {
+        ...this.options,
+        onGridReady: (params) => params.api.sizeColumnsToFit(),
+      };
+      this.grid = new agGrid.Grid(this.$el, this.gridOptions);
+      this.gridOptions.api.addGlobalListener(this.handle_event);
     },
     call_api_method(name, ...args) {
       this.gridOptions.api[name](...args);
@@ -55,27 +55,3 @@ export default {
     options: Object,
   },
 };
-
-function replaceArray(source, target) {
-  for (let i = 0; i < source.length; i++) {
-    if (typeof source[i] === "object") {
-      replaceObject(source[i], target[i]);
-    } else if (typeof source[i] === "array") {
-      replaceArray(source[i], target[i]);
-    } else {
-      target[i] = source[i];
-    }
-  }
-}
-
-function replaceObject(source, target) {
-  for (let key in source) {
-    if (typeof source[key] === "object") {
-      replaceObject(source[key], target[key]);
-    } else if (typeof source[key] === "array") {
-      replaceArray(source[key], target[key]);
-    } else {
-      target[key] = source[key];
-    }
-  }
-}

+ 8 - 2
nicegui/functions/timer.py

@@ -42,12 +42,13 @@ class Timer:
         with self.slot:
             await asyncio.sleep(self.interval)
             await self._invoke_callback()
+        self.cleanup()
 
     async def _run_in_loop(self) -> None:
         with self.slot:
             while True:
                 if self.slot.parent.client.id not in globals.clients:
-                    return
+                    break
                 try:
                     start = time.time()
                     if self.active:
@@ -55,10 +56,11 @@ class Timer:
                     dt = time.time() - start
                     await asyncio.sleep(self.interval - dt)
                 except asyncio.CancelledError:
-                    return
+                    break
                 except:
                     traceback.print_exc()
                     await asyncio.sleep(self.interval)
+        self.cleanup()
 
     async def _invoke_callback(self) -> None:
         try:
@@ -67,3 +69,7 @@ class Timer:
                 await AsyncUpdater(result)
         except Exception:
             traceback.print_exc()
+
+    def cleanup(self) -> None:
+        self.slot = None
+        self.callback = None

+ 18 - 0
nicegui/nicegui.py

@@ -54,6 +54,7 @@ def handle_startup(with_welcome_message: bool = True) -> None:
         safe_invoke(t)
     create_task(binding.loop())
     create_task(prune_clients())
+    create_task(prune_slot_stacks())
     globals.state = globals.State.STARTED
     if with_welcome_message:
         print(f'NiceGUI ready to go on http://{globals.host}:{globals.port}')
@@ -148,6 +149,23 @@ async def prune_clients() -> None:
         await asyncio.sleep(10)
 
 
+async def prune_slot_stacks() -> None:
+    while True:
+        running = [
+            id(task)
+            for task in asyncio.tasks.all_tasks()
+            if not task.done() and not task.cancelled()
+        ]
+        stale = [
+            id_
+            for id_ in globals.slot_stacks
+            if id_ not in running
+        ]
+        for id_ in stale:
+            del globals.slot_stacks[id_]
+        await asyncio.sleep(10)
+
+
 def delete_client(id: str) -> None:
     binding.remove(list(globals.clients[id].elements.values()), Element)
     del globals.clients[id]