Skip to content

Commit

Permalink
Adding simple event system.
Browse files Browse the repository at this point in the history
  • Loading branch information
rcschrg committed Jan 31, 2024
1 parent 835ed31 commit cd45bb8
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions mango/agent/role.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def subscribe_send(self, role: Role, method: Callable):
else:
self._send_msg_subs[role] = [method]

def emit_event(self, event: Any, event_source: Any):
def emit_event(self, event: Any, event_source: Any = None):
subs = self._role_event_type_to_handler[type(event)]
for _, method in subs:
method(event, event_source)
Expand Down Expand Up @@ -438,7 +438,7 @@ def subscribe_event(self, role: Role, event_type: Any, handler_method: Callable)
:param event_type: the event type you want to handle
:type event_type: Any
"""
self._role_handler.subscribe_event(role, event_type, event_type)
self._role_handler.subscribe_event(role, event_type, handler_method)

@property
def addr(self):
Expand Down
6 changes: 3 additions & 3 deletions tests/unit_tests/role/role_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,22 +124,22 @@ def setup(self) -> None:
def test_emit_event():
# GIVEN
role_handler = RoleHandler(None, None)
context = RoleContext(None, None, RoleHandler, None, None)
context = RoleContext(None, None, role_handler, None, None)
ex_role = SubRole()
ex_role2 = RoleHandlingEvents()
context.add_role(ex_role)
context.add_role(ex_role2)
event = Event("Here I am!")

# WHEN
role_handler.emit_event(event=event)
context.emit_event(event=event)

# THEN
assert ex_role2.event == event
assert ex_role2.source is None

# WHEN
role_handler.emit_event(event=event, event_source=ex_role)
context.emit_event(event=event, event_source=ex_role)

# THEN
assert ex_role2.event == event
Expand Down

0 comments on commit cd45bb8

Please sign in to comment.