Stately.js icon indicating copy to clipboard operation
Stately.js copied to clipboard

order of states

Open supipd opened this issue 13 years ago • 7 comments

Citate : The machine will transition into the initial state (the first attached stateObject) immediately.

In Javascript order of object properties is not guaranted. For exact order must be array used, isn't it ? So, instead

     { stateX:{...}, stateY:{...} } 

it will be something like

      [ { state:"X",  params: {...} },  { state:"Y", params:{...} } ]

supipd avatar Mar 06 '13 15:03 supipd

That is correct, the order of object properties isn't guaranteed, although it seems to work in all major browsers and even node.js.

If there is an problem with the initial state on some platforms, I'd rather extend the current implementation with an optional parameter to refer to the expected init state.

fschaefer avatar Mar 06 '13 15:03 fschaefer

@supipd have you got any examples where it was out of order? what engine was it on?

I've never seen it get out of order (in node.js), and I don't think the array approach is workable, or desirable.

rowanu avatar Mar 07 '13 14:03 rowanu

V8 and Javascript Gotchas 4.3 Other minor gotchas http://book.mixu.net/ch4.html

supipd avatar Mar 08 '13 14:03 supipd

I know what you mentioned is technically correct, but I asked if you'd actually seen it yourself? Please re-open this ticket (with details) if you have.

As @fschaefer mentioned, it doesn't seem to be an issue in practice. Until we know it is an issue that is causing people pain, I don't see any reason to spend large amounts of effort on it.

rowanu avatar Mar 08 '13 16:03 rowanu

OK, I accept closing this ticket, this is only last tearfull reaction. Most of modules and libraries (commercial too) I tried had minor or major bugs which can cause total crash of application, Today, when realtime, fullfeatured application in-browser (or nodejs) are IN, crashing due to "... its only web, simple restart, or use another, or ... " is not very pleasant. And user has full rights to throw tomatoes to us.

What needs more efforts ... solving problem from people, who knows source code to details, or study, debug, and correct another's work ?

Thanks @fschaefer for more correct reaction.

So, code, and views (V8 in nodejs (GoogleChrome too)):

var Stately = require('./stately.js');

console.log("Let's go on");
var stObj = {
'1': {          //logical state LOG1 ... device in ON state
    on: function() { console.log("going ON"); return this[1]; }
,   off: function() { console.log("going OFF"); return this[0]; }
,   err: function() { console.log("going ERR"); return this[2]; }
}
,   '0': {          //logical state LOG0 ... device in OFF state
    on: function() { console.log("going ON"); return this[1]; }
  , off: function() { console.log("going OFF"); return this[0]; }
  , err: function() { console.log("going ERR"); return this[2]; }
}
,   '2':{           //logical state ERR ... device in ERROR or UNKNOWN state
    on: function() { console.log("going ON"); return this[1]; }
  , off: function() { console.log("going OFF"); return this[0]; }
  , err: function() { console.log("going ERR"); return this[2]; }
  }
};
var machine = Stately.machine( stObj );

machine.onbefore0 = function(sS,eN,lSn,nSn) { console.log("onbefore: ",sS,eN,lSn,nSn); } 
machine.onenter0 = function(sS,eN,lSn,nSn) { console.log("onenter: ",sS,eN,lSn,nSn); }
machine.onleave0 = function(sS,eN,lSn,nSn) { console.log("onleave: ",sS,eN,lSn,nSn); }
machine.onbefore1 = function(sS,eN,lSn,nSn) { console.log("onbefore: ",sS,eN,lSn,nSn); }
machine.onenter1 = function(sS,eN,lSn,nSn) { console.log("onenter: ",sS,eN,lSn,nSn); }
machine.onleave1 = function(sS,eN,lSn,nSn) { console.log("onleave: ",sS,eN,lSn,nSn); }
machine.onbefore2 = function(sS,eN,lSn,nSn) { console.log("onbefore: ",sS,eN,lSn,nSn); }
machine.onenter2 = function(sS,eN,lSn,nSn) { console.log("onenter: ",sS,eN,lSn,nSn); }
machine.onleave2 = function(sS,eN,lSn,nSn) { console.log("onleave: ",sS,eN,lSn,nSn); }

console.log("INITIAL STATE (first writed object property is '1'): BUT :",machine.getMachineState(),
"\n----------------------------------------------                                       \n\
     I want default LOG1 ... ON                                     \n\
  For some reasons I cannot forget possibility of numerical object properties                   \n\
 Simple .... THIS IS DEFINED(and I have no reason to forbid) BY USER, not me as programmer. \n\
( real time, user re-configurable application ...                                           \n\
again USER RECONFIGURABLE ! )   and if somebody loves pure numbers .... ? let him live! \n\
"   );


function rndState () {
return parseInt(Math.random()*3);
}
setInterval( function() {
console.log("BEFORE: ",machine.getMachineState());
var transition = rndState ();
console.log("RANDOM: ",transition);
machine[['on','off','err'][transition]]();
console.log("AFTER: ",machine.getMachineState());
}, 1000)

nodeV8Stately nodeV8objOrder

supipd avatar Mar 09 '13 12:03 supipd

@supipd please take a look at the devel branch if it suit your needs. You can now pass in an additional argument with the name of the desired initial state: Stately(statesObject, initialStateName);

fschaefer avatar Mar 10 '13 08:03 fschaefer

Thanks a lot, it's better. (extreme: javascript allow use as property name empty string "" ... so problem is again in combination states '2' ... error '1': ... on '0' ... off '' ... unknown var initStateName; ( initStateName == "") === ( initStateName =='0') ... true )
But this is really extreme. Thanks again.

supipd avatar Mar 11 '13 04:03 supipd