admin管理员组

文章数量:1356288

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');
}

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');
}
Share Improve this question edited Oct 29, 2017 at 4:02 Josh asked Oct 29, 2017 at 3:32 JoshJosh 18.7k7 gold badges54 silver badges72 bronze badges 1
  • sorry for the time but i see the code and i prefer the middle because you don't need to check all, just using events and callbacks can got the errors. you can show it on the browser console and the way is to make some errors by test what is going on. The good way i guess is to implements all methods during the transaction where are major of errors stored. The connection can still running and you can catch this event by using blocked event ... that the reason why just implement and add a console.log there to be sure. – user8556290 Commented Oct 29, 2017 at 4:39
Add a ment  | 

3 Answers 3

Reset to default 3

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 bine your 2nd and 3rd approaches. (close is not fired if close() is called by script)

Better late than never, but here you go:

function isOpen(db) {
    let open = true;
    try {
        db.transaction('');
    } catch(err) {
        if (err.name === "InvalidStateError") {
            open = false;
        }
    }
    return open;
}

Example:

indexedDB.open('test', 1).onsuccess = ({target: { result: db }}) => {
    console.log(isOpen(db)); // true
    db.close();
    console.log(isOpen(db)); // false
}

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/en-US/docs/Web/API/IDBTransaction

本文标签: javascriptHow do I check if an indexedDB instance is openStack Overflow