29 lines
975 B
Python
29 lines
975 B
Python
import inspect
|
|
import server
|
|
from fastapi import WebSocket
|
|
|
|
_EVENTS = {}
|
|
|
|
def event(event_name: str, priority: int = 9999):
|
|
def inlineEvent(func):
|
|
if not inspect.iscoroutinefunction(func):
|
|
raise TypeError(f'Декоратор event поддерживает только подпрограммы')
|
|
if event_name not in _EVENTS:
|
|
_EVENTS[event_name] = []
|
|
|
|
_EVENTS[event_name].append({'function': func, 'priority': priority})
|
|
_EVENTS[event_name].sort(key = lambda x: x['priority'])
|
|
|
|
return func
|
|
return inlineEvent
|
|
|
|
async def call_event(event_name: str, connection: WebSocket, uuid: str | None, *args, **kwargs):
|
|
if event_name not in _EVENTS:
|
|
return
|
|
|
|
for item in _EVENTS[event_name]:
|
|
result = await item['function'](*args, **kwargs)
|
|
if uuid is None or result is None:
|
|
return
|
|
await server.Server.send(connection = connection, uuid = uuid, message = result)
|