I have a situation where I normally pass in a dict, but in a certain context I want to pass in a custom dict that will load values via server calls. I’ve written the custom dict, but am getting an odd sort of interruption of the code. Skulpt is clearly not tolerating the delay of the server call.
What Should Happen
Code like this accesses an element of the custom dict:
self.label_2.text = Cache.media['foo']
The custom dict itself will, if it does not have an entry for the key ‘foo’, make a server call to get the entry. After that it caches it so it gets returned without the server call.
I have some debug output in place, and the output I expect to see is:
Calling Fetch
Fetching media for foo -- this is just before the server call in the custom dict
get_media called for foo -- this is printed in the server call
get_media fetched URL -- this is also printed in the server call
Got foo -- this is after the server call returns in the custom dict
Done calling Fetch
What Actually Happens
Calling Fetch
Fetching media for foo
KeyError: foo
at Cache, line 41 -- this line of code is after the server call should have returned
called from Form1, line 16
get_media called for foo -- yet the server call seems to happen after that
get_media fetched URL
There seems to be some level of asynchronous behavior going on in this particular situation. This goes way beyond my knowledge of how Skulpt and Javascript work to identify why it’s happening or how to fix it.
Here’s a clone that provides a minimum repeatable example:Anvil | Login
In Form1’s init method, you can test the functionality of the custom dict without the server call by commenting out line 17. That works fine. With line 17 uncommented, the server call is made and the behavior is seen.
If this is just the way Skulpt works, I can use a different mechanism than the custom dictionary for this special context. I’m hoping, though, that there’s an easy fix for this.