Skip to content

EventFilteredConnection

Base classes

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

⋔ Inheritance diagram

graph TD
  4265520919["async_executor.EventFilteredConnection"]
  3481597255["typing.Generic"]
  2751680898["builtins.object"]
  3481597255 --> 4265520919
  2751680898 --> 3481597255

🛈 DocStrings

Filtered connection proxy for specific event types.

Source code in src/anyenv/calling/async_executor.py
class EventFilteredConnection[E]:
    """Filtered connection proxy for specific event types."""

    def __init__(
        self,
        executor: AsyncExecutor[Any, Any] | AsyncIteratorExecutor[Any, Any],
        event_types: tuple[type, ...],
    ) -> None:
        self._executor = executor
        self._event_types = event_types

    def connect(self, handler: Callable[[E], Any]) -> None:
        """Connect handler to filtered events."""
        # Check if handler is already connected for these types
        handlers_list = self._executor._filtered_handlers[self._event_types]  # noqa: SLF001
        for original_handler, _ in handlers_list:
            if original_handler is handler:
                return

        # Create filtered handler wrapper
        async def filtered_handler(event: Any) -> Any:
            if isinstance(event, self._event_types):
                if inspect.iscoroutinefunction(handler):
                    return await handler(event)  # type: ignore[arg-type]
                return handler(event)  # type: ignore[arg-type]
            return None

        # Store mapping and add to observer system
        handlers_list.append((handler, filtered_handler))
        self._executor._observers.add_handler(filtered_handler)  # noqa: SLF001

    def disconnect(self, handler: Callable[[E], Any]) -> None:
        """Disconnect handler from filtered events."""
        if self._event_types not in self._executor._filtered_handlers:  # noqa: SLF001
            return

        handlers_list = self._executor._filtered_handlers[self._event_types]  # noqa: SLF001

        for i, (original_handler, filtered_handler) in enumerate(handlers_list):
            if original_handler is handler:
                # Remove from observer system and our tracking
                self._executor._observers.remove_handler(filtered_handler)  # noqa: SLF001
                handlers_list.pop(i)
                # Clean up empty entries
                if not handlers_list:
                    del self._executor._filtered_handlers[self._event_types]  # noqa: SLF001
                break

    def clear(self) -> None:
        """Clear all handlers for this event filter."""
        if self._event_types not in self._executor._filtered_handlers:  # noqa: SLF001
            return

        handlers_list = self._executor._filtered_handlers[self._event_types]  # noqa: SLF001
        handlers_to_remove = [handler for handler, _ in handlers_list]
        for handler in handlers_to_remove:
            self.disconnect(handler)

clear

clear() -> None

Clear all handlers for this event filter.

Source code in src/anyenv/calling/async_executor.py
def clear(self) -> None:
    """Clear all handlers for this event filter."""
    if self._event_types not in self._executor._filtered_handlers:  # noqa: SLF001
        return

    handlers_list = self._executor._filtered_handlers[self._event_types]  # noqa: SLF001
    handlers_to_remove = [handler for handler, _ in handlers_list]
    for handler in handlers_to_remove:
        self.disconnect(handler)

connect

connect(handler: Callable[[E], Any]) -> None

Connect handler to filtered events.

Source code in src/anyenv/calling/async_executor.py
def connect(self, handler: Callable[[E], Any]) -> None:
    """Connect handler to filtered events."""
    # Check if handler is already connected for these types
    handlers_list = self._executor._filtered_handlers[self._event_types]  # noqa: SLF001
    for original_handler, _ in handlers_list:
        if original_handler is handler:
            return

    # Create filtered handler wrapper
    async def filtered_handler(event: Any) -> Any:
        if isinstance(event, self._event_types):
            if inspect.iscoroutinefunction(handler):
                return await handler(event)  # type: ignore[arg-type]
            return handler(event)  # type: ignore[arg-type]
        return None

    # Store mapping and add to observer system
    handlers_list.append((handler, filtered_handler))
    self._executor._observers.add_handler(filtered_handler)  # noqa: SLF001

disconnect

disconnect(handler: Callable[[E], Any]) -> None

Disconnect handler from filtered events.

Source code in src/anyenv/calling/async_executor.py
def disconnect(self, handler: Callable[[E], Any]) -> None:
    """Disconnect handler from filtered events."""
    if self._event_types not in self._executor._filtered_handlers:  # noqa: SLF001
        return

    handlers_list = self._executor._filtered_handlers[self._event_types]  # noqa: SLF001

    for i, (original_handler, filtered_handler) in enumerate(handlers_list):
        if original_handler is handler:
            # Remove from observer system and our tracking
            self._executor._observers.remove_handler(filtered_handler)  # noqa: SLF001
            handlers_list.pop(i)
            # Clean up empty entries
            if not handlers_list:
                del self._executor._filtered_handlers[self._event_types]  # noqa: SLF001
            break

:fa-brands-github: Show source on GitHub