Commit b08e246c authored by Romain Courteaud's avatar Romain Courteaud

[IndexedDB] close the DB by cancelling the promise

parent 5ce1c7d2
...@@ -97,13 +97,19 @@ ...@@ -97,13 +97,19 @@
} }
function waitForOpenIndexedDB(db_name, callback) { function waitForOpenIndexedDB(db_name, callback) {
var request;
function canceller() {
if ((request !== undefined) && (request.result !== undefined)) {
request.result.close();
}
}
function resolver(resolve, reject) { function resolver(resolve, reject) {
// Open DB // // Open DB //
var request = indexedDB.open(db_name); request = indexedDB.open(db_name);
request.onerror = function (error) { request.onerror = function (error) {
if (request.result) { canceller();
request.result.close();
}
if ((error !== undefined) && if ((error !== undefined) &&
(error.target instanceof IDBOpenDBRequest) && (error.target instanceof IDBOpenDBRequest) &&
(error.target.error instanceof DOMError)) { (error.target.error instanceof DOMError)) {
...@@ -115,17 +121,16 @@ ...@@ -115,17 +121,16 @@
}; };
request.onabort = function () { request.onabort = function () {
request.result.close(); canceller();
reject("Aborting connection to: " + db_name); reject("Aborting connection to: " + db_name);
}; };
request.ontimeout = function () { request.ontimeout = function () {
request.result.close();
reject("Connection to: " + db_name + " timeout"); reject("Connection to: " + db_name + " timeout");
}; };
request.onblocked = function () { request.onblocked = function () {
request.result.close(); canceller();
reject("Connection to: " + db_name + " was blocked"); reject("Connection to: " + db_name + " was blocked");
}; };
...@@ -133,26 +138,32 @@ ...@@ -133,26 +138,32 @@
request.onupgradeneeded = handleUpgradeNeeded; request.onupgradeneeded = handleUpgradeNeeded;
request.onversionchange = function () { request.onversionchange = function () {
request.result.close(); canceller();
reject(db_name + " was upgraded"); reject(db_name + " was upgraded");
}; };
request.onsuccess = function () { request.onsuccess = function () {
var result;
try {
result = callback(request.result);
} catch (error) {
reject(error);
}
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
return callback(request.result); return result;
}) })
.push(function (result) { .push(function (final_result) {
request.result.close(); canceller();
resolve(result); resolve(final_result);
}, function (error) { }, function (error) {
request.result.close(); canceller();
reject(error); reject(error);
}); });
}; };
} }
return new RSVP.Promise(resolver); return new RSVP.Promise(resolver, canceller);
} }
function waitForTransaction(db, stores, flag, callback) { function waitForTransaction(db, stores, flag, callback) {
...@@ -182,14 +193,8 @@ ...@@ -182,14 +193,8 @@
reject(error); reject(error);
}); });
}; };
tx.onerror = function (error) { tx.onerror = reject;
canceller(); tx.onabort = reject;
reject(error);
};
tx.onabort = function (evt) {
reject(evt.target);
};
return tx;
} }
return new RSVP.Promise(resolver, canceller); return new RSVP.Promise(resolver, canceller);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment