Skip to content

Commit

Permalink
✨ add initial signals class definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
harshanarayana committed Jan 21, 2020
1 parent bffdb3b commit 333b975
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions sanic/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from aiosignal import Signal
from typing import Dict, List
from sanic.request import Request
from sanic.response import HTTPResponse


class SignalData:
__slots__ = (
"context", "exception", "additional_info", "request", "response", "namespace", "name"
)

def __init__(self, context, exception=None, request=None, response=None, additional_info=None):
self.context: str = context
self.exception: Exception = exception
self.additional_info: dict = additional_info
self.request: Request = request
self.response: HTTPResponse = response


class Namespace:
def __init__(self, namespace):
self._namespace = namespace # type: str
self._signals: Dict[str, Signal] = {}

def signal(self, name, owner):
try:
self._signals[name].append(Signal(owner=owner))
except KeyError:
self._signals[name] = Signal(owner=owner)

def register(self, name, callback):
self._signals[name].append(callback)

async def publish(self, name, data: SignalData = None):
if not data:
data = {}
data.namespace = self._namespace
data.name = name
await self._signals[name].send(data=data)

def freeze(self):
for key, signal in self._signals.items():
signal.freeze()

0 comments on commit 333b975

Please sign in to comment.