Skip to content

Counter

🛈 DocStrings

Simple counter with signals for state changes.

Source code in src/anyenv/signals/examples.py
class Counter:
    """Simple counter with signals for state changes."""

    incremented = Signal[int]()
    reset = Signal[()]()

    def __init__(self, initial: int = 0) -> None:
        self._value = initial

    def increment(self) -> None:
        """Increment the counter and emit the incremented signal."""
        self._value += 1
        asyncio.create_task(self.incremented.emit(self._value))  # noqa: RUF006

    @property
    def value(self) -> int:
        """Return the current value of the counter."""
        return self._value

value property

value: int

Return the current value of the counter.

increment

increment() -> None

Increment the counter and emit the incremented signal.

Source code in src/anyenv/signals/examples.py
def increment(self) -> None:
    """Increment the counter and emit the incremented signal."""
    self._value += 1
    asyncio.create_task(self.incremented.emit(self._value))  # noqa: RUF006

:fa-brands-github: Show source on GitHub