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

╄→гoц情女王★ 提交于 2019-12-11 04:44:13

问题


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.


回答1:


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

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(...);
  }
};


来源:https://stackoverflow.com/questions/39997018/is-there-any-way-to-detect-that-indexed-db-is-blocked-due-readwrite-or-versionch

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