Răsfoiți Sursa

#219 fix context when client is None

Falko Schindler 2 ani în urmă
părinte
comite
527e0430d8
1 a modificat fișierele cu 4 adăugiri și 3 ștergeri
  1. 4 3
      nicegui/helpers.py

+ 4 - 3
nicegui/helpers.py

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