Skip to content

handler

dev_tool.event.app.handler

AppLifecycleHandler

Bases: EventHandler

A handler class for application lifecycle events.

This class manages hooks for different application events and executes them when corresponding events are received.

The constructor for the AppLifecycleHandler class.

Initializes an empty hooks dictionary for storing event callbacks.

Source code in dev_tool/event/app/handler.py
def __init__(self) -> None:
    """
    The constructor for the AppLifecycleHandler class.

    Initializes an empty hooks dictionary for storing event callbacks.
    """

    self.hooks = {}

hooks = {} instance-attribute

handle

A method that handles incoming events by executing registered hooks.

Parameters:

  • event (Event) –

    The event object to handle.

Source code in dev_tool/event/app/handler.py
def handle(self, event: Event) -> None:
    """
    A method that handles incoming events by executing registered hooks.

    :param event: The event object to handle.
    """

    if event.type in self.hooks:
        for hook in self.hooks[event.type]:
            with contextlib.suppress(Exception):
                hook(event)

register_hook

A method that registers a hook function for a specific event type.

Parameters:

  • event_type (AppEventType) –

    The application event type to register the hook for.

  • function (Callable) –

    The callback function to execute when the event occurs.

Source code in dev_tool/event/app/handler.py
def register_hook(self, event_type: AppEventType, function: Callable) -> None:
    """
    A method that registers a hook function for a specific event type.

    :param event_type: The application event type to register the hook for.
    :param function: The callback function to execute when the event occurs.
    """

    if event_type not in self.hooks:
        self.hooks[event_type] = []

    self.hooks[event_type].append(function)