Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing IndexedDB from multiple javascript threads
    text
    copied!<p><strong>Overview:</strong> I am trying to avoid a race condition with accessing an IndexedDB from both a webpage and a web-worker. </p> <p><strong>Setup:</strong> Webpage that is saving items to the local IndexedDB as the user works with the site. Whenever a user saves data to the local DB the record is marked as "Unsent".</p> <p>Web-worker background thread that is pulling data from the IndexedDB, sending it to the server and once the server receives it, marking the data in the IndexedDB as "Sent".</p> <p><strong>Problem:</strong> Since access to the IndexedDB is asynchronous, I can not be guaranteed that the user won't update a record at the same time the web-worker is sending it to the server. The timeline is shown below:</p> <ol> <li>Web-worker gets data from DB and sends it to the server</li> <li>While the transfer is happening, the user updates the data saving it to the DB.</li> <li>The web-worker gets the response from the server and then updates the DB to "Sent"</li> <li>There is now data in DB that hasn't been sent to the server but marked as "Sent"</li> </ol> <p><strong>Failed Solution:</strong> After getting the response from the server, I can recheck to row to see if anything has been changed. However I am still left with a small window where data can be written to the DB and it will never be sent to the server.</p> <p>Example: After server says data is saved, then:</p> <pre><code>IndexedDB.HasDataChanged( function(changed) { // Since this is async, this changed boolean could be lying. // The data might have been updated after I checked and before I was called. if (!changed){ IndexedDB.UpdateToSent() } }); </code></pre> <p><strong>Other notes:</strong> There is a sync api according to the W3 spec, but no one has implemented it yet so it can not be used (http://www.w3.org/TR/IndexedDB/#sync-database). The sync api was designed to be used by web-workers, to avoid this exact situation I would assume.</p> <p>Any thoughts on this would be greatly appreciated. Have been working on it for about a week and haven't been able to come up with anything that will work.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload