• Hello Experts,
    I read the thread you mentioned above, and I am trying to turn its content into a set of design and understanding statements. Could you tell me if I correctly understand the way it works? I hope I won’t confuse others with silly ideas...
    These statements ensure that after doing a save() of the software, unplugging the Espruino to un-power it, re-powering it, the software will startup and reach a deterministic state at the end of the onInit() execution.
    1 - Global constants are declared as var XXX= xxx. At re-power up, they will be reloaded from flash with their value as they were initialized the first time when the software has been downloaded from the IDE and subsequently executed from top down to bottom.
    2 - Global variables are initialized the first time the program is downloaded to Espruino from the IDE (as said in 1). On save(), their current value is saved in flash. At power up, global variables are reset with the values stored in flash (due to the last save()). So, if they have been changed by program execution and not re-saved, their current value is lost and replaced by the flash previously saved value.
    3 - On save() execution, function declaration and global code are saved in the flash memory. An instantiated module like ‘var modulevar = require(xxx)’ is saved and is recovered with full instantiation and capability at re-power up (like any other global variable previously saved) .
    4 - At powerup, the following operations take place (in order) :
    a/ Global variables and code is downloaded from flash memory to ram. There is no execution from top to bottom of the code. So initialization done in the global context is never replayed after the first IDE download and execution.
    b/ onInit() function is invoked
    c/ when onInit() ends, the system goes to 'Idle' if no event is present in the JavaScript queue (We define 'Idle' as a state where no event is present in the event queue and any processing triggered by previous events is terminated).
    5 - onInit() must ensure that at its end, all variables are in the required state.
    6 - As consequence of 5, onInit() function must take care of changing global variables to desired values if the saved values are no longer relevant.
    7 - onInit() is executed in a time period (init must not last forever! ) and must ensure consistency inside its own execution scope. Javascript is event oriented and not sequence oriented. It means that programmer is responsible for dependencies during asynchronous execution. Two patterns are available to ensure sequential execution (and therefore ensure dependencies constraints):
    a/nesting callbacks that will execute in order - called when previous conditions are met-,
    or,
    b/place initialization statements in time order using timeout function which call initialization functions in order of increasing timeout values.

    May be there are other ideas I have not reported.

    I think my code follow these rules, but it doesn't work! (yet).

    Thanks a lot for your help.

    GeekBot

About

Avatar for GeekBot @GeekBot started