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.