Commit 80583aef authored by Xiaowu Zhang's avatar Xiaowu Zhang

add transaction check after request

parent 67911e51
...@@ -140,7 +140,9 @@ ...@@ -140,7 +140,9 @@
try { try {
request = store.put(metadata); request = store.put(metadata);
resolver = function (resolve, reject) { resolver = function (resolve, reject) {
request.onerror = reject; request.onerror = function (e) {
reject(e);
};
request.onsuccess = function () { request.onsuccess = function () {
resolve(metadata); resolve(metadata);
}; };
...@@ -153,9 +155,16 @@ ...@@ -153,9 +155,16 @@
} }
} }
function transactionEnd(transaction) {
var resolver;
resolver = function (resolve, reject) {
transaction.onabort = reject;
transaction.oncomplete = function () {
resolve("end");
};
};
return new RSVP.Promise(resolver);
}
/** /**
* get a data from a store object * get a data from a store object
* @param {ObjectStore} store The objectstore * @param {ObjectStore} store The objectstore
...@@ -183,8 +192,12 @@ ...@@ -183,8 +192,12 @@
function removeIndexedDB(store, id) { function removeIndexedDB(store, id) {
function resolver(resolve, reject) { function resolver(resolve, reject) {
var request = store["delete"](id); var request = store["delete"](id);
request.onerror = reject; request.onerror = function (e) {
request.onsuccess = resolve; reject(e);
};
request.onsuccess = function () {
resolve(request.result);
};
} }
return new RSVP.Promise(resolver); return new RSVP.Promise(resolver);
} }
...@@ -228,7 +241,7 @@ ...@@ -228,7 +241,7 @@
global_db, global_db,
result; result;
new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
//open a database //open a database
return open(jio_storage._database_name); return open(jio_storage._database_name);
...@@ -251,19 +264,16 @@ ...@@ -251,19 +264,16 @@
.push(function (researchResult) { .push(function (researchResult) {
//create an id in attachment si necessary //create an id in attachment si necessary
if (researchResult.result === undefined) { if (researchResult.result === undefined) {
putIndexedDB(researchResult.store, {"_id": metadata._id}); return putIndexedDB(researchResult.store, {"_id": metadata._id});
} else {
return end(result);
} }
}) })
.push(function () {
return transactionEnd(transaction);
})
.push(function () { .push(function () {
return end(result); return end(result);
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
// Check if transaction is ongoing, if so, abort it
if (transaction !== undefined) {
transaction.abort();
}
if (global_db !== undefined) { if (global_db !== undefined) {
global_db.close(); global_db.close();
} }
...@@ -286,7 +296,7 @@ ...@@ -286,7 +296,7 @@
transaction, transaction,
global_db, global_db,
meta; meta;
new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
return openIndexedDB(jio_storage._database_name); return openIndexedDB(jio_storage._database_name);
}) })
...@@ -311,13 +321,12 @@ ...@@ -311,13 +321,12 @@
if (result._attachment) { if (result._attachment) {
meta._attachment = result._attachment; meta._attachment = result._attachment;
} }
return transactionEnd(transaction);
})
.push(function () {
return ({"data": meta}); return ({"data": meta});
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
// Check if transaction is ongoing, if so, abort it
if (transaction !== undefined) {
transaction.abort();
}
if (global_db !== undefined) { if (global_db !== undefined) {
global_db.close(); global_db.close();
} }
...@@ -338,7 +347,15 @@ ...@@ -338,7 +347,15 @@
transaction, transaction,
global_db, global_db,
queue = new RSVP.Queue(); queue = new RSVP.Queue();
queue.push(function () { function tmp(index, array, store) {
return removeIndexedDB(store, [param._id, array[index]])
.then(function () {
if (index < array.length - 1) {
return tmp(index + 1, array, store);
}
});
}
return queue.push(function () {
return openIndexedDB(jio_storage._database_name); return openIndexedDB(jio_storage._database_name);
}) })
.push(function (db) { .push(function (db) {
...@@ -359,32 +376,26 @@ ...@@ -359,32 +376,26 @@
var store = transaction.objectStore("attachment"); var store = transaction.objectStore("attachment");
return getIndexedDB(store, param._id); return getIndexedDB(store, param._id);
}) })
.push(function (result) { .push(function (result) {
if (result._attachment) { if (result._attachment) {
var i, l, key, array, func, store; var array, store;
array = Object.keys(result._attachment); array = Object.keys(result._attachment);
store = transaction.objectStore("blob"); store = transaction.objectStore("blob");
for (i = 0, l = array.length; i < l; i += 1) { return tmp(0, array, store);
key = array[i];
//delete blob
func = removeIndexedDB(store, [param._id, key]);
queue.push(func);
}
} }
}) })
.push(function () { .push(function () {
var store = transaction.objectStore("attachment"); var store = transaction.objectStore("attachment");
//delete attachment //delete attachment
return removeIndexedDB(store, param._id); return removeIndexedDB(store, param._id);
}) })
.push(function () { .push(function () {
return transactionEnd(transaction);
})
.push(function () {
return ({"status": 204}); return ({"status": 204});
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
// Check if transaction is ongoing, if so, abort it
if (transaction !== undefined) {
transaction.abort();
}
if (global_db !== undefined) { if (global_db !== undefined) {
global_db.close(); global_db.close();
} }
...@@ -417,7 +428,7 @@ ...@@ -417,7 +428,7 @@
return ({"id": metadata._id}); return ({"id": metadata._id});
} }
that._putOrPost(openIndexedDB, promiseResearch, return that._putOrPost(openIndexedDB, promiseResearch,
promiseOngoingPost, promiseEndPost, promiseOngoingPost, promiseEndPost,
command, metadata); command, metadata);
...@@ -447,7 +458,7 @@ ...@@ -447,7 +458,7 @@
function promiseEndPut() { function promiseEndPut() {
return {"status": (found ? 204 : 201) }; return {"status": (found ? 204 : 201) };
} }
that._putOrPost(openIndexedDB, promiseResearch, return that._putOrPost(openIndexedDB, promiseResearch,
promiseOngoingPut, promiseEndPut, promiseOngoingPut, promiseEndPut,
command, metadata); command, metadata);
...@@ -596,14 +607,14 @@ ...@@ -596,14 +607,14 @@
global_db, global_db,
BlobInfo, BlobInfo,
readResult; readResult;
jIO.util.readBlobAsArrayBuffer(metadata._blob) return jIO.util.readBlobAsArrayBuffer(metadata._blob)
.then(function (event) { .then(function (event) {
readResult = event.target.result; readResult = event.target.result;
BlobInfo = { BlobInfo = {
"content_type": metadata._blob.type, "content_type": metadata._blob.type,
"length": metadata._blob.size "length": metadata._blob.size
}; };
new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
return openIndexedDB(jio_storage._database_name); return openIndexedDB(jio_storage._database_name);
}) })
...@@ -632,14 +643,14 @@ ...@@ -632,14 +643,14 @@
return putIndexedDB(store, {"_id": metadata._id, return putIndexedDB(store, {"_id": metadata._id,
"_attachment" : metadata._attachment, "_attachment" : metadata._attachment,
"blob": metadata._blob}, readResult); "blob": metadata._blob}, readResult);
}).push(function () { })
.push(function () {
return transactionEnd(transaction);
})
.push(function () {
return {"status": 204}; return {"status": 204};
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
// Check if transaction is ongoing, if so, abort it
if (transaction !== undefined) {
transaction.abort();
}
if (global_db !== undefined) { if (global_db !== undefined) {
global_db.close(); global_db.close();
} }
...@@ -662,7 +673,7 @@ ...@@ -662,7 +673,7 @@
transaction, transaction,
global_db, global_db,
_id_attachment = [param._id, param._attachment]; _id_attachment = [param._id, param._attachment];
new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
return openIndexedDB(jio_storage._database_name); return openIndexedDB(jio_storage._database_name);
}) })
...@@ -714,7 +725,7 @@ ...@@ -714,7 +725,7 @@
transaction, transaction,
global_db, global_db,
_id_attachment = [param._id, param._attachment]; _id_attachment = [param._id, param._attachment];
new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
return openIndexedDB(jio_storage._database_name); return openIndexedDB(jio_storage._database_name);
}) })
...@@ -742,15 +753,14 @@ ...@@ -742,15 +753,14 @@
delete result._attachment[param._attachment]; delete result._attachment[param._attachment];
var store = transaction.objectStore("attachment"); var store = transaction.objectStore("attachment");
return putIndexedDB(store, result); return putIndexedDB(store, result);
})
.push(function () {
return transactionEnd(transaction);
}) })
.push(function () { .push(function () {
return ({ "status": 204 }); return ({ "status": 204 });
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
// Check if transaction is ongoing, if so, abort it
if (transaction !== undefined) {
transaction.abort();
}
if (global_db !== undefined) { if (global_db !== undefined) {
global_db.close(); global_db.close();
} }
......
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