Hierarchical state machine framework in Javascript