onsuccess callback in race condition?

Hi,

I would like to get some clarification on async implementation of the
indexedDB API.

Example 1:
Below is a sample code from the following blog article.
http://hacks.mozilla.org/2010/06/comparing-indexeddb-and-webdatabase/

var request = window.indexedDB.open("CandyDB",
                                    "My candy store database");
request.onsuccess = function(event) {
  var db = event.result;
  if (db.version != "1") {
    // User's first visit, initialize database.
    var createdObjectStoreCount = 0;
    var objectStores = [
      { name: "kids", keyPath: "id", autoIncrement: true },
      { name: "candy", keyPath: "id", autoIncrement: true },
      { name: "candySales", keyPath: "", autoIncrement: true }
    ];
 
    function objectStoreCreated(event) {
      if (++createdObjectStoreCount == objectStores.length) {
        db.setVersion("1").onsuccess = function(event) {
          loadData(db);
        };
      }
    }
 
    for (var index = 0; index < objectStores.length; index++) {
      var params = objectStores[index];
      request = db.createObjectStore(params.name, params.keyPath,
                                     params.autoIncrement);
      request.onsuccess = objectStoreCreated;
    }
  }
  else {
    // User has been here before, no initialization required.
    loadData(db);
  }
};

In this code, request is returned from request = window.indexedDB.open()
call, then request.onsuccess = function() {...} is assigned.

Maybe I don't fully understand how this IndexedDB works,
it seems that there could be a race condition between the open() operation
and request.onsuccess assignment. The open() operation could be completed
before request.onsuccess got assigned to a callback function. Does this mean
that the request.onsuccess callback function will not be called?

Is there a mechanism to assure that the callback gets called regardless
whether the open() operation completed or not when request.onsuccess is
assigned to a callback function?

Of course, the same issue exists for all other async calls.

Whether this can be assured or not, maybe the spec document can point it out
if not already in the coming draft.

Example 2:
On the other hand, in the July 15, 2010 editor's draft, 3.2.2 The IDBRequest
Interface section, there is an example:

indexedDB.request.onsuccess = function(evt) {...};
indexedDB.request.onerror = function(evt) {...};
indexedDB.open('AddressBook', 'Address Book');

This indicates that request object is a member of indexedDB interface, and
request.onsuccess can be assigned to a callback before indexDB.open() is
called, therefore is no race condition issue here.

But IndexDB is a 
interface IDBFactory {
    readonly attribute DOMStringList databases;
    IDBRequest open (in DOMString name, in DOMString description) raises
(IDBDatabaseException);
};

It does not appear to have a request object as member.


Can someone clarify these above 2 examples for me?

Does request.onsuccess need to be assigned to a callback before
indexDB.open() get called to avoid race condition? Like in example 2)

Maybe both examples are correct. Then what makes sure that in example 1) the
callback function get called even if open() has been completed before the
callback function is assigned to request.onsuccess?

Thank you.
Victor

Received on Thursday, 15 July 2010 16:03:30 UTC