File size: 690 Bytes
f38488f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/env python3
from collections import defaultdict
from typing import Callable, Any, Dict, List

class Channel:
    """
    Central Message Bus.
    Components publish events (e.g., 'sensor_data', 'surprise_alert')
    and other components subscribe to those topics.
    """
    def __init__(self):
        self._subscribers: Dict[str, List[Callable]] = defaultdict(list)

    def subscribe(self, topic: str, callback: Callable):
        self._subscribers[topic].append(callback)

    def publish(self, topic: str, payload: Any):
        for callback in self._subscribers[topic]:
            callback(payload)

# Global singleton so all modules see the same bus
channel = Channel()