浏览代码

#219 fix context when client is None

Falko Schindler 2 年之前
父节点
当前提交
527e0430d8
共有 1 个文件被更改,包括 4 次插入3 次删除
  1. 4 3
      nicegui/helpers.py

+ 4 - 3
nicegui/helpers.py

@@ -1,5 +1,6 @@
 import asyncio
 import functools
+from contextlib import nullcontext
 from typing import Any, Awaitable, Callable, Optional, Union
 
 from . import globals
@@ -17,15 +18,15 @@ def safe_invoke(func: Union[Callable, Awaitable], client: Optional[Client] = Non
     try:
         if isinstance(func, Awaitable):
             async def func_with_client():
-                with client:
+                with client or nullcontext():
                     await func
             create_task(func_with_client())
         else:
-            with client:
+            with client or nullcontext():
                 result = func()
             if isinstance(result, Awaitable):
                 async def result_with_client():
-                    with client:
+                    with client or nullcontext():
                         await result
                 create_task(result_with_client())
     except: