有什么方法可以检测到索引数据库由于多个选项卡中的读写或版本更改而被阻止

Is there any way to detect that indexed db is blocked due readwrite or versionchange in multiple tab

是否有任何方法可以检测到索引数据库由于多个读写或版本更改锁而被阻塞tab.how以检测锁已释放,然后继续进行读写或版本更改操作。

要检测另一个选项卡中的indexedDB数据库是否被阻止,您可以在连接数据库时监听被阻止的事件。

const request = indexedDB.open(...);
request.onblocked = function(event) {
  console.log('blocked :(');
};

正如 Bell 先生在评论中所说,阻止事件并不意味着成功事件永远不会触发,它只是意味着连接过程 'paused' 被阻止(无限期)。简单监听成功事件表明连接进程不再阻塞。

const wasBlocked = false;
const request = indexedDB.open(...);
request.onblocked = function(event) {
  wasBlocked = true;
};
request.onsuccess = function(event) {
  if(wasBlocked) {
    fireUnblockedEvent(...);
  }
};