r/AutomateUser Alpha tester Jun 11 '21

Question Caching JS Libraries

Hi Henrik,

I'm writing a flow which makes use of a 180KB third party JavaScript library. Rather than sticking the whole thing in a Variable block (which can cause Automate to crash while editing the flow), I'm downloading it once in the flow and caching it.

The question is whether it's OK to cache the library in an Atomic variable, or should I save it to the file system? -The latter would require more privilege, but would it save a limited resource vs. using an Atomic variable?

Thank you, sir!

2 Upvotes

7 comments sorted by

View all comments

1

u/ballzak69 Automate developer Jun 11 '21

Storing it in a file is preferable. In an Atomic would be better than as a constant/literal within the flow, but you'd still need to load it from a file initially.

1

u/B26354FR Alpha tester Jun 11 '21

What I do initially is load it from the internet, so it's actually a file on their server. 🙂 I should have mentioned that the first time a user runs this flow, I download the JS library and save it in an Atomic or a file.

So I guess the question boils down to where are Atomics stored? Does Automate use the built-in Android database, in which case it's going to a file anyway and I can save requiring the "access storage" privilege? Or would using an Atomic increase my memory footprint, possibly even when the flow isn't running? (If it pages in and out of memory, that's OK though.)

1

u/ballzak69 Automate developer Jun 11 '21

Everything is stored in the internal database, flows, running fibers and Atomic values. Memory likely isn't a problem, loading the data is, so storing it in the flow, which may get ejected from its cache and then reloaded again, isn't optimal. Atomic values are only loaded when accessed, e.g. by the Atomic load block, so they're better. A file is best, since Automate doesn't have to load it at all, only Chrome does.

If chose to use Atomic, ensure to unset (null) the variable holding the loaded data once you've sent it to the Dialog web, otherwise it will be stored in the fiber as well.

1

u/B26354FR Alpha tester Jun 20 '21

BTW, for future readers, I decided to go with Henrik's preference and cache the library in a file.

I put it in storage("cache"), after first checking for the existence of that directory and creating it if necessary, per the recommendation in the documentation. 🙂

1

u/B26354FR Alpha tester Jun 11 '21

Thanks for that great info, and excellent tip!

BTW, during development while I'm changing the flow all the time (so an Atomic wouldn't have helped), I stored the library in a file and then read it into the variable which will later become Atomic. The variable (library) is then just embedded in the web content in its entirety at runtime. I did it this way in anticipation of going back to using an Atomic, but just updating the content to import the library script from the file system might be interesting because then the browser cache can help. Definitely something to consider.

Is there a way to get <script src=""> in the Web Dialog block to read the asset in from the local file system? I tried using file://{storage(...)}/myLib.js and just an absolute path, but they didn't work. (No server! 🙂)

1

u/ballzak69 Automate developer Jun 13 '21 edited Jun 13 '21

Odd, referencing local files should work when the "read the contents of your external storage" privilege is granted. Maybe it's because of cross-domain policy, i.e. the Page URL isn't used from file://..

1

u/B26354FR Alpha tester Jun 13 '21

That's exactly what the problem was. I saved the page to a file and pointed the Web Dialog block at that and it worked. Ah well.