Skip to content

Commit 8015726

Browse files
authored
added emit_async in Signal class
Runs the connected function using async and await
1 parent f6bf87d commit 8015726

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ build-backend = "hatchling.build"
55

66
[project]
77
name = "generalpy"
8-
version = "1.14.0"
8+
version = "1.15.0"
99
authors = [
1010
{ name="sayyid5416" },
1111
]

src/generalpy/signal.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
class Signal:
1717
"""
18-
A signal object that can be used to connect callback.
18+
A signal object that can be used to connect callbacks and emit signals.
1919
- Connect a callback using `.connect(...)` method
2020
- Newer callback will overwrite the older callback
2121
- Emit the signal using `.emit(...)` method to run the connected callback
@@ -115,6 +115,23 @@ def emit(self, *args: Any, **kwargs: Any):
115115
else:
116116
self.returnedValue_from_cb = self._callback(*_args, **_kwargs)
117117

118+
async def emit_async(self, *args: Any, **kwargs: Any):
119+
"""
120+
(`Async`) Emit the signal with the given arguments
121+
- Connected callback will run with `args` and `kwargs` (if present)
122+
- Callback will run using `async` and `await`
123+
- These `args` and `kwargs` be passed along with those which were set in `.connect(...)` method
124+
- These `kwargs` will take precedence (in case of duplication) over those which were set in `.connect(...)` method
125+
- Final Args would be like `(earlier_args, these_args, final_kwargs)`
126+
"""
127+
# Modify
128+
_args = self.cb_args + args
129+
_kwargs = {**self.cb_kwargs, **kwargs}
130+
131+
# Run
132+
if self._callback:
133+
self.returnedValue_from_cb = await self._callback(*args, **kwargs)
134+
118135
def get_returned_value(self):
119136
"""
120137
Returns the returned-value of connected callback

0 commit comments

Comments
 (0)