You are reading a single comment by @Gordon and its replies. Click here to read the full conversation.
  • I think https://www.espruino.com/Saving probably covers this, but:

    This feature of restoring "timers, watches, and even pin state" seems a bit dangerous to me

    It didn't used to, but it got added because that's what users asked for.

    The idea is that it's what the average user would expect - as you start doing more serious stuff you'll probably start moving towards upload+save without running onInit first, or even saving directly to flash.

    Would it be useful or even doable for save() to just save code without any other state that runs code (timers,..)?

    Go to the Web IDE, Communications, Save on send, and choose to save directly to flash. I think this is what you're after...

    Also I see command history is saved too probably eating memory

    Command history is deleted automatically as and when memory runs low :)

    save with no hints or more detail

    Thanks for the hint - I'll make sure that links to https://www.espruino.com/Saving . If you think that could have anything added that cleared up the situation a bit then I'd be really interested to hear though.

  • If you think that could have anything added that cleared up the situation a bit then I'd be really interested to hear

    Well after also reading the Saving page it is a bit more clear but still from all of this I did not get the idea that it is dangerous and definitely not what I wanted to use or that I should be extra careful before using it. The Summary in Saving page starts with

    Just type save() on the left-hand side of the IDE and the current state of Espruino including all saved code will be written so that it is loaded at boot time.

    and then hints that there are maybe better ways and then pros and cons are discussed but nothing that would warn me that this automatic save needs serious thinking when to call it in all but nontrivial cases. So I'd like to see some bigger warning there.

    The API reference says

    When Espruino powers on, it will resume from where it was when you typed save(). If you want code to be executed right after loading (for instance to initialise devices connected to Espruino), add an init event handler to E with E.on('init', function() { ... your_code ... });. This will then be automatically executed by Espruino every time it starts.

    This does not explain potential issues. Also BTW is the on init function always guaranteed to run before e.g. my just restored setInterval will fire? Should I set E.on(init) before calling save() so it gets saved too? This may not be clear from those sentences.

    I would add something like this to save() API reference

    Please note that if you have watches and intervals already set at the time of save, they will continue to run after resume so use the on init method to restore hardware to expected state before they will run again. If you omit this it may lead to unexpected and possibly serious failures if your code does not handle such state properly. This method is targeted for simple use cases where it will 'just work' however it may not be the best for more complex scenarios. For more details and alternatives see page Saving. See dump() to understand what gets saved.

About

Avatar for Gordon @Gordon started