-
I was able to subclass and patch signals to maintain the order is which receivers are connected: from __future__ import annotations
from collections import defaultdict
from typing import Hashable
from blinker import NamedSignal
class OrderedSet(dict):
def __or__(self, other: set | OrderedSet) -> OrderedSet: # type: ignore[override]
other_keys = other if isinstance(other, set) else other.keys()
return OrderedSet.fromkeys(*self.keys(), *other_keys) # type: ignore[return-value]
def add(self, value: Hashable) -> None:
self[value] = None
def discard(self, value: Hashable) -> None:
if value in self:
del self[value]
class Signal(NamedSignal):
def __init__(self, name: str, doc: str | None = None) -> None:
super().__init__(name, doc)
self._by_receiver = defaultdict(OrderedSet) # type: ignore[arg-type]
self._by_sender = defaultdict(OrderedSet) # type: ignore[arg-type]
class Namespace(dict):
"""A mapping of signal names to signals."""
def signal(self, name: str, doc: str | None = None) -> Signal:
"""Return the `Signal` *name*, creating it if required.
Repeated calls to this function will return the same signal object.
"""
try:
return self[name]
except KeyError:
return self.setdefault(name, Signal(name, doc))
signal = Namespace().signal Import your own |
Beta Was this translation helpful? Give feedback.
Answered by
davidism
Apr 27, 2024
Replies: 1 comment 1 reply
-
You can set |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
davidism
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can set
Signal.set_class = OrderedSet
as of Blinker 1.7.