In my current webapp the display can contain multiple editable objects - the data for which is either fetched from the server (and then stored for future use)or picked up from the local IndexedDB objectstore.
This I have implemented and it works perfectly. However, now I want to go a step further - fetching data that are not available locally when the user needs to work with them is liable to break the rythm of work for the user.
So I am thinking of implementing a lookahead that gets server side data prior to the user wanting to work with them. The way this would work
- When the app is launched I spawn a web worker that watches an entry, call it PreFetch, in an IndexedDB that it shares with the main app.
- The user hovers over an editable item bearing, say, the HTML id, abcd1234.
- In the app I add this id to the IndexedDB PreFetch key value - which is a comma separated list of ids.
- The web worker periodically picks up the PreFetch CSV list, then resets it, and fetches those data that are not locally available and stores them in the objectstore.
IndexedDB is nice - no doubts about that. However, it is not clear to me that what I am planning - having two threads updating the same objectstore will not create a deadlock (or worse - bring the whole house crashing down around my ears).
Given the asynchronous nature of IndexedDB operations I am concerned about two kinds of issues
a. The main thread is writing the PreFetch key whilst the worker is deleting its contents. b. The main thread attempts to fetch data from IndexedDB and decides "it is not there" while at the same time the worker has just fetched those data and is storing them.
The former is liable to defeat the purpose of doing worker driven data prefetches whilst the latter is liable to trigger unnecessary server traffic to fetch information that has already been fetched.
The former I can probably avoid by using localStorage to share the PreFetch list. The latter I cannot control.
My question then - are IndexedDB methods threadsafe? Googling for IndexedDB and thread safety has not yielded anything terribly useful other than one or two posts on this forum.
I have thought of a way to avoid this issue - the main thread and the worker both check a flag variable in localStorage prior to attempting to read/write the objectstore. However, it is not clear to me that I need to do this.