26 lines
752 B
Python
26 lines
752 B
Python
import asyncio
|
|
from typing import Dict, Set, Any
|
|
|
|
class Hub:
|
|
def __init__(self):
|
|
self._lock = asyncio.Lock()
|
|
self._subs: Dict[int, Set[Any]] = {} # panel_id -> set[WebSocket]
|
|
|
|
async def subscribe(self, panel_id: int, ws):
|
|
async with self._lock:
|
|
self._subs.setdefault(panel_id, set()).add(ws)
|
|
|
|
async def unsubscribe(self, panel_id: int, ws):
|
|
async with self._lock:
|
|
s = self._subs.get(panel_id)
|
|
if s and ws in s:
|
|
s.remove(ws)
|
|
if s and len(s) == 0:
|
|
self._subs.pop(panel_id, None)
|
|
|
|
async def connections(self, panel_id: int):
|
|
async with self._lock:
|
|
return list(self._subs.get(panel_id, set()))
|
|
|
|
hub = Hub()
|