Skip to content

Commit

Permalink
Merge pull request #78 from GraiaProject/feat/optional-dispatcher
Browse files Browse the repository at this point in the history
Optional event.Dispatcher
  • Loading branch information
GreyElaina authored May 28, 2024
2 parents 2bc6f11 + 338c1a6 commit e2b338a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
17 changes: 10 additions & 7 deletions src/graia/broadcast/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import traceback
from contextlib import asynccontextmanager
from typing import (
Any,
Callable,
Dict,
Iterable,
Expand Down Expand Up @@ -104,15 +105,17 @@ def default_listener_generator(self, event_class) -> Iterable[Listener]:
async def layered_scheduler(
self,
listener_generator: Iterable[Listener],
event: Dispatchable,
event: Any,
addition_dispatchers: Optional[List["T_Dispatcher"]] = None,
):
grouped: Dict[int, List[Listener]] = group_dict(
listener_generator, lambda x: x.priorities.get(event.__class__) or x.priority
)
event_dispatcher_mixin = dispatcher_mixin_handler(event.Dispatcher)
if addition_dispatchers:
event_dispatcher_mixin = event_dispatcher_mixin + addition_dispatchers
event_dispatcher_mixin = []
if hasattr(event, "Dispatcher"):
event_dispatcher_mixin = dispatcher_mixin_handler(event.Dispatcher)
if addition_dispatchers:
event_dispatcher_mixin = event_dispatcher_mixin + addition_dispatchers
with self.event_ctx.use(event):
for _, current_group in sorted(grouped.items(), key=lambda x: x[0]):
tasks = [
Expand Down Expand Up @@ -293,7 +296,7 @@ async def param_compile(

dii.ctx.reset(dii_token)

def postEvent(self, event: Dispatchable, upper_event: Optional[Dispatchable] = None):
def postEvent(self, event: Any, upper_event: Optional[Any] = None):
if not hasattr(self, "_loop"):
from creart import it

Expand All @@ -304,7 +307,7 @@ def postEvent(self, event: Dispatchable, upper_event: Optional[Dispatchable] = N
event=event,
addition_dispatchers=(
[CoverDispatcher(i, upper_event) for i in dispatcher_mixin_handler(upper_event.Dispatcher)]
if upper_event
if upper_event and hasattr(upper_event, "Dispatcher")
else []
),
)
Expand Down Expand Up @@ -382,7 +385,7 @@ def removeListener(self, target):

def receiver(
self,
event: Union[str, Type[Dispatchable]],
event: Union[str, Type[Any]],
priority: int = 16,
dispatchers: Optional[List[T_Dispatcher]] = None,
namespace: Optional[Namespace] = None,
Expand Down
3 changes: 1 addition & 2 deletions src/graia/broadcast/interfaces/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
Union,
)

from ..entities.event import Dispatchable
from ..entities.signatures import Force
from ..exceptions import ExecutionStop, RequirementCrashed
from ..typing import T_Dispatcher
Expand All @@ -32,7 +31,7 @@
from .. import Broadcast


T_Event = TypeVar("T_Event", bound=Dispatchable, covariant=True)
T_Event = TypeVar("T_Event", covariant=True)


class DispatcherInterface(Generic[T_Event]):
Expand Down
6 changes: 3 additions & 3 deletions src/graia/broadcast/interrupt/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import asyncio
from asyncio import Future, get_running_loop
from typing import Optional, Type, Union
from typing import Any, Optional, Type, Union

from .. import Broadcast
from ..entities.event import Dispatchable
Expand Down Expand Up @@ -56,7 +56,7 @@ async def wait(
for i in listeners:
self.broadcast.removeListener(i)

def leader_listener_generator(self, waiter: Waiter, event_type: Type[Dispatchable], future: Future):
def leader_listener_generator(self, waiter: Waiter, event_type: Type[Any], future: Future):
async def inside_listener(event: event_type):
if future.done():
return RemoveMe
Expand All @@ -67,7 +67,7 @@ async def inside_listener(event: event_type):
inline_dispatchers=waiter.using_dispatchers,
decorators=waiter.using_decorators,
),
dispatchers=dispatcher_mixin_handler(event.Dispatcher),
dispatchers=dispatcher_mixin_handler(event.Dispatcher) if hasattr(event, "Dispatcher") else [],
)
# at present, the state of `future` is absolutely unknown.
if result is not None and not future.done():
Expand Down
1 change: 0 additions & 1 deletion src/graia/broadcast/utilles.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ def __iter__(self):
stack.pop()



class _CoveredObjectMeta(type):
if TYPE_CHECKING:
__origin__: Any
Expand Down

0 comments on commit e2b338a

Please sign in to comment.