Skip to content

Signal class

edgy.Signal

Signal()

Base class for all Edgy signals.

Creates a new signal.

Source code in edgy/core/signals/signal.py
25
26
27
28
29
def __init__(self) -> None:
    """
    Creates a new signal.
    """
    self.receivers: Dict[Union[int, Tuple[int, int]], Callable] = {}

receivers instance-attribute

receivers = {}

connect

connect(receiver)

Connects a given receiver to the the signal.

PARAMETER DESCRIPTION
receiver

TYPE: Callable

Source code in edgy/core/signals/signal.py
31
32
33
34
35
36
37
38
39
40
41
42
43
def connect(self, receiver: Callable) -> None:
    """
    Connects a given receiver to the the signal.
    """
    if not callable(receiver):
        raise SignalError("The signals should be callables")

    if not func_accepts_kwargs(receiver):
        raise SignalError("Signal receivers must accept keyword arguments (**kwargs).")

    key = make_id(receiver)
    if key not in self.receivers:
        self.receivers[key] = receiver

disconnect

disconnect(receiver)

Removes the receiver from the signal.

PARAMETER DESCRIPTION
receiver

TYPE: Callable

Source code in edgy/core/signals/signal.py
45
46
47
48
49
50
51
def disconnect(self, receiver: Callable) -> bool:
    """
    Removes the receiver from the signal.
    """
    key = make_id(receiver)
    func: Union[Callable, None] = self.receivers.pop(key, None)
    return True if func is not None else False

send async

send(sender, **kwargs)

Sends the notification to all the receivers.

PARAMETER DESCRIPTION
sender

TYPE: Type[Model]

**kwargs

TYPE: Any DEFAULT: {}

Source code in edgy/core/signals/signal.py
53
54
55
56
57
58
async def send(self, sender: Type["Model"], **kwargs: Any) -> None:
    """
    Sends the notification to all the receivers.
    """
    receivers = [func(sender=sender, **kwargs) for func in self.receivers.values()]
    await asyncio.gather(*receivers)