1

Here is an example of state machine I intend to build

  this.statemachine =
      {
           state: 'idle',
           transitions:
           {
                  idle:{
                          ebaseinit: function ()
                          {
                             console.log("Firebase Init complete");
                          } ,

                          coreinit : function ()
                          {
                             console.log("Firestore Init complete");
                          }

                       },
                   ready:
                      {
                         fetchdata : function ()
                         {
                           console.log("Getting                                   data");
                         }
                         

                      }    
           }
      }        
Depending on the event that occurs I need to invoke respective function. Tried this.statemachine.transitions.indexof("idle") It does not work. Any suggestion to implement accessing the function without using direct numbers like this.statemachine.transitions[1] etc should help

1
  • 2
    this.statemachine.transitions[ this.statemachine.state ]? Commented May 24, 2018 at 9:36

2 Answers 2

1

You can catch the event name and use it as an object key.

this.statemachine.transitions['ready']

Sign up to request clarification or add additional context in comments.

Comments

1

First thing you have to understand is statemachine.transitions is not an array it is an object thats why you cant use indexOf on it

if(this.statemachine.transitions)
 let obj = this.statemachine.transitions[statemachine.state]

or

You can use a for loop to iterate through the object and find what you are looking for

if (this.statemachine.transitions){
      for (var key in this.statemachine.transitions) {
        if (this.statemachine.transitions.hasOwnProperty(key)) {
          if(this.statemachine.state == key){
            console.log(this.statemachine.transitions[key])
          }
        }
      }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.