Skip to content

Commit 87447de

Browse files
authored
feat: ✨ Add message abstraction
1 parent 1337fa2 commit 87447de

5 files changed

Lines changed: 65 additions & 69 deletions

File tree

cq/__init__.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1-
from ._core.command import (
1+
from ._core.dispatcher.bus import Bus
2+
from ._core.dispatcher.pipe import Pipe
3+
from ._core.dto import DTO
4+
from ._core.message import (
25
AnyCommandBus,
36
Command,
47
CommandBus,
8+
Event,
9+
EventBus,
10+
Message,
11+
Query,
12+
QueryBus,
513
command_handler,
14+
event_handler,
615
find_command_bus,
16+
find_event_bus,
17+
find_query_bus,
18+
query_handler,
719
)
8-
from ._core.dispatcher.bus import Bus
9-
from ._core.dispatcher.pipe import Pipe
10-
from ._core.dto import DTO
11-
from ._core.event import Event, EventBus, event_handler, find_event_bus
1220
from ._core.middleware import Middleware, MiddlewareResult
13-
from ._core.query import Query, QueryBus, find_query_bus, query_handler
1421

1522
__all__ = (
1623
"AnyCommandBus",
@@ -20,6 +27,7 @@
2027
"DTO",
2128
"Event",
2229
"EventBus",
30+
"Message",
2331
"Middleware",
2432
"MiddlewareResult",
2533
"Pipe",

cq/_core/command.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

cq/_core/event.py

Lines changed: 0 additions & 20 deletions
This file was deleted.

cq/_core/message.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from abc import ABC
2+
from typing import Any
3+
4+
import injection
5+
6+
from cq._core.dispatcher.bus import Bus, SimpleBus, SubscriberDecorator, TaskBus
7+
from cq._core.dto import DTO
8+
9+
10+
class Message(DTO, ABC):
11+
__slots__ = ()
12+
13+
14+
class Command(Message, ABC):
15+
__slots__ = ()
16+
17+
18+
class Event(Message, ABC):
19+
__slots__ = ()
20+
21+
22+
class Query(Message, ABC):
23+
__slots__ = ()
24+
25+
26+
type CommandBus[T] = Bus[Command, T]
27+
type EventBus = Bus[Event, None]
28+
type QueryBus[T] = Bus[Query, T]
29+
30+
AnyCommandBus = CommandBus[Any]
31+
32+
33+
command_handler: SubscriberDecorator[Command, Any] = SubscriberDecorator(CommandBus)
34+
event_handler: SubscriberDecorator[Event, None] = SubscriberDecorator(EventBus)
35+
query_handler: SubscriberDecorator[Query, Any] = SubscriberDecorator(QueryBus)
36+
37+
injection.set_constant(SimpleBus(), CommandBus, alias=True)
38+
injection.set_constant(TaskBus(), EventBus, alias=True)
39+
injection.set_constant(SimpleBus(), QueryBus, alias=True)
40+
41+
42+
def find_command_bus[T]() -> CommandBus[T]:
43+
return injection.find_instance(CommandBus)
44+
45+
46+
def find_event_bus() -> EventBus:
47+
return injection.find_instance(EventBus)
48+
49+
50+
def find_query_bus[T]() -> QueryBus[T]:
51+
return injection.find_instance(QueryBus)

cq/_core/query.py

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)