How to run startup code once only?

The main view of my app has an associated JS file. In this file I run some initialisation code. I would expect this to be only run once when the app starts up, but in the debug log I can see that it is run twice. Why could this be?

If I refresh the app (Ctrl+R) it is only run once.

Is there a way to ensure that startup code can be run once only?

Incidentally, I ran this test on a real Android device.

Thanks!

You don’t have a good control over how many times code is run, especially in preview. Preview is watching files as they change, and may sometimes refresh multiple times (although we’ve improved that aspect A LOT lately), and it tries to restore the state the app was in before reify.

If you’re following best practices of reactive programming, you shouldn’t really care about how many times a given block of code is run. If you still do, and that’s business-critical, you can easily write a simple JS “request pool” that takes care about executing particular tasks only once.

All that said, a release build of an app should very rarely (if ever) run code more than once, unless explicitly instructed to do so.

Ok thanks. I should have thought about the preview re-loading things multiple times!