ingesolvoll / re-statecharts

re-frame integration for clj-statecharts
Eclipse Public License 2.0
20 stars 1 forks source link

Race condition when ::rs/start is followed by ::rs/transition #3

Open brjann opened 2 years ago

brjann commented 2 years ago

I'm switching from kee-frame.fsm.alpha to re-statecharts and this far I really like the new library!

I think I have stumbled upon a race condition though. I cannot return this from an effect

{:dispatch-n [[::rs/start fsm]
              [::rs/transition (:id fsm) :some-event]]

The :some-event transition does not happen. I guess because rs/integrate does not start the fsm immediately but instead dispatches ::rs/init which will get executed after my call to ::rs/transition

ingesolvoll commented 2 years ago

Hm, that’s an interesting case. I think you could even say that theoretically the transition could happen before the start because re-frame doesn’t guarantee order? But I think your analysis is correct, start has not finished the things it needs to do before transition happens.

The immediate suggestion, without looking further into this, would be to either separate the 2 events, or just have the start state of the fsm be whatever some-event causes.

brjann commented 2 years ago

Thanks!

I think you could even say that theoretically the transition could happen before the start because re-frame doesn’t guarantee order?

Oh, I was pretty sure that events in dispatch-n are guaranteed order?

Edit: They are: https://day8.github.io/re-frame/api-builtin-effects/

have the start state of the fsm be whatever some-event causes.

Yes, that is actually what I did. For different reasons I would have liked to be able to dispatch like that, but your suggested method also works. I just wanted you to know of this potential race condition if it is not intended.