I have several FormFunctions cloud deployed and all together they are forming an application. The function parts are getting very long and have shared parts with the other forms. So the logical thing to do is to put recycled parts in a package and load the package in each web form to enable using the functions in it.
I was searching for a mechanism to prevent loading the package each time the form is populated and submitted. It seems there is no efficiency because the $SessionID changes for each refresh. A new kernel results in a need to load the package from the start. So if a package is long this delays the loading of the page. It looks like 4 $SessionIDs are recycled for all my form actions.
In a desktop session I am able to keep one $SessionID and with Needs[package`] I can prevent a second load.
What options do I have if I have 8 forms and 4 of them have 3 shared functions and the other 4 have 5 other functions to share. Do I need to create 2 package files for more efficient loading.
One other option I was pondering is to ask the 8 webforms to submit their results to one and the same API. This is possible but every API request also has its own $SessionID cycling over 4 IDs.
Ive also tested if within 1 browser session it would keep the same $SessionID and therefore it would not load the package every time. But this does not seem to be so.
The question is what the mechanism is in loading (partial) packages. Can you tell me what the big picture behind my struggles is? I seem to have a few black holes in my thinking.
Below my testing code
BeginPackage[“mypackage`”]
Unprotect[myFunc]; myFunc::usage="provides session ID" Begin["`Private`"] myFunc[x_] := $SessionIDEnd[]
Protect[myFunc];
EndPackage[]
Copy it in the cloud
CopyFile["e:\\folder\\mypackage.wl", CloudObject["mypackage.wl"]]
API definition in desktop
api = CloudDeploy[
APIFunction[{"x" -> "Number"},
CloudGet["mypackage.wl"];
mypackage`myFunc[#x] &]]
Search for matching session IDs
Table[URLExecute[api, {"x" -> 3}], {16}] // Tally
When I check the $ContextPath it seems the mypackage context is also loaded in my desktop kernel. CloudGet is defined as reading a cloud object and evaluating each expression. But this does not tell me how the API on the cloudserver loads the package. Always new or is there a Needs[] mechanism at work.