statelyai / xstate

Actor-based state management & orchestration for complex app logic.
https://stately.ai/docs
MIT License
27.23k stars 1.26k forks source link

Bug: Simulated delayed transitions will not execute if guarded #5116

Closed anselanza closed 2 weeks ago

anselanza commented 2 weeks ago

XState version

XState version 5

Description

I have reduced this to a very simple, reproducible example at https://stately.ai/registry/editor/9ef8a9f6-fcef-4bbc-8e29-0e5d80c0d523?machineId=b68ae09b-11e3-4675-9bda-97a7a7504db0&mode=Simulate

An "after" (delayed) transition works as expected when simulating. But only if there is no guard condition on the transition.

As soon as there is a guard condition, even if that guard is trivial (it always returns true), the timeout is reached but the transition never happens.

In the screenshot below, notice that the "after 1 sec" event gets logged, but no transition to the "Active" state happens. Unless, of course, you explicitly click on the button for xstate.after(1000)#(machine).waiting which sort of defeats the purpose of simulating a delayed transition, and in any case behaves differently to the case in which there is no guard.

Screenshot 2024-11-05 at 16 53 20

Expected result

I would expect that, when simulating, guards would be evaluated after the given delay and, if evaluated to true, the transition to the target state should occur.

Actual result

The simulation works as expected if the guard is removed (the machine transitions to the target state after the delay), but when a guard is present then the timeout is logged but no transition actually happens.

Reproduction

https://stately.ai/registry/editor/9ef8a9f6-fcef-4bbc-8e29-0e5d80c0d523?machineId=b68ae09b-11e3-4675-9bda-97a7a7504db0&mode=Simulate

Additional context

using Xstate v5

anselanza commented 2 weeks ago

Apologies, this should probably be filed under Stately Studio Issues