How do I check if an indexedDB instance is open?

╄→尐↘猪︶ㄣ 提交于 2020-07-20 17:18:52

问题


Suppose I have an instance of an indexedDB object. Is there a simple way of detecting if the object is currently in the 'open' state?

I've tried database.closePending and looking at other properties but do not see a simple property that tells me the state of the database.

  • I am looking to do this synchronously.
  • Doing something like attempting open a transaction on a database and checking if an exception occurs is not a reasonable solution for me.
  • I don't want to maintain an extra variable associated with the database instance.

Perhaps I am missing some simple function in the api? Is there some observable feature of the instance variable that I can quickly and easily query to determine state?

Stated a different way, can you improve upon the following implementation?

function isOpen(db) {
  if(db && Object.prototype.toString.call(db) === '[object IDBDatabase]') {
    var names = db.objectStoreNames();
    if(names && names.length) {
      try {
        var transaction = db.transaction(names[0]);
        transaction.abort();
        return true;
      } catch(error) {
      }
    }
  }
}

Or this method?

var opened = false;
var db;
var request = indexedDB.open(...);
request.onsuccess = function() {
  db = request.result;
  opened = true;
};

function isOpen(db) {
  return opened;
}

db.close();
opened = false;

Or this method?

var db;
var request = indexedDB.open(...);
request.onsuccess = function() {
  db = request.result;
  db.onclose = function() {
    db._secret_did_close = true;
  };
};

function isOpen(db) {
  return db instanceof IDBDatabase && !db.hasOwnProperty('_secret_did_close');
}

回答1:


There's nothing else in the API that tells you if a connection is closed. Your enumeration of possibilities is what is available.

Also note that there is no closePending property in the API. The specification text uses a close pending flag to represent internal state, but this is not exposed to script.

Doing something like attempting open a transaction on a database and checking if an exception occurs is not a reasonable solution for me.

Why? This is the most reliable approach. Maintaining extra state would not account for unexpected closure (e.g. the user has deleted browsing data, forcing the connection to close) although that's what the onclose handler would account for - you'd need to combine your 2nd and 3rd approaches. (close is not fired if close() is called by script)




回答2:


You should create a request by using indexedDB.open and if the connection is open you will jump onsuccess method.

request = indexedDB.open('html5',1);

request.onsuccess = function() {
    console.log('Database connected');
};

Example :

https://codepen.io/headmax/pen/BmaOMR?editors=1111

About how to close or how to known if the indexedDB is still open : I guess you need to implement all events on every transaction : for example to take the control you can take the events : transaction.onerror, transaction.onabort ... If you need some example explanation i guess you have to create a new post ;).

https://developer.mozilla.org/en-US/docs/Web/API/IDBTransaction



来源:https://stackoverflow.com/questions/46996600/how-do-i-check-if-an-indexeddb-instance-is-open

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