Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access indexedDB synchronously?

The indexedDB has a spec saying that you can access an indexed database synchronously, but it hasn't been implemented yet.

I was just wondering if there is a way to make it synchronous manually,

My JavaScript looks like this,

var trans = databaseAsync.transaction(["mapTile"], IDBTransaction.READ_WRITE);
var store = trans.objectStore("mapTile");
var keyRange = IDBKeyRange.bound(evt.data[0], evt.data[0]);
var cursorRequest = store.openCursor(keyRange);

// can you put some kind of wait in here?

cursorRequest.onsuccess = function(e)
{
    var result = e.target.result;
    if(!!result == false)
    {
    }
}

So can you put something in there to make it wait until the onsuccess method has been called before continuing on?

The reason why I want to do this is the code above is wrapped inside this method,

dojo.extend(esri.layers.ArcGISTiledMapServiceLayer, {
      getTileUrl : function(level, row, col)
      {
          // blah
          return url;
      }

So it is an ESRI tile layer (which will load tiles onto a map on my web page), and that method needs to return the url straight away for a particular tile. It will either be a URL to load the image from if it isn't cached in the database already, or this,

data:image;base64,*BASE64DATA*

Where BASE64DATA is the data from the database if previously cached.

I was previously using the localStorage for this, which works synchronously, but that has a 5MB limit so I thought I would experiment with indexedDB.

like image 928
peter Avatar asked Feb 09 '12 22:02

peter


People also ask

Is IndexedDB synchronous?

Synchronous and asynchronousOperations performed using IndexedDB are done asynchronously, so as not to block applications.

How many API modes IndexedDB have?

Before creating the database I want to tell you about the two API modes of IndexedDB. Synchronous mode: This mode was created to be used only in conjunction with web workers. But most browsers currently do not support the synchronous mode.

Can service workers access IndexedDB?

Note: IndexedDB can be used inside a service worker for data storage if you require it.


1 Answers

IndexedDB Sync API is marked as a risky part of the IndexedDB specification and they might be removed due to potential lack of implementations.

I've implemented 'sync' solution using the 'oncomplete' transaction event which guaranties that the current action is finished before starting the next one, and I also use custom semaphore and queue logic which handles the async calls from GUI and ensures that 2 open connections towards IndexedDB database won't happen at the same time.

like image 165
Deni Spasovski Avatar answered Oct 04 '22 20:10

Deni Spasovski