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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-cq
3
- Version: 0.2.2
3
+ Version: 0.3.0
4
4
  Summary: Lightweight CQRS library.
5
5
  Home-page: https://github.com/100nm/python-cq
6
6
  License: MIT
@@ -12,9 +12,9 @@ from ._core.message import (
12
12
  QueryBus,
13
13
  command_handler,
14
14
  event_handler,
15
- find_command_bus,
16
- find_event_bus,
17
- find_query_bus,
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
- "find_command_bus",
39
- "find_event_bus",
40
- "find_query_bus",
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 SimpleBus[I, O](BaseDispatcher[I, O], Bus[I, O]):
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](BaseDispatcher[I, None], Bus[I, None]):
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
- def find_command_bus[T]() -> CommandBus[T]:
43
- return injection.find_instance(CommandBus)
42
+ @injection.inject
43
+ def get_command_bus[T](bus: CommandBus[T] = NotImplemented, /) -> CommandBus[T]:
44
+ return bus
44
45
 
45
46
 
46
- def find_event_bus() -> EventBus:
47
- return injection.find_instance(EventBus)
47
+ @injection.inject
48
+ def get_event_bus(bus: EventBus = NotImplemented, /) -> EventBus:
49
+ return bus
48
50
 
49
51
 
50
- def find_query_bus[T]() -> QueryBus[T]:
51
- return injection.find_instance(QueryBus)
52
+ @injection.inject
53
+ def get_query_bus[T](bus: QueryBus[T] = NotImplemented, /) -> QueryBus[T]:
54
+ return bus
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "python-cq"
3
- version = "0.2.2"
3
+ version = "0.3.0"
4
4
  description = "Lightweight CQRS library."
5
5
  license = "MIT"
6
6
  authors = ["remimd"]
File without changes
File without changes
File without changes
File without changes