Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple Observers not working on singular State machine #384

Closed
MKhibkin1 opened this issue Apr 21, 2023 · 4 comments · Fixed by #387
Closed

Multiple Observers not working on singular State machine #384

MKhibkin1 opened this issue Apr 21, 2023 · 4 comments · Fixed by #387
Labels

Comments

@MKhibkin1
Copy link

  • Python State Machine version:
  • Python version:
  • Operating System:

Description

I would like to have multiple observers responding to an event from a statemachine.

What I Did

from statemachine import StateMachine, State


class MyObs(object):

    def __init__(self):
        pass

    def on_move_car(self):
        print("I observed moving from 1")
        return self

class MyObs2(object):
    def __init__(self):
        pass

    def on_move_car(self):
        print("I observed moving from 2")
        return self

class Car(StateMachine):
    def __init__(self):
        super().__init__()

    stopped = State(initial=True)
    moving = State()

    move_car = stopped.to(moving)
    stop_car = moving.to(stopped)

    @move_car.on
    def on_move_car(self):
        print("Im moving")


if __name__ == "__main__":

    car = Car()
    obs = MyObs()
    obs2 = MyObs2()
    car.add_observer(obs)
    car.add_observer(obs2)

    car.move_car()

I would expect to see both "I observed moving from 1" and "I observed moving from 2" but it seems the first observer to encounter the event will swallow the callback.

@MKhibkin1 MKhibkin1 changed the title Observer' Multiple Observers not working on singular State machine Apr 21, 2023
@fgmacedo fgmacedo added the bug label Apr 26, 2023
@fgmacedo
Copy link
Owner

fgmacedo commented Apr 26, 2023

Hi @MKhibkin1 , I was able to reproduce the issue. Thanks for reporting this.

The problem is that I've implemented an "unique" by name on the callback registry. I use the callback name to know if a callback is already registered.

Here I check if the callback is already in the list:

if func in self.items:
return

And here is how I compare two distinct callbacks (by it's name):

def __eq__(self, other):
return self.func == getattr(other, "func", other)

I'm thinking about using a qualified name to the callback, including a identification of the observer, so even with multiple observers we still get unique names.

@cmutzel
Copy link

cmutzel commented Jun 9, 2023

@fgmacedo - I am really loving your library and excited to see this issue fixed. It looks like you haven't created a release since doing so? Is there a way I could help you to do the release - would like to get this fix.

@fgmacedo
Copy link
Owner

Hi @cmutzel, thanks for your kind words. I was waiting to deliver a more significant release. But let's release what we already have implemented.

@fgmacedo
Copy link
Owner

New version 2.1.0 released! 🎉

https://python-statemachine.readthedocs.io/en/latest/releases/2.1.0.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants