matthewp / robot

🤖 A functional, immutable Finite State Machine library

Home Page:https://thisrobot.life

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Nested Docs and Usage

Westbrook opened this issue · comments

Currently, the docs outline the current workflow for using the nested stoplight example:

let service = interpret(stoplight, () => {});

service.send('next');
console.log(service.machine.current); // yellow

service.send('next');
console.log(service.machine.current); // red

let child = service.send;
console.log(child.machine.current); // walk

child.send('toggle');
console.log(child.machine.current); // dontWalk
console.log(service.machine.current); // green

It seems like let child = service.send; should be let child = service.child;, right? If so, easy enough to change.

However, when applying the documented code, the final console.log returns red for me. https://stackblitz.com/edit/robot-stoplight?file=index.js There seems to be some possibilities here, but the ones I can see are lacking:

It is possible that the child machine should be:

const stopwalk = createMachine({
  walk: state(
    transition('toggle', 'dontWalk')
  ),
  dontWalk: state(),  // <--- makes it "final"?
});

And, the docs should run two paths through the machine, getting the "new" child the second time?

let service = interpret(stoplight, () => {});
service.send('next');
console.log(service.machine.current); // yellow

service.send('next');
console.log(service.machine.current); // red

let child = service.child;
console.log(child.machine.current); // walk

child.send('toggle');
console.log(child.machine.current); // dontWalk
console.log(service.machine.current); // green

service.send('next');
console.log(service.machine.current); // yellow

service.send('next');
console.log(service.machine.current); // red

child = service.child;     // <--- magic happens here when you get the NEW child machine 
console.log(child.machine.current); // walk

child.send('toggle');
console.log(child.machine.current); // dontWalk
console.log(service.machine.current); // green

Code for alternate versions: https://stackblitz.com/edit/robot-stoplight?file=alt.js

Yes 🤦‍♀️you're absolutely right. My mistake. I'll update this when I have some time (probably this weekend). Thanks for the catch!

Btw, making this simpler and more integrated with Robot is something I want to do eventually (for ex., have nested as an official API). Probably should have another issue for that.

This has been updated! Thanks for the notice.