Skip to content

fix: Multiple observers can watch the same callback#387

Merged
fgmacedo merged 1 commit intodevelopfrom
macedo/fix-384-multiple-observers
May 5, 2023
Merged

fix: Multiple observers can watch the same callback#387
fgmacedo merged 1 commit intodevelopfrom
macedo/fix-384-multiple-observers

Conversation

@fgmacedo
Copy link
Copy Markdown
Owner

@fgmacedo fgmacedo commented May 5, 2023

Fixes #384 .

Multiple observers now can watch the same callback

we register multiple observers to the same named callback.

This works also as a regression test.

>>> from statemachine import State
>>> from statemachine import StateMachine

>>> class MyObs:
...     def on_move_car(self):
...         print("I observed moving from 1")

>>> class MyObs2:
...     def on_move_car(self):
...         print("I observed moving from 2")
...


>>> class Car(StateMachine):
...     stopped = State(initial=True)
...     moving = State()
...
...     move_car = stopped.to(moving)
...     stop_car = moving.to(stopped)
...
...     def on_move_car(self):
...         print("I'm moving")

Running:

>>> car = Car()
>>> obs = MyObs()
>>> obs2 = MyObs2()
>>> car.add_observer(obs)
Car(model=Model(state=stopped), state_field='state', current_state='stopped')

>>> car.add_observer(obs2)
Car(model=Model(state=stopped), state_field='state', current_state='stopped')

>>> car.add_observer(obs2)  # test to not register duplicated observer callbacks
Car(model=Model(state=stopped), state_field='state', current_state='stopped')

>>> car.move_car()
I'm moving
I observed moving from 1
I observed moving from 2
[None, None, None]

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented May 5, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 1 Code Smell

No Coverage information No Coverage information
0.0% 0.0% Duplication

@codecov
Copy link
Copy Markdown

codecov Bot commented May 5, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (c60f7fe) 100.00% compared to head (c22dcda) 100.00%.

Additional details and impacted files
@@            Coverage Diff            @@
##           develop      #387   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           20        20           
  Lines          976       985    +9     
  Branches       161       163    +2     
=========================================
+ Hits           976       985    +9     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
statemachine/callbacks.py 100.00% <100.00%> (ø)
statemachine/dispatcher.py 100.00% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@fgmacedo fgmacedo marked this pull request as ready for review May 5, 2023 13:20
Comment on lines +40 to +59
def _build_attr_wrapper(attr: str, obj):
# if `attr` is not callable, then it's an attribute or property,
# so `func` contains it's current value.
# we'll build a method that get's the fresh value for each call
getter = attrgetter(attr)

def wrapper(*args, **kwargs):
return getter(obj)

return wrapper


def _build_sm_event_wrapper(func):
"Events already have the 'machine' parameter defined."

def wrapper(*args, **kwargs):
kwargs.pop("machine", None)
return func(*args, **kwargs)

return wrapper
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

author's note: Refac extracting methods from ensure_callable

@fgmacedo fgmacedo merged commit 2de95fb into develop May 5, 2023
@fgmacedo fgmacedo deleted the macedo/fix-384-multiple-observers branch May 5, 2023 17:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Multiple Observers not working on singular State machine

1 participant