Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions cq/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
from ._core.queuing.queues.abc import Consumer, Delivery, Producer, Queue
from ._core.queuing.queues.memory import MemoryQueue
from ._core.related_events import AnyIORelatedEvents, RelatedEvents
from ._core.routing.command_pipeline import (
ContextCommandPipeline as _ContextCommandPipeline,
)
from ._core.routing.command_pipeline import ContextCommandPipeline
from ._core.routing.di import DIAdapter
from ._core.routing.dispatchers.abc import Dispatcher
from ._core.routing.dispatchers.bus import Bus
Expand Down Expand Up @@ -77,9 +75,4 @@
new_event_bus = __router__.new_event_bus
new_query_bus = __router__.new_query_bus


class ContextCommandPipeline[C: Command](_ContextCommandPipeline[C]):
__slots__ = ()

def __init__(self, di: DIAdapter = __router__.di) -> None:
super().__init__(di)
ContextCommandPipeline._set_default_di(__router__.di)
11 changes: 9 additions & 2 deletions cq/_core/routing/command_pipeline.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TYPE_CHECKING, Any, Self, overload
from typing import TYPE_CHECKING, Any, ClassVar, Self, overload

from cq._core.common.typing import Decorator
from cq._core.message import Command, CommandBus, Query, QueryBus
Expand All @@ -18,7 +18,10 @@ class ContextCommandPipeline[C: Command](ContextPipeline[C]):

__query_dispatcher: Dispatcher[Query, Any]

def __init__(self, di: DIAdapter) -> None:
__default_di: ClassVar[DIAdapter]

def __init__(self, di: DIAdapter | None = None, /) -> None:
di = di or self.__default_di
super().__init__(LazyDispatcher(CommandBus, di))
self.__query_dispatcher = LazyDispatcher(QueryBus, di)

Expand Down Expand Up @@ -50,3 +53,7 @@ def query_step[Q: Query]( # type: ignore[misc]
/,
) -> Any:
return self.step(wrapped, dispatcher=self.__query_dispatcher)

@classmethod
def _set_default_di(cls, di: DIAdapter, /) -> None:
cls.__default_di = di
4 changes: 4 additions & 0 deletions cq/_core/routing/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Any, Self

from cq._core.message import Command, Event, Query
from cq._core.routing.command_pipeline import ContextCommandPipeline
from cq._core.routing.di import DIAdapter, NoDI
from cq._core.routing.dispatchers.bus import Bus, SimpleBus, TaskBus
from cq._core.routing.handler import (
Expand Down Expand Up @@ -54,6 +55,9 @@ def query_handler(self) -> HandlerDecorator[Query, Any]:
def query_types(self) -> KeysView[type[Query]]:
return self.__query_registry.message_types

def command_pipeline[T](self) -> ContextCommandPipeline[T]:
return ContextCommandPipeline(self.__di)

def new_command_bus(self) -> Bus[Command, Any]:
bus = SimpleBus(self.__command_registry)

Expand Down
4 changes: 1 addition & 3 deletions tests/test_context_command_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ class Context:
bar: Bar
baz: Baz

pipeline: ContextCommandPipeline[Command0] = ContextCommandPipeline(
router.di
)
pipeline: ContextCommandPipeline[Command0] = router.command_pipeline()

pipeline.add_static_step(Command1())

Expand Down