Skip to content

Commit 3e75f29

Browse files
authored
refactoring: Message bus types
1 parent 4f7c129 commit 3e75f29

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

cq/_core/message.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import injection
44

5+
from cq._core.dispatcher.base import Dispatcher
56
from cq._core.dispatcher.bus import Bus, SimpleBus, TaskBus
67
from cq._core.handler import (
78
HandlerDecorator,
@@ -15,9 +16,9 @@
1516
Event = object
1617
Query = object
1718

18-
type CommandBus[T] = Bus[Command, T]
19-
type EventBus = Bus[Event, None]
20-
type QueryBus[T] = Bus[Query, T]
19+
type CommandBus[T] = Dispatcher[Command, T]
20+
type EventBus = Dispatcher[Event, None]
21+
type QueryBus[T] = Dispatcher[Query, T]
2122

2223
AnyCommandBus = CommandBus[Any]
2324

@@ -33,8 +34,7 @@
3334
)
3435

3536

36-
@injection.injectable(inject=False, mode="fallback")
37-
def new_command_bus(*, threadsafe: bool | None = None) -> CommandBus: # type: ignore[type-arg]
37+
def new_command_bus(*, threadsafe: bool | None = None) -> Bus[Command, Any]:
3838
bus = SimpleBus(command_handler.manager)
3939
transaction_scope_middleware = InjectionScopeMiddleware(
4040
CQScope.TRANSACTION,
@@ -45,11 +45,27 @@ def new_command_bus(*, threadsafe: bool | None = None) -> CommandBus: # type: i
4545
return bus
4646

4747

48-
@injection.injectable(inject=False, mode="fallback")
49-
def new_event_bus() -> EventBus:
48+
def new_event_bus() -> Bus[Event, None]:
5049
return TaskBus(event_handler.manager)
5150

5251

53-
@injection.injectable(inject=False, mode="fallback")
54-
def new_query_bus() -> QueryBus: # type: ignore[type-arg]
52+
def new_query_bus() -> Bus[Query, Any]:
5553
return SimpleBus(query_handler.manager)
54+
55+
56+
@injection.injectable(inject=False, mode="fallback")
57+
def _() -> CommandBus: # type: ignore[type-arg]
58+
return new_command_bus()
59+
60+
61+
@injection.injectable(inject=False, mode="fallback")
62+
def _() -> EventBus:
63+
return new_event_bus()
64+
65+
66+
@injection.injectable(inject=False, mode="fallback")
67+
def _() -> QueryBus: # type: ignore[type-arg]
68+
return new_query_bus()
69+
70+
71+
del _

uv.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)