我正在尝试针对IndexedDB
库编写一些测试。
在这样做的过程中,我故意通过调用blocked
来创建open()
事件,该版本针对已经打开的数据库具有更高版本。
虽然我可以通过在第一个versionchange
期间在早期版本上设置的open()
侦听器中关闭数据库来绕过阻塞,但出于测试目的,我希望避免添加此{{1} }}侦听器,而是让versionchange
事件完全触发,这样我就可以确认我的blocked
侦听器是否正常工作。
问题是,在此阶段进行调用以关闭数据库似乎无助于撤消阻止,随后调用删除同一数据库也会收到onblocked
事件(在Firefox和Chrome中)。因此,在阻止代码执行后,我无法继续进行进一步的测试。
如果不使用之前添加的blocked
侦听器,是否无法从blocked
事件中恢复?
以下是代码:
versionchange
1 个答案:
答案 0 :(得分:4)
You're opening two connections but only closing one.
In fireBlockedEvent you're closing the connection opened via open(DB_NAME, DB_VERSION) (passed in as db). But you're never closing the connection opened in fireBlockedEvent itself via open(DB_NAME, NEW_DB_VERSION) before calling deleteDatabase().
The delete request will be blocked until all connections are closed.
Since you're writing test code it's hard to suggest a specific fix without altering the semantics, but this would work:
function fireBlockedEvent (db) {
var req = indexedDB.open(DB_NAME, NEW_DB_VERSION);
req.onblocked = function () {
console.log('blocked');
// close the blocking connection:
db.close();
};
req.onsuccess = function (e) {
// close the formerly blocked connection:
e.target.result.close();
var req = indexedDB.deleteDatabase(DB_NAME);
req.onsuccess = function () {
console.log('we ok');
};
req.onblocked = function () {
console.log('still blocked');
};
};
}