Commit b08e246c authored by Romain Courteaud's avatar Romain Courteaud

[IndexedDB] close the DB by cancelling the promise

parent 5ce1c7d2
......@@ -97,13 +97,19 @@
}
function waitForOpenIndexedDB(db_name, callback) {
var request;
function canceller() {
if ((request !== undefined) && (request.result !== undefined)) {
request.result.close();
}
}
function resolver(resolve, reject) {
// Open DB //
var request = indexedDB.open(db_name);
request = indexedDB.open(db_name);
request.onerror = function (error) {
if (request.result) {
request.result.close();
}
canceller();
if ((error !== undefined) &&
(error.target instanceof IDBOpenDBRequest) &&
(error.target.error instanceof DOMError)) {
......@@ -115,17 +121,16 @@
};
request.onabort = function () {
request.result.close();
canceller();
reject("Aborting connection to: " + db_name);
};
request.ontimeout = function () {
request.result.close();
reject("Connection to: " + db_name + " timeout");
};
request.onblocked = function () {
request.result.close();
canceller();
reject("Connection to: " + db_name + " was blocked");
};
......@@ -133,26 +138,32 @@
request.onupgradeneeded = handleUpgradeNeeded;
request.onversionchange = function () {
request.result.close();
canceller();
reject(db_name + " was upgraded");
};
request.onsuccess = function () {
var result;
try {
result = callback(request.result);
} catch (error) {
reject(error);
}
return new RSVP.Queue()
.push(function () {
return callback(request.result);
return result;
})
.push(function (result) {
request.result.close();
resolve(result);
.push(function (final_result) {
canceller();
resolve(final_result);
}, function (error) {
request.result.close();
canceller();
reject(error);
});
};
}
return new RSVP.Promise(resolver);
return new RSVP.Promise(resolver, canceller);
}
function waitForTransaction(db, stores, flag, callback) {
......@@ -182,14 +193,8 @@
reject(error);
});
};
tx.onerror = function (error) {
canceller();
reject(error);
};
tx.onabort = function (evt) {
reject(evt.target);
};
return tx;
tx.onerror = reject;
tx.onabort = reject;
}
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