Skip to content

Signal

Sub classes

Name Children Inherits
AppSignal
anyenv.signals.examples
Application-specific signal constrained to allowed event types.

    Base classes

    Name Children Inherits
    Generic
    typing
    Abstract base class for generic types.

    ⋔ Inheritance diagram

    graph TD
      2761987693["core.Signal"]
      3481597255["typing.Generic"]
      2751680898["builtins.object"]
      3481597255 --> 2761987693
      2751680898 --> 3481597255

    🛈 DocStrings

    Descriptor: define at class level, get BoundSignal per instance.

    Example

    class MyClass: changed = Signalstr

    Source code in src/anyenv/signals/core.py
    class Signal[*Ts]:
        """Descriptor: define at class level, get BoundSignal per instance.
    
        Example:
            class MyClass:
                changed = Signal[str]()
        """
    
        __slots__ = ("_bound_signals", "_name", "_weak_refs")
    
        def __init__(self) -> None:
            self._name: str = ""
            self._bound_signals: dict[int, BoundSignal[*Ts]] = {}
            self._weak_refs: dict[int, ref[object]] = {}
    
        def __set_name__(self, owner: type, name: str) -> None:
            self._name = name
    
        def __get__(self, obj: object | None, owner: type | None = None) -> BoundSignal[*Ts]:
            if obj is None:
                # Class-level access - return a dummy for introspection
                return BoundSignal()
            obj_id = id(obj)
            if obj_id not in self._bound_signals:
                # setdefault: if two threads get here, both use the one stored first.
                new = BoundSignal[*Ts]()
                if self._bound_signals.setdefault(obj_id, new) is not new:
                    return self._bound_signals[obj_id]
    
                # Set up cleanup callback when obj is garbage collected
                def cleanup(_: object) -> None:
                    self._bound_signals.pop(obj_id, None)
                    self._weak_refs.pop(obj_id, None)
    
                # Store the weak reference to keep it alive
                self._weak_refs[obj_id] = ref(obj, cleanup)
            return self._bound_signals[obj_id]
    

    :fa-brands-github: Show source on GitHub