How can I access indexedDB synchronously?

Indexed DB has a specification saying that you can access the indexed database synchronously, but it is not yet implemented.

I'm just wondering if there is a way to do this synchronously 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 add something there to make it wait until the onsuccess method is called?

The reason I want to do this is because the code above in this method is

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

So, this is an ESRI tile layer (which uploads tiles to a map on my web page), and this method should immediately return the URL for a specific tile. This will either be the URL to download the image if it is not already cached in the database, or this,

 data:image;base64,*BASE64DATA* 

Where BASE64DATA is the data from the database, if it was previously cached.

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

+6
source share
4 answers

There is a more general question: how to wait for asynchronous functions, where you will find the answer: How to block asynchronous functions in JavaScript

I also found a nice waitFor procedure inside the trial tool .

+2
source

The IndexedDB Sync API is flagged as a dangerous part of the IndexedDB specification , and they may be removed due to a potential lack of implementations.

I implemented "synchronization" using the "oncomplete" transaction event, which guarantees that the current action will be completed before the next one, and I also use custom semaphore and queue , which processes asynchronous calls from the GUI and ensures that there are 2 open connections to the database IndexedDB data won at the same time happening.

+2
source

An elegant way to do this is presented in the accepted answer to this question, which I asked a couple of months ago . Unfortunately, it relies on a JavaScript 1.7 function (generators), which is only supported in Firefox. Hopefully someday this technique will be more widely supported.

+2
source

IE10 supports sync api, but indexeddb sync api can only be used inside a web worker. If you are running in a user interface thread, you need to use the asynchronous API.

The easiest way to work with the asynchronous API is through promises.

0
source

Source: https://habr.com/ru/post/908099/


All Articles