Skip to content

🔔 Notifications & Handlers

Modern event handling with Bazario

🌟 What are Notifications?

Notifications in Bazario represent events that are published in response to certain actions. Think of them as messengers that broadcast changes throughout your system, without expecting a response back.

🛠️ Implementation Guide

📝 Example: Defining Notifications and Handlers

from bazario import Notification, NotificationHandler
from dataclasses import dataclass

@dataclass(frozen=True)
class PostAdded(Notification):
    post_id: int
    user_id: int

class PostAddedFirstHandler(NotificationHandler[PostAdded]):
    def handle(self, notification: PostAdded) -> None:
        logger.info(
            "Post first added: post_id=%s, user_id=%s",
            notification.post_id, notification.user_id,
        )

class PostAddedSecondHandler(NotificationHandler[PostAdded]):
    def handle(self, notification: PostAdded) -> None:
        logger.info(
            "Post second added: post_id=%s, user_id=%s",
            notification.post_id, notification.user_id,
        )

🔌 Setup Process

1️⃣ Register with Registry

registry.add_notification_handlers(
    PostAdded, 
    PostAddedFirstHandler,
    PostAddedSecondHandler,
)
2️⃣ Configure IoC Container
container.register(PostAddedFirstHandler)
container.register(PostAddedSecondHandler)
3️⃣ Publishing Notifications
from bazario import Publisher

def controller(publisher: Publisher) -> None:
    publisher.publish(PostAdded(post_id=1, user_id=1))
    # ✨ Output:
    # Post first added: post_id=1, user_id=1
    # Post second added: post_id=1, user_id=1