Skip to content

Commit

Permalink
chore: BuiltinDispatcher, 0.23.4
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Nov 24, 2023
1 parent a1d6304 commit ee586c8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dependencies = [
"creart~=0.3.0",
]
name = "graia-broadcast"
version = "0.23.3"
version = "0.23.4"
description = "a highly customizable, elegantly designed event system based on asyncio"

[tool.pdm.build]
Expand Down
17 changes: 10 additions & 7 deletions src/graia/broadcast/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys
import traceback
from contextlib import asynccontextmanager
from typing import Callable, Dict, Iterable, List, Optional, Set, Type, Union
from typing import Callable, Dict, Iterable, List, Optional, Set, Type, Union, get_origin

from .builtin.defer import DeferDispatcher
from .builtin.depend import DependDispatcher
Expand Down Expand Up @@ -65,14 +65,17 @@ def __init__(self):

@self.prelude_dispatchers.append
class BroadcastBuiltinDispatcher(BaseDispatcher):
@staticmethod
async def catch(interface: DispatcherInterface):
if interface.annotation is interface.event.__class__:
return interface.event
elif interface.annotation is Broadcast:
@classmethod
async def catch(cls, interface: DispatcherInterface):
annotation = get_origin(interface.annotation) or interface.annotation
if annotation is Broadcast:
return interface.broadcast
elif interface.annotation is DispatcherInterface:
if annotation is DispatcherInterface:
return interface
if annotation is interface.event.__class__:
return interface.event
if isinstance(annotation, type) and isinstance(interface.event, annotation):
return interface.event

def default_listener_generator(self, event_class) -> Iterable[Listener]:
return list(
Expand Down
7 changes: 5 additions & 2 deletions src/test/dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,14 @@ async def test_event_dispatch():
executed = []

@bcc.receiver(TestEvent)
async def _1(ster, p, b: Broadcast, i: DispatcherInterface):
async def _1(ster, p, b: Broadcast, e: TestEvent, e1: Dispatchable, i: DispatcherInterface, i1: DispatcherInterface[TestEvent]):
assert ster == "1"
assert p == "P_event"
assert b is bcc
assert i.__class__ == DispatcherInterface
assert e.__class__ is TestEvent
assert e1.__class__ is TestEvent
assert i.__class__ is DispatcherInterface
assert i1.__class__ is DispatcherInterface
executed.append(1)

await bcc.postEvent(TestEvent())
Expand Down

0 comments on commit ee586c8

Please sign in to comment.