feat: optimize interceptors

This commit is contained in:
martsforever
2025-10-29 17:51:49 +08:00
parent ef5f20ed82
commit 0f273c1088
+6 -4
View File
@@ -1,4 +1,4 @@
from typing import List
from typing import List, Dict
class GeneralInterceptor():
@@ -33,11 +33,13 @@ class GeneralInterceptor():
self.after_delete = after_delete
_general_interceptors: List[GeneralInterceptor] = []
_general_interceptors: Dict[str, GeneralInterceptor] = []
def add_general_interceptor(interceptor: GeneralInterceptor):
_general_interceptors.append(interceptor)
target_interceptor_list = _general_interceptors.get(interceptor.module, [])
target_interceptor_list.append(interceptor)
_general_interceptors[interceptor.module] = target_interceptor_list
async def invoke_interceptor(method: str, module: str, **kwargs):
@@ -45,7 +47,7 @@ async def invoke_interceptor(method: str, module: str, **kwargs):
module = module[1:]
if module.endswith('/'):
module = module[:-1]
match_interceptors = [item for item in _general_interceptors if item.module == module]
match_interceptors = _general_interceptors.get(module, [])
for item in match_interceptors:
interceptor_method = getattr(item, method, None) # 使用 getattr 获取方法
if interceptor_method and callable(interceptor_method):