When can I tell that I have opened a connection in indexedDB?

最后都变了- 提交于 2019-12-11 07:43:44

问题


In the getCursor_ function below, please explain how I could determine if IndexedDb has opened and if not re-run function once it has? The getCursor_ runs correctly. However, since all of these calls are asynchronous, the function fails when executing before the database has completed opening.

This code is executed in a separate process:

var ixDb;
var ixDbRequest;

ixDbRequest = window.indexedDB.open(dbName, dbVersion);

ixDbRequest.onsuccess = function (e) {
            ixDb = ixDbRequest.result || e.result;
}; 

The getCursor_ function below works fine unless ixDbRequest has not completed execution. I figured out how to test for that, but I am not sure how to wait in the instance where the open database request is still running.

function getCursor_(objectStoreName) {
    if (!ixDb) {
        if (ixDbRequest) {
            // "Database is still opening.  Need code to wait or re-run 
            // once completed here.
            // I tried the following with no luck:
            ixDbRequest.addEventListener ("success",
                getCursor_(objectStoreName), false); 
            return;
        }
    }

    var transaction = ixDb.transaction(objectStoreName, 
                                       IDBTransaction.READ_ONLY);
    var objectStore = transaction.objectStore(objectStoreName);  
    try{
        var request = objectStore.openCursor(); 
        return request;
    }
    catch(e){
        console.log('IndexedDB Error' + '(' + e.code + '): ' + e.message);
    }
}

UPDATE BELOW:

The answer from @Dangerz definitely helped put me on the right track. However, since the function call is asynchronous, I also ended up having to add a callback in order to actually be able to use the cursor once the "success" event finally fired, and I was able to get the requested IndexedDb cursor. The final working function is below (re-factored slightly to remove the negative logic above "if(!ixDb)" . I am totally open to suggestions, if anyone sees room for improvement!

//****************************************************************************
//Function getCursor - Returns a cursor for the requested ObjectStore
//  objectStoreName  - Name of the Object Store / Table "MyOsName" 
//         callback  - Name of the function to call and pass the cursor 
//                     request back to upon completion.
//                     Ex. getCursor_("ObjectStore_Name", MyCallBackFunction);
//                     Function MyCallBackFunction(CursorRequestObj) { 
//                     CursorRequestObj.onsuccess = 
//                               function() {//do stuff here};
//                     }
//****************************************************************************
function getCursor_(objectStoreName, callback) {
  //The the openCursor call is asynchronous, so we must check to ensure a 
  // database connection has been established and then provide the cursor 
  // via callback. 
  if (ixDb) {
      var transaction = 
        ixDb.transaction(objectStoreName, IDBTransaction.READ_ONLY);
      var objectStore = transaction.objectStore(objectStoreName); 

      try{
        var request = objectStore.openCursor(); 
        callback(request);
        console.log('ixDbEz: Getting cursor request for ' 
                    + objectStoreName + ".");
      }
      catch(e){
        console.log('ixDbEz Error' + ' getCursor:(' 
                    + e.code + '): ' + e.message);
      } 
  }
  else { 
    if (ixDbRequest) {
      ixDbRequest.addEventListener ("success"
                    , function() { getCursor_(objectStoreName, callback); }
                    , false);
    }
  }
}

回答1:


Change your addEventListener line to:

ixDbRequest.addEventListener ("success", function() { getCursor_(objectStoreName) }, false);


来源:https://stackoverflow.com/questions/11641751/when-can-i-tell-that-i-have-opened-a-connection-in-indexeddb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!