-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix tests * Improve coverage * Add changenotes * Update doc
- Loading branch information
Showing
13 changed files
with
82 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Signal handlers (registered callbacks) should be coroutines. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Drop undocumented `app.on_pre_signal` and `app.on_post_signal`. Signal | ||
handlers should be coroutines, support for regular functions is | ||
dropped. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,32 @@ | ||
import asyncio | ||
from itertools import count | ||
|
||
from aiohttp.frozenlist import FrozenList | ||
|
||
|
||
class BaseSignal(FrozenList): | ||
|
||
__slots__ = () | ||
|
||
async def _send(self, *args, **kwargs): | ||
for receiver in self: | ||
res = receiver(*args, **kwargs) | ||
if asyncio.iscoroutine(res) or asyncio.isfuture(res): | ||
await res | ||
|
||
|
||
class Signal(BaseSignal): | ||
class Signal(FrozenList): | ||
"""Coroutine-based signal implementation. | ||
To connect a callback to a signal, use any list method. | ||
Signals are fired using the :meth:`send` coroutine, which takes named | ||
Signals are fired using the send() coroutine, which takes named | ||
arguments. | ||
""" | ||
|
||
__slots__ = ('_app', '_name', '_pre', '_post') | ||
__slots__ = ('_owner',) | ||
|
||
def __init__(self, app): | ||
def __init__(self, owner): | ||
super().__init__() | ||
self._app = app | ||
klass = self.__class__ | ||
self._name = klass.__module__ + ':' + klass.__qualname__ | ||
self._pre = app.on_pre_signal | ||
self._post = app.on_post_signal | ||
self._owner = owner | ||
|
||
def __repr__(self): | ||
return '<Signal owner={}, frozen={}, {!r}>'.format(self._owner, | ||
self.frozen, | ||
list(self)) | ||
|
||
async def send(self, *args, **kwargs): | ||
""" | ||
Sends data to all registered receivers. | ||
""" | ||
if self: | ||
ordinal = None | ||
debug = self._app._debug | ||
if debug: | ||
ordinal = self._pre.ordinal() | ||
await self._pre.send( | ||
ordinal, self._name, *args, **kwargs) | ||
await self._send(*args, **kwargs) | ||
if debug: | ||
await self._post.send( | ||
ordinal, self._name, *args, **kwargs) | ||
|
||
if not self.frozen: | ||
raise RuntimeError("Cannot send non-frozen signal.") | ||
|
||
class FuncSignal(BaseSignal): | ||
"""Callback-based signal implementation. | ||
To connect a callback to a signal, use any list method. | ||
Signals are fired using the :meth:`send` method, which takes named | ||
arguments. | ||
""" | ||
|
||
__slots__ = () | ||
|
||
def send(self, *args, **kwargs): | ||
""" | ||
Sends data to all registered receivers. | ||
""" | ||
for receiver in self: | ||
receiver(*args, **kwargs) | ||
|
||
|
||
class DebugSignal(BaseSignal): | ||
|
||
__slots__ = () | ||
|
||
async def send(self, ordinal, name, *args, **kwargs): | ||
await self._send(ordinal, name, *args, **kwargs) | ||
|
||
|
||
class PreSignal(DebugSignal): | ||
|
||
__slots__ = ('_counter',) | ||
|
||
def __init__(self): | ||
super().__init__() | ||
self._counter = count(1) | ||
|
||
def ordinal(self): | ||
return next(self._counter) | ||
|
||
|
||
class PostSignal(DebugSignal): | ||
|
||
__slots__ = () | ||
await receiver(*args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.