Utils: Remove unused defer utils
These did not work anyways Signed-off-by: kingbri <8082010+kingbri1@users.noreply.github.com>
This commit is contained in:
parent
b751e0a1d5
commit
027ffce05d
1 changed files with 0 additions and 50 deletions
|
|
@ -87,53 +87,3 @@ def unwrap_optional_type(type_hint) -> Type:
|
|||
|
||||
return type_hint
|
||||
|
||||
|
||||
def with_defer(func):
|
||||
"""
|
||||
Decorator for a go-style defer
|
||||
"""
|
||||
|
||||
def wrapper(*args, **kwargs):
|
||||
deferred_calls = []
|
||||
|
||||
# This 'defer' function is what you'll call inside your decorated function
|
||||
def defer(fn, *fn_args, **fn_kwargs):
|
||||
deferred_calls.append((fn, fn_args, fn_kwargs))
|
||||
|
||||
try:
|
||||
# Inject 'defer' into the kwargs of the original function
|
||||
return func(*args, defer=defer, **kwargs)
|
||||
finally:
|
||||
# After the original function finishes (or raises), run deferred calls
|
||||
for fn, fn_args, fn_kwargs in reversed(deferred_calls):
|
||||
fn(*fn_args, **fn_kwargs)
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
def with_defer_async(func):
|
||||
"""
|
||||
Decorator for running async functions in go-style defer blocks
|
||||
"""
|
||||
|
||||
async def wrapper(*args, **kwargs):
|
||||
deferred_calls = []
|
||||
|
||||
# This 'defer' function is what you'll call inside your decorated function
|
||||
def defer(fn, *fn_args, **fn_kwargs):
|
||||
deferred_calls.append((fn, fn_args, fn_kwargs))
|
||||
|
||||
try:
|
||||
# Inject 'defer' into the kwargs of the original function
|
||||
return await func(*args, defer=defer, **kwargs)
|
||||
finally:
|
||||
# After the original function finishes (or raises), run deferred calls
|
||||
for fn, fn_args, fn_kwargs in reversed(deferred_calls):
|
||||
if inspect.iscoroutinefunction(fn):
|
||||
await fn(*fn_args, **fn_kwargs)
|
||||
elif inspect.iscoroutine(fn):
|
||||
await fn
|
||||
else:
|
||||
fn(*fn_args, **fn_kwargs)
|
||||
|
||||
return wrapper
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue