In Memory Event Bus¶
in_memory_event_bus
¶
In-memory implementation of the EventBusPort port.
Dispatches events to multiple registered handlers (fan-out) and
commands to a single registered handler. Handlers are looked up
by the exact type of the message.
InMemoryEventBus
¶
Bases: EventBusPort[EventPayloadType, CommandPayloadType]
In-memory event bus with separate event/command dispatch.
Attributes:
| Name | Type | Description |
|---|---|---|
_event_handlers |
dict[type[Any], list[EventHandler[Any]]]
|
Per-event-type list of handlers. |
_command_handlers |
dict[type[Any], CommandHandler[Any]]
|
Per-command-type single handler. |
Source code in src/forging_blocks/infrastructure/event_buses/in_memory_event_bus.py
register_handler(message_type: type[Any], handler: Any) -> None
¶
Register a handler for a message type.
For event types, multiple handlers can be registered (fan-out).
For command types, only one handler is allowed per type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message_type
|
type[Any]
|
The message class to handle. |
required |
handler
|
Any
|
A handler instance. |
required |
Source code in src/forging_blocks/infrastructure/event_buses/in_memory_event_bus.py
publish(event: Event[EventPayloadType]) -> Result[None, EventBusError]
async
¶
Publish an event to all registered handlers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
Event[EventPayloadType]
|
The domain event. |
required |
Returns:
| Type | Description |
|---|---|
Result[None, EventBusError]
|
|
Result[None, EventBusError]
|
handler raises. |
Source code in src/forging_blocks/infrastructure/event_buses/in_memory_event_bus.py
send(command: Command[CommandPayloadType]) -> Result[None, EventBusError]
async
¶
Send a command to its registered handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
Command[CommandPayloadType]
|
The command. |
required |
Returns:
| Type | Description |
|---|---|
Result[None, EventBusError]
|
|
Result[None, EventBusError]
|
handler raises or no handler is registered. |