问题
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