W3C home > Mailing lists > Public > public-webapps@w3.org > January to March 2010

Re: [IndexedDB] API feedback

From: Aaron Boodman <aa@google.com>
Date: Fri, 12 Mar 2010 09:05:07 -0800
Message-ID: <278fd46c1003120905s5b31f5f9o72c140b79dfa4ebe@mail.gmail.com>
To: Jeremy Orlow <jorlow@chromium.org>
Cc: Kris Zyp <kris@sitepen.com>, Shawn Wilsher <sdwilsh@mozilla.com>, Nikunj Mehta <nikunj@o-micron.com>, public-webapps WG <public-webapps@w3.org>, Arun Ranganathan <aranganathan@mozilla.com>, Jonas Sicking <sicking@mozilla.com>
I haven't been following the discussion with IndexedDB closely, but I
think the general ID of an indexed name/value store is a great idea,
and would be Good for the web.

However, I do agree with Jeremy that the current shape of the API is
too confusing. Maybe I can be useful as a new set of eyes.

Looking at just this snip:

function findFred(db) {
  db.request.onsuccess = function() {
    var index = db.request.result;
    index.request.onsuccess = function() {
      var matching = index.request.result;
      if (matching)
        report(matching.isbn, matching.name, matching.author);
      else
        report(null);
    }
    index.get('fred');
  }
  db.openIndex('BookAuthor');
}

This example is hard to read where the callback is setup before the
call like this. Without making any API changes, I think you could
improve things:

db.openIndex('BookAuthor');
db.request.onsuccess = function() {
  ...
};

You just have to make sure that the API guarantees that
onsuccess/onfailure will always be called in a separate event, which
is important anyway for consistency.

I think it's counter-intuitive to have a single 'request' object per
database. Even though it might be technically true that in
single-threaded JavaScript that is the reality. A request describes
something that there are many of, that doesn't get reused. Reusing the
single onsuccess event also seems like something that is likely to
cause bugs (people forgetting to reset it correctly, an old callback
gets fired). Finally, developers might be used to XHR, where there are
of course multiple requests.

So I think one improvement could be create one request for each logical request:

function findFred(db) {
  var req1 = db.openIndex('BookAuthor');
  req.onsuccess = function() {
    var index = req1.result;
    var req2 = index.get('fred');
    req2.onsuccess = function() {
      ...
    }
  }
}

Like I said, I've only thought about this for a moment. I know how
hard API design can be, and these are my drive-by first-blush
comments. But on the other hand, sometimes drive-by, first-blush
comments are useful for figuring out APIs.

HTH,

- a
Received on Friday, 12 March 2010 17:05:38 GMT

This archive was generated by hypermail 2.3.1 : Tuesday, 26 March 2013 18:49:37 GMT