matthewp / robot

🤖 A functional, immutable Finite State Machine library

Home Page:https://thisrobot.life

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using immediate AND transition

arggh opened this issue · comments

commented

I apologize if this is a silly question, I just got started with robot.

I wrote this state machine (codesandbox here) and it didn't work as I expected:

const canSubmit = () => false; // will never approve!
const machine = createMachine({
  idle: state(transition("submit", "validate")),
  validate: state(
    immediate("submission", guard(canSubmit)),
    transition("revalidate", "submission", guard(canSubmit))
  ),
  submission: state()
});

What I thought would happen

After sending submit, I expected the machine to be in validate state, waiting for the revalidate transition.

What happened

Machine went back to idle

Why it's strange

In the guide, there's this code:

const machine = createMachine({
  idle: state(
    transition('submit', 'validate')
  ),
  validate: state(
    immediate('submission', guard(canSubmit)),
    immediate('idle') // <--- you can omit this line if you want to?
  ),
  submission: state()
});

...but to go back to idle then the guard prevents proceeding to 'submission', it's completely unnecessary to call immediate('idle'). The machine will go back to idle without it, too.

Is there a fault in my thinking, or a fault in the robot?🤔

Interesting. I sort of expect what you expect as well. I'll look at the tests and the code and get back to you.

It's a bug, fix coming soon.

Fixed in https://github.com/matthewp/robot/releases/tag/v0.2.12, thanks for finding this interesting bug!