python-cq 0.2.2__tar.gz → 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {python_cq-0.2.2 → python_cq-0.3.0}/PKG-INFO +1 -1
- {python_cq-0.2.2 → python_cq-0.3.0}/cq/__init__.py +6 -6
- {python_cq-0.2.2 → python_cq-0.3.0}/cq/_core/dispatcher/bus.py +30 -5
- {python_cq-0.2.2 → python_cq-0.3.0}/cq/_core/message.py +9 -6
- {python_cq-0.2.2 → python_cq-0.3.0}/pyproject.toml +1 -1
- {python_cq-0.2.2 → python_cq-0.3.0}/README.md +0 -0
- {python_cq-0.2.2 → python_cq-0.3.0}/cq/_core/__init__.py +0 -0
- {python_cq-0.2.2 → python_cq-0.3.0}/cq/_core/dispatcher/__init__.py +0 -0
- {python_cq-0.2.2 → python_cq-0.3.0}/cq/_core/dispatcher/base.py +0 -0
- {python_cq-0.2.2 → python_cq-0.3.0}/cq/_core/dispatcher/pipe.py +0 -0
- {python_cq-0.2.2 → python_cq-0.3.0}/cq/_core/dto.py +0 -0
- {python_cq-0.2.2 → python_cq-0.3.0}/cq/_core/middleware.py +0 -0
- {python_cq-0.2.2 → python_cq-0.3.0}/cq/exceptions.py +0 -0
- {python_cq-0.2.2 → python_cq-0.3.0}/cq/middlewares/__init__.py +0 -0
- {python_cq-0.2.2 → python_cq-0.3.0}/cq/middlewares/retry.py +0 -0
- {python_cq-0.2.2 → python_cq-0.3.0}/cq/py.typed +0 -0
|
@@ -12,9 +12,9 @@ from ._core.message import (
|
|
|
12
12
|
QueryBus,
|
|
13
13
|
command_handler,
|
|
14
14
|
event_handler,
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
get_command_bus,
|
|
16
|
+
get_event_bus,
|
|
17
|
+
get_query_bus,
|
|
18
18
|
query_handler,
|
|
19
19
|
)
|
|
20
20
|
from ._core.middleware import Middleware, MiddlewareResult
|
|
@@ -35,8 +35,8 @@ __all__ = (
|
|
|
35
35
|
"QueryBus",
|
|
36
36
|
"command_handler",
|
|
37
37
|
"event_handler",
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
38
|
+
"get_command_bus",
|
|
39
|
+
"get_event_bus",
|
|
40
|
+
"get_query_bus",
|
|
41
41
|
"query_handler",
|
|
42
42
|
)
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import asyncio
|
|
2
|
-
from abc import abstractmethod
|
|
2
|
+
from abc import ABC, abstractmethod
|
|
3
3
|
from collections import defaultdict
|
|
4
|
-
from collections.abc import Callable
|
|
4
|
+
from collections.abc import Awaitable, Callable
|
|
5
5
|
from dataclasses import dataclass, field
|
|
6
6
|
from inspect import isclass
|
|
7
7
|
from types import GenericAlias
|
|
8
|
-
from typing import Protocol, Self, TypeAliasType, runtime_checkable
|
|
8
|
+
from typing import Any, Protocol, Self, TypeAliasType, runtime_checkable
|
|
9
9
|
|
|
10
10
|
import injection
|
|
11
11
|
|
|
@@ -14,6 +14,8 @@ from cq._core.dispatcher.base import BaseDispatcher, Dispatcher
|
|
|
14
14
|
type HandlerType[**P, T] = type[Handler[P, T]]
|
|
15
15
|
type HandlerFactory[**P, T] = Callable[..., Handler[P, T]]
|
|
16
16
|
|
|
17
|
+
type Listener[T] = Callable[[T], Awaitable[Any]]
|
|
18
|
+
|
|
17
19
|
type BusType[I, O] = type[Bus[I, O]]
|
|
18
20
|
|
|
19
21
|
|
|
@@ -34,6 +36,10 @@ class Bus[I, O](Dispatcher[I, O], Protocol):
|
|
|
34
36
|
def subscribe(self, input_type: type[I], factory: HandlerFactory[[I], O]) -> Self:
|
|
35
37
|
raise NotImplementedError
|
|
36
38
|
|
|
39
|
+
@abstractmethod
|
|
40
|
+
def add_listeners(self, *listeners: Listener[I]) -> Self:
|
|
41
|
+
raise NotImplementedError
|
|
42
|
+
|
|
37
43
|
|
|
38
44
|
@dataclass(eq=False, frozen=True, slots=True)
|
|
39
45
|
class SubscriberDecorator[I, O]:
|
|
@@ -59,7 +65,24 @@ class SubscriberDecorator[I, O]:
|
|
|
59
65
|
return self.injection_module.find_instance(self.bus_type)
|
|
60
66
|
|
|
61
67
|
|
|
62
|
-
class
|
|
68
|
+
class BaseBus[I, O](BaseDispatcher[I, O], Bus[I, O], ABC):
|
|
69
|
+
__slots__ = ("__listeners",)
|
|
70
|
+
|
|
71
|
+
__listeners: list[Listener[I]]
|
|
72
|
+
|
|
73
|
+
def __init__(self) -> None:
|
|
74
|
+
super().__init__()
|
|
75
|
+
self.__listeners = []
|
|
76
|
+
|
|
77
|
+
def add_listeners(self, *listeners: Listener[I]) -> Self:
|
|
78
|
+
self.__listeners.extend(listeners)
|
|
79
|
+
return self
|
|
80
|
+
|
|
81
|
+
async def _trigger_listeners(self, input_value: I, /) -> None:
|
|
82
|
+
await asyncio.gather(*(listener(input_value) for listener in self.__listeners))
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
class SimpleBus[I, O](BaseBus[I, O]):
|
|
63
86
|
__slots__ = ("__handlers",)
|
|
64
87
|
|
|
65
88
|
__handlers: dict[type[I], HandlerFactory[[I], O]]
|
|
@@ -69,6 +92,7 @@ class SimpleBus[I, O](BaseDispatcher[I, O], Bus[I, O]):
|
|
|
69
92
|
self.__handlers = {}
|
|
70
93
|
|
|
71
94
|
async def dispatch(self, input_value: I, /) -> O:
|
|
95
|
+
await self._trigger_listeners(input_value)
|
|
72
96
|
input_type = type(input_value)
|
|
73
97
|
|
|
74
98
|
try:
|
|
@@ -91,7 +115,7 @@ class SimpleBus[I, O](BaseDispatcher[I, O], Bus[I, O]):
|
|
|
91
115
|
return self
|
|
92
116
|
|
|
93
117
|
|
|
94
|
-
class TaskBus[I](
|
|
118
|
+
class TaskBus[I](BaseBus[I, None]):
|
|
95
119
|
__slots__ = ("__handlers",)
|
|
96
120
|
|
|
97
121
|
__handlers: dict[type[I], list[HandlerFactory[[I], None]]]
|
|
@@ -101,6 +125,7 @@ class TaskBus[I](BaseDispatcher[I, None], Bus[I, None]):
|
|
|
101
125
|
self.__handlers = defaultdict(list)
|
|
102
126
|
|
|
103
127
|
async def dispatch(self, input_value: I, /) -> None:
|
|
128
|
+
await self._trigger_listeners(input_value)
|
|
104
129
|
handler_factories = self.__handlers.get(type(input_value))
|
|
105
130
|
|
|
106
131
|
if not handler_factories:
|
|
@@ -39,13 +39,16 @@ injection.set_constant(TaskBus(), EventBus, alias=True)
|
|
|
39
39
|
injection.set_constant(SimpleBus(), QueryBus, alias=True)
|
|
40
40
|
|
|
41
41
|
|
|
42
|
-
|
|
43
|
-
|
|
42
|
+
@injection.inject
|
|
43
|
+
def get_command_bus[T](bus: CommandBus[T] = NotImplemented, /) -> CommandBus[T]:
|
|
44
|
+
return bus
|
|
44
45
|
|
|
45
46
|
|
|
46
|
-
|
|
47
|
-
|
|
47
|
+
@injection.inject
|
|
48
|
+
def get_event_bus(bus: EventBus = NotImplemented, /) -> EventBus:
|
|
49
|
+
return bus
|
|
48
50
|
|
|
49
51
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
+
@injection.inject
|
|
53
|
+
def get_query_bus[T](bus: QueryBus[T] = NotImplemented, /) -> QueryBus[T]:
|
|
54
|
+
return bus
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|