Skip to content

Commit

Permalink
Merge pull request #198 from WinterFramework/fix-domain-event-dispatcher
Browse files Browse the repository at this point in the history
Fix early instantiating of global_domain_event_dispatcher
  • Loading branch information
pristupa authored Jun 1, 2021
2 parents 5c40e8d + c3f1a99 commit c8a4b51
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [9.1.0] - 2021-06-01

### Bugfixes
- global_domain_event_dispatcher now lazily loads injector which could be empty if importing too early

## [9.0.0] - 2021-03-31

### New features
Expand Down
2 changes: 1 addition & 1 deletion requirements/base.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
dataclasses>=0.6
djangorestframework>=3.9.0
Django>=1.11.16
Django<3.0.0>=1.11.16
docstring_parser>=0.1
drf-yasg>=1.13.0
furl==2.0.0
Expand Down
2 changes: 1 addition & 1 deletion winter/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '9.0.0'
__version__ = '9.1.0'
2 changes: 1 addition & 1 deletion winter_ddd/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .aggregate_root import AggregateRoot
from .domain_event import DomainEvent
from .domain_event_dispatcher import global_domain_event_dispatcher
from .domain_event_handler import domain_event_handler
from .domain_event_handler import global_domain_event_dispatcher
from .domain_events import DomainEvents
6 changes: 4 additions & 2 deletions winter_ddd/domain_event_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class DomainEventDispatcher:
def __init__(self):
self._subscriptions: Dict[EventFilter, List[DomainEventSubscription]] = {}
self._event_type_to_event_filters_map: Dict[Type[DomainEvent], List[EventFilter]] = {}
self._injector = get_injector()

def add_subscription(self, subscription: DomainEventSubscription):
self._subscriptions.setdefault(subscription.event_filter, []).append(subscription)
Expand All @@ -40,9 +39,12 @@ def dispatch(self, events: Iterable[DomainEvent]):

for event_filter, events in filtered_events.items():
for subscription in self._subscriptions.get(event_filter, []):
handler_instance = self._injector.get(subscription.handler_class)
handler_instance = get_injector().get(subscription.handler_class)
if subscription.collection:
subscription.handler_method(handler_instance, events)
else:
for event in events:
subscription.handler_method(handler_instance, event)


global_domain_event_dispatcher = DomainEventDispatcher()
5 changes: 1 addition & 4 deletions winter_ddd/domain_event_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@
import re

from winter.core.utils.typing import is_union
from .domain_event_dispatcher import DomainEventDispatcher
from .domain_event_dispatcher import global_domain_event_dispatcher
from .domain_event_subscription import DomainEventSubscription

domain_events_class_name_pattern = re.compile(r'typing.List\[.+\]')


global_domain_event_dispatcher = DomainEventDispatcher()


# noinspection PyPep8Naming
class domain_event_handler:
"""Decorator for method in class"""
Expand Down

0 comments on commit c8a4b51

Please sign in to comment.