Re: InedxedDB events : misconception?

On Mon, Apr 22, 2013 at 9:43 AM, Michaël Rouges
<michael.rouges@gmail.com> wrote:
> Hello everyone,
>
> I'm surprised by how events are added to IndexedDB objects.
>
> For example, base, opening a database:
>
> var request;
> request = indexedDB.open('database');
> request.onsuccess = function () {};
> request.onupgradeneeded = function () {};
> request.onerror = function () {};
>
> As you can see, the events are added after the query execution.
>
> It does not seem natural and can cause stability problems.
>
> Indeed, if the application had to be completed by the addition of events,
> they will never be triggered.

Events don't fire until the next tick.  As long as you don't spin the
event loop, there's no race conditions and everything's fine.

> Here is an example generates an error:
>
> var request;
> request = indexedDB.open('database');
> setTimeout(function () {
>     request.onsuccess = function () {};
>     request.onupgradeneeded = function () {};
>     request.onerror = function () {};
> }, 1500);

This... is precisely the sort of thing that you shouldn't do.  Just
don't do this, and you're golden.

> In my view, any item requiring events should be set first, then we add the
> events and finally, we execute it.
>
> The code should look like this:
>
> var request;
> request = indexedDB.open('database');
> request.onsuccess = function () {};
> request.onupgradeneeded = function () {};
> request.onerror = function () {};
> request.execute();

The "request.execute()" is more or less implied by hitting the bottom
of your function, when it returns control back to the browsers.

~TJ

Received on Monday, 22 April 2013 16:47:47 UTC