ifandelse / machina.js

js ex machina - finite state machines in JavaScript
http://machina-js.org/
Other
1.93k stars 147 forks source link

Deep states? #88

Closed hippich closed 9 years ago

hippich commented 9 years ago

I am contemplating idea of making my own state machine lib to handle deep states situation, but really don't want to make another wheel :)

Is there a way, or plans or interest in making deep states possible? Quite often when dealing with complex UI you might benefit from having of more than just one level of states. Something like:

Key here is that _onEnter and _onExit will be called on each parent too when necessary. I.e. if my current state is unauthenticated.authenticate.login and after successful authentication application state switches to authenticated.dashboard.overview following calls should fire:

  1. unauthenticated.authenticate.login._onExit();
  2. unauthenticated.authenticate._onExit();
  3. unauthenticated._onExit();
  4. authenticated._onEnter();
  5. authenticated.dashboard._onEnter();
  6. authenticated.dashboard.overview._onEnter();

But most importantly, if later state changed to authenticated.dashboard.users, only following calls should be made:

  1. authenticated.dashboard.overview._onExit();
  2. authenticated.dashboard.users._onEnter();

Why this is important? Quite often your frontend app need to use multiple templates inside each other. And tracking which template currently shown on the screen and should I load and display new one is tricky. Another approach is to render each page from top-level, but this is wasteful. Therefore I am looking for more tree-like state machine solution.

hope all of this make sense ;)

ifandelse commented 9 years ago

@hippich Have you read the bits about hierarchical FSMs in machina? If not, see this.

hippich commented 9 years ago

This is exactly what I was looking for! Thank you!