66from graia .broadcast .entities .signatures import Force
77from graia .broadcast .exceptions import ExecutionStop
88from graia .broadcast .interfaces .dispatcher import DispatcherInterface
9+ from graia .broadcast .builtin .factory import (
10+ AsyncDispatcherContextManager ,
11+ ResponseCodeEnum ,
12+ StatusCodeEnum ,
13+ )
914
1015from graia .application .exceptions import ConflictItem
1116
@@ -43,7 +48,7 @@ def origin_or_zero(origin: Optional[_T]) -> Union[_T, int]:
4348 return origin
4449
4550
46- class Kanata (BaseDispatcher ):
51+ class Kanata (AsyncDispatcherContextManager ):
4752 "彼方."
4853 signature_list : List [Union [NormalMatch , PatternReceiver ]]
4954 stop_exec_if_fail : bool = True
@@ -53,6 +58,9 @@ class Kanata(BaseDispatcher):
5358 allow_quote : bool
5459 skip_one_at_in_quote : bool
5560
61+ args = ()
62+ kwargs = {}
63+
5664 def __init__ (
5765 self ,
5866 signature_list : List [Union [NormalMatch , PatternReceiver ]],
@@ -290,7 +298,10 @@ def allocation(
290298 async def catch_argument_names (self ) -> List [str ]:
291299 return [i .name for i in self .signature_list if isinstance (i , PatternReceiver )]
292300
293- async def beforeDispatch (self , interface : DispatcherInterface ):
301+ async def generator_factory (self ):
302+ interface : DispatcherInterface = (yield )
303+ current_status : StatusCodeEnum = StatusCodeEnum .DISPATCHING # init stat
304+
294305 message_chain : MessageChain = (
295306 await interface .lookup_param ("__kanata_messagechain__" , MessageChain , None )
296307 ).exclude (Source )
@@ -308,27 +319,21 @@ async def beforeDispatch(self, interface: DispatcherInterface):
308319
309320 mapping_result = self .detect_and_mapping (self .signature_list , message_chain )
310321 if mapping_result is not None :
311- self .parsed_items = self .allocation (mapping_result )
312- else :
313- if self .stop_exec_if_fail :
314- raise ExecutionStop ()
315-
316- async def catch (self , interface : DispatcherInterface ):
317- if interface .name == "__kanata_messagechain__" :
318- return
319-
320- random_id = random .random ()
321- if self .parsed_items is not None :
322- result = self .parsed_items .get (interface .name , random_id )
323- return Force (result ) if result is not random_id else None
322+ parsed_items = self .allocation (mapping_result )
324323 else :
325324 if self .stop_exec_if_fail :
326325 raise ExecutionStop ()
326+ yield
327+ while current_status is StatusCodeEnum .DISPATCHING :
328+ result = None
329+
330+ if interface .name != "__kanata_messagechain__" :
331+ random_id = random .random ()
332+ if parsed_items is not None :
333+ item = parsed_items .get (interface .name , random_id )
334+ result = Force (item ) if item is not random_id else None
335+ else :
336+ if self .stop_exec_if_fail :
337+ raise ExecutionStop ()
327338
328- async def afterDispatch (
329- self ,
330- interface : "DispatcherInterface" ,
331- exception : Optional [Exception ] = None ,
332- tb : Optional [TracebackType ] = None ,
333- ):
334- self .parsed_items = None
339+ current_status , external = yield (ResponseCodeEnum .VALUE , result )
0 commit comments