Javascript - Configuration steps

1

I have a problem that is: I need to start my application to make a series of settings.

I tried to do using state machine, and I ended up finding this: link

events: [           
        { name: 'start', from: ['none', 'getInformations'], to: 'getInformations' },
        { name: 'getInformations', from: 'getInformations', to: 'getTuner' },
        { name: 'getTuner', from: 'getTuner', to: 'getNetworkInfo' },
        { name: 'getNetworkInfo', from: 'getNetworkInfo', to: 'none' }
      ],

In case, when some step fails, I can not get back to it because it has already been defined where it would go.

I really need to create a configuration step so I have the flexibility to choose where I want to go and to respect asynchronous calls. Anyone have any tips on how to do this? Without using state machine, the only solution I can think of is:

    while (validacao) {
        switch(estado) {
             case: 0:
                 fazAlgumaCoisa();
                 break;
              case 1:
                  fazOutraCoisa();
                  break;
        }
        }

fazAlgumaCoisa() {
   http.get('blablabla:8080').succes(){
     estado = 1;
}
}

But I do not see this as an elegant solution. If someone can give me a light, I thank you very much.

EDIT : Let's say I have 4 steps that are required to make my application work and need to run one after another:

step1 - > step2 - > step3 - > step4

However, in step 2, I got an error and need to move it to step 2 again.

How could I do this?

EDIT2 : After a few readings in several places, I came up with this code (illustrative examples only):

    var nextState = 'stopped';

var states = {

    stopped : function() {
        console.log("in stopped");
        nextState = 'started';

    },

    started : function() {
        console.log("in started");
        nextState = 'finished';
    },

    finished: function () {
        console.log("in finished");
        nextState = 'finished';
        clearInterval(interval);
    }
};

var interval = setInterval(function() {
    states[nextState]();
}, 300);

I'm not sure if this is a good practice and how it behaves in memory, but it seems to me a pleasant solution, considering that setInterval will be destroyed when it reaches the last step. Also, accessing the functions will be extremely fast since I'm passing the correct index to the array that contains my instructions.

    
asked by anonymous 14.10.2015 / 14:50

1 answer

2

You can write your state machine "on hand" using mutually recursive functions:

function faz_coisas(onDone){

    function passo1(){
        umaCoisaAssincrona(function(result){
          blah();
          passo2();
        });
    }

    function passo2(){
        ajax({
           onsuccess: passo3,
           onerror: passo2
        });
    }

    function passo3(){
        outracoisa(function(){
            onDone(meu_resultado);
        });
    }

    passo1();
}

//chamando o inicializador:
faz_coisas(function(result){
   console.log("tá tudo pronto");
});

The basic way to write asynchronous functions in Javascript is through something called "continuation passing style": its functions call a continuation function instead of returning values normally. In case of step1, step2 and step3, where the next step is always the same, this is quite simple (just call the next step function directly) but in case of reusable functions like "doing_things" you do not know a priori which function runs after you're done. What you can do then is to receive this continuation as a parameter (our "onDone").

Now some more advanced questions: writing the code in continuation-passing-style we can do anything we want. However, our program is much less structured, ugly and annoying to write - for example, the loop of step2 has to be done with a recursive call instead of with a while. In a way, we can say that continuation-passing-style is a very low level of programming.

If you want something more level and structured, you can take a look at some library of asynchronous programming (which uses callbacks or promisses) or, for me that is ideal, use one code preprocessor that extends the Javascript syntax with generators .

    
14.10.2015 / 20:43