|
1 | 1 | from dataclasses import dataclass |
2 | | -from typing import Annotated, Any |
| 2 | +from typing import TYPE_CHECKING, Annotated, Any |
3 | 3 |
|
4 | 4 | from fastapi import BackgroundTasks, Depends |
5 | 5 | from injection.ext.fastapi import Inject |
6 | 6 |
|
7 | | -from cq import Bus, Command, CommandBus, DeferredBus, Event, EventBus, Query, QueryBus |
| 7 | +from cq import ( |
| 8 | + Command, |
| 9 | + CommandBus, |
| 10 | + DeferredDispatcher, |
| 11 | + Dispatcher, |
| 12 | + Event, |
| 13 | + EventBus, |
| 14 | + Query, |
| 15 | + QueryBus, |
| 16 | +) |
8 | 17 |
|
9 | | -__all__ = ("DeferredCommandBus", "DeferredEventBus", "DeferredQueryBus") |
| 18 | +__all__ = ( |
| 19 | + "DeferredCommandBus", |
| 20 | + "DeferredEventBus", |
| 21 | + "DeferredQueryBus", |
| 22 | + "FastAPIDeferredDispatcher", |
| 23 | +) |
10 | 24 |
|
11 | 25 |
|
12 | 26 | @dataclass(repr=False, eq=False, frozen=True, slots=True) |
13 | | -class FastAPIDeferredBus[I](DeferredBus[I]): |
| 27 | +class FastAPIDeferredDispatcher[I](DeferredDispatcher[I]): |
14 | 28 | background_tasks: BackgroundTasks |
15 | | - bus: Bus[I, Any] |
| 29 | + dispatcher: Dispatcher[I, Any] |
16 | 30 |
|
17 | 31 | async def defer(self, input_value: I, /) -> None: |
18 | | - self.background_tasks.add_task(self.bus.dispatch, input_value) |
| 32 | + self.background_tasks.add_task(self.dispatcher.dispatch, input_value) |
19 | 33 |
|
20 | 34 |
|
21 | 35 | async def new_deferred_command_bus[T]( |
22 | 36 | background_tasks: BackgroundTasks, |
23 | 37 | command_bus: Inject[CommandBus[T]], |
24 | | -) -> DeferredBus[Command]: |
25 | | - return FastAPIDeferredBus(background_tasks, command_bus) |
| 38 | +) -> DeferredDispatcher[Command]: |
| 39 | + return FastAPIDeferredDispatcher(background_tasks, command_bus) |
26 | 40 |
|
27 | 41 |
|
28 | 42 | async def new_deferred_event_bus( |
29 | 43 | background_tasks: BackgroundTasks, |
30 | 44 | event_bus: Inject[EventBus], |
31 | | -) -> DeferredBus[Event]: |
32 | | - return FastAPIDeferredBus(background_tasks, event_bus) |
| 45 | +) -> DeferredDispatcher[Event]: |
| 46 | + return FastAPIDeferredDispatcher(background_tasks, event_bus) |
33 | 47 |
|
34 | 48 |
|
35 | 49 | async def new_deferred_query_bus[T]( |
36 | 50 | background_tasks: BackgroundTasks, |
37 | 51 | query_bus: Inject[QueryBus[T]], |
38 | | -) -> DeferredBus[Query]: |
39 | | - return FastAPIDeferredBus(background_tasks, query_bus) |
| 52 | +) -> DeferredDispatcher[Query]: |
| 53 | + return FastAPIDeferredDispatcher(background_tasks, query_bus) |
40 | 54 |
|
41 | 55 |
|
42 | | -DeferredCommandBus = Annotated[DeferredBus[Command], Depends(new_deferred_command_bus)] |
43 | | -DeferredEventBus = Annotated[DeferredBus[Event], Depends(new_deferred_event_bus)] |
44 | | -DeferredQueryBus = Annotated[DeferredBus[Query], Depends(new_deferred_query_bus)] |
| 56 | +if TYPE_CHECKING: |
| 57 | + type DeferredCommandBus = DeferredDispatcher[Command] |
| 58 | + type DeferredEventBus = DeferredDispatcher[Event] |
| 59 | + type DeferredQueryBus = DeferredDispatcher[Query] |
| 60 | + |
| 61 | +else: |
| 62 | + DeferredCommandBus = Annotated[ |
| 63 | + DeferredDispatcher[Command], |
| 64 | + Depends(new_deferred_command_bus, use_cache=False), |
| 65 | + ] |
| 66 | + DeferredEventBus = Annotated[ |
| 67 | + DeferredDispatcher[Event], |
| 68 | + Depends(new_deferred_event_bus, use_cache=False), |
| 69 | + ] |
| 70 | + DeferredQueryBus = Annotated[ |
| 71 | + DeferredDispatcher[Query], |
| 72 | + Depends(new_deferred_query_bus, use_cache=False), |
| 73 | + ] |
0 commit comments