From 621df40c66aeeff7e89b12523ed181500e0f7ded Mon Sep 17 00:00:00 2001 From: InfinityPacer <160988576+InfinityPacer@users.noreply.github.com> Date: Thu, 12 Dec 2024 15:37:43 +0800 Subject: [PATCH] feat(event): add support for priority in event registration --- app/core/event.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app/core/event.py b/app/core/event.py index f8af7da35..a33da2812 100644 --- a/app/core/event.py +++ b/app/core/event.py @@ -502,13 +502,15 @@ def __handle_event_error(self, event: Event, handler: Callable, e: Exception): } ) - def register(self, etype: Union[EventType, ChainEventType, List[Union[EventType, ChainEventType]], type]): + def register(self, etype: Union[EventType, ChainEventType, List[Union[EventType, ChainEventType]], type], + priority: int = DEFAULT_EVENT_PRIORITY): """ 事件注册装饰器,用于将函数注册为事件的处理器 :param etype: - 单个事件类型成员 (如 EventType.MetadataScrape, ChainEventType.PluginAction) - 事件类型类 (EventType, ChainEventType) - 或事件类型成员的列表 + :param priority: 可选,链式事件的优先级,默认为 DEFAULT_EVENT_PRIORITY """ def decorator(f: Callable): @@ -528,11 +530,11 @@ def decorator(f: Callable): # 遍历列表,处理每个事件类型 for event in event_list: if isinstance(event, (EventType, ChainEventType)): - self.add_event_listener(event, f) + self.add_event_listener(event, f, priority) elif isinstance(event, type) and issubclass(event, (EventType, ChainEventType)): # 如果是 EventType 或 ChainEventType 类,提取该类中的所有成员 for et in event.__members__.values(): - self.add_event_listener(et, f) + self.add_event_listener(et, f, priority) else: raise ValueError(f"无效的事件类型: {event}")