Skip to content

Commit

Permalink
Refactor registering events and command handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
lucassus committed Sep 20, 2023
1 parent 5f8a2a8 commit 50564d8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
11 changes: 4 additions & 7 deletions app/modules/accounts/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ def _create_commands_container(bus: MessageBus) -> AppContainer:
]
)

for command, handler in container.command_handlers().items():
bus.register(command, handler)

for event, handlers in container.event_handlers().items():
for handler in handlers:
bus.listen(event, handler)

return container


Expand All @@ -51,6 +44,10 @@ def bootstrap_accounts_module(mappers: registry, bus: MessageBus) -> Authenticat
start_mappers(mappers)

container = _create_commands_container(bus)

bus.register_all(container.command_handlers())
bus.listen_all(container.event_handlers())

_create_queries_container()

return container.authentication()
8 changes: 2 additions & 6 deletions app/modules/projects/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,5 @@ def bootstrap_projects_module(

container = _create_container(bus, authentication)

for command, handler in container.command_handlers().items():
bus.register(command, handler)

for event, handlers in container.event_handlers().items():
for handler in handlers:
bus.listen(event, handler)
bus.register_all(container.command_handlers())
bus.listen_all(container.event_handlers())
9 changes: 9 additions & 0 deletions app/shared/message_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,14 @@ def register(

self._command_handlers[command_class] = cast(CommandHandler[Any, CommandThatReturns[Any]], handler)

def register_all(self, command_handlers: dict[type[C], CommandHandler[C, CR]]):
for command, handler in command_handlers.items():
self.register(command, handler)

def listen(self, event_class: type[Event], handler: EventHandler[E]):
self._event_listeners[event_class].append(handler)

def listen_all(self, event_handlers: dict[type[Event], list[EventHandler[E]]]):
for event, handlers in event_handlers.items():
for handler in handlers:
self.listen(event, handler)

0 comments on commit 50564d8

Please sign in to comment.