有什么方法可以检测到索引数据库在多个选项卡中由于读写或版本更改而被阻止 [英] Is there any way to detect that indexed db is blocked due readwrite or versionchange in multiple tab

查看:83
本文介绍了有什么方法可以检测到索引数据库在多个选项卡中由于读写或版本更改而被阻止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何方法可以检测到由于多个选项卡中的读写锁定或版本更改锁而导致索引数据库被阻止.如何检测到该锁已释放,然后继续进行读写或版本更改操作.

Is there any way to detect that indexed db is blocked due readwrite or versionchange locks in multiple tab.how to detect the lock is released and then continue with readwrite or versionchange operations.

推荐答案

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

To detect that an indexedDB database is blocked in another tab, you can listen for the blocked event when connecting to the database.

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

正如Bell先生在评论中指出的那样,阻止事件并不意味着成功事件就永远不会触发,而只是意味着(无限期地)阻止连接过程时暂停"了连接过程.只需侦听成功事件即可表明连接过程不再受阻.

As Mr. Bell states in the comments, a block event does not mean a success event will never fire, it just means the connection process is 'paused' while blocked (indefinitely). Simply listening for the success event indicates the connection process is no longer blocked.

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

这篇关于有什么方法可以检测到索引数据库在多个选项卡中由于读写或版本更改而被阻止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆