Commit 22094762 authored by preetwinder's avatar preetwinder

fix pending issues

parent 19a104f5
...@@ -81,63 +81,82 @@ ...@@ -81,63 +81,82 @@
}); });
} }
function iterateCursor(on, query, limit) { function waitForAllSynchronousCursor(request, callback) {
return new RSVP.Promise(function (resolve, reject) { var force_cancellation = false;
var result = [], count = 0, cursor;
cursor = on.openKeyCursor(query); function canceller() {
cursor.onsuccess = function (cursor) { force_cancellation = true;
if (cursor.target.result && count !== limit) { }
count += 1;
result.push({id: cursor.target.result.primaryKey, value: {}}); function resolver(resolve, reject) {
cursor.target.result.continue(); request.onerror = reject;
request.onsuccess = function (evt) {
var cursor = evt.target.result;
if (cursor && !force_cancellation) {
try {
callback(cursor);
} catch (error) {
reject(error);
}
// continue to next iteration
cursor["continue"]();
} else { } else {
resolve(result); resolve();
} }
}; };
cursor.onerror = function (error) { }
reject(error.message); return new RSVP.Promise(resolver, canceller);
}; }
});
function getCursorResult(cursor, limit) {
var result = [], count = 0;
function pushLimitedMetadata(cursor) {
if (count >= limit[0] && count < limit[1]) {
result.push({id: cursor.primaryKey, value: {}});
}
count += 1;
}
return waitForAllSynchronousCursor(cursor, pushLimitedMetadata)
.then(function () {
return result;
});
} }
function VirtualIDB(description) { function VirtualIDB(description) {
this._operations = description.operations; this._operations = description.operations;
} }
function virtualOperation(type, context, function_arguments) {
var cancel_callback;
function resolver(resolve, reject) {
cancel_callback = reject;
context._operations.push({type: type, arguments: function_arguments,
onsuccess: resolve, onerror: reject});
}
function canceller() {
cancel_callback();
}
return new RSVP.Promise(resolver, canceller);
}
VirtualIDB.prototype.hasCapacity = function (name) { VirtualIDB.prototype.hasCapacity = function (name) {
return (name === "list"); return (name === "list");
}; };
VirtualIDB.prototype.put = function (id, value) { VirtualIDB.prototype.put = function () {
var context = this; return virtualOperation("put", this, arguments);
return new RSVP.Promise(function (resolve, reject) {
context._operations.push({type: "put", arguments: [id, value],
onsuccess: resolve, onerror: reject});
});
}; };
VirtualIDB.prototype.remove = function (id) { VirtualIDB.prototype.remove = function () {
var context = this; return virtualOperation("remove", this, arguments);
return new RSVP.Promise(function (resolve, reject) {
context._operations.push({type: "remove", arguments: [id],
onsuccess: resolve, onerror: reject});
});
}; };
VirtualIDB.prototype.get = function (id) { VirtualIDB.prototype.get = function () {
var context = this; return virtualOperation("get", this, arguments);
return new RSVP.Promise(function (resolve, reject) {
context._operations.push({type: "get", arguments: [id],
onsuccess: resolve, onerror: reject});
});
}; };
VirtualIDB.prototype.buildQuery = function (options) { VirtualIDB.prototype.buildQuery = function () {
var context = this; return virtualOperation("buildQuery", this, arguments);
return new RSVP.Promise(function (resolve, reject) {
context._operations.push({type: "buildQuery", arguments: [options],
onsuccess: resolve, onerror: reject});
});
}; };
VirtualIDB.prototype.allAttachments = function () { VirtualIDB.prototype.allAttachments = function () {
...@@ -204,9 +223,9 @@ ...@@ -204,9 +223,9 @@
} }
} }
if (operation.type === "buildQuery") { if (operation.type === "buildQuery") {
request = iterateCursor(store); request = store.getAllKeys();
request.then(operation.onsuccess).fail(operation.onerror); request.oerror = operation.onerror;
return; return {request: request, onsuccess: operation.onsuccess};
} }
if (operation.type === "remove") { if (operation.type === "remove") {
request = store.delete(operation.arguments[0]); request = store.delete(operation.arguments[0]);
...@@ -237,10 +256,7 @@ ...@@ -237,10 +256,7 @@
if (continuation_resolve) { if (continuation_resolve) {
continuation_resolve.apply(null, arguments); continuation_resolve.apply(null, arguments);
} }
while (true) { while (operations.length !== 0) {
if (operations.length === 0) {
break;
}
operation_result = processVirtualOperation(operations.shift(), store, operation_result = processVirtualOperation(operations.shift(), store,
index_keys, clear_storage); index_keys, clear_storage);
// use the current request to continue the repeat loop if possible // use the current request to continue the repeat loop if possible
...@@ -413,14 +429,30 @@ ...@@ -413,14 +429,30 @@
IndexStorage2.prototype._runQuery = function (key, value, limit) { IndexStorage2.prototype._runQuery = function (key, value, limit) {
var context = this; var context = this;
return waitForOpenIndexedDB(context._database_name, context._version,
context._index_keys, context._sub_storage_description, return RSVP.Queue()
context._signature_storage_name, function (db) { .push(function () {
return waitForTransaction(db, ["index-store"], "readonly", return waitForOpenIndexedDB(context._database_name, context._version,
function (tx) { context._index_keys, context._sub_storage_description,
return iterateCursor(tx.objectStore("index-store") context._signature_storage_name, function (db) {
.index("Index-" + key), value, limit); return waitForTransaction(db, ["index-store"], "readonly",
function (tx) {
if (limit) {
return getCursorResult(tx.objectStore("index-store")
.index("Index-" + key).openCursor(value), limit);
}
return waitForIDBRequest(tx.objectStore("index-store")
.index("Index-" + key).getAllKeys(value));
});
}); });
})
.push(function (result) {
if (limit) {
return result;
}
return result.target.result.map(function (item) {
return {id: item, value: {}};
});
}); });
}; };
...@@ -433,12 +465,7 @@ ...@@ -433,12 +465,7 @@
if (context._index_keys.indexOf(query.key) !== -1) { if (context._index_keys.indexOf(query.key) !== -1) {
return context._runQuery(query.key, query.value, options.limit) return context._runQuery(query.key, query.value, options.limit)
.then(function (result) { .then(function (result) {
return result.map(function (value) { return result;
return {
id: value.id,
value: {}
};
});
}); });
} }
} }
...@@ -483,7 +510,10 @@ ...@@ -483,7 +510,10 @@
var context = this; var context = this;
return context._sub_storage.post(value) return context._sub_storage.post(value)
.push(function (id) { .push(function (id) {
return context._put(id, value); return context._put(id, value)
.then(function () {
return id;
});
}); });
}; };
......
...@@ -653,10 +653,10 @@ ...@@ -653,10 +653,10 @@
context.jio.put("11", {"a": "16", "b": "2"}) context.jio.put("11", {"a": "16", "b": "2"})
]) ])
.then(function () { .then(function () {
return context.jio.allDocs({limit: 4, query: "b:2"}); return context.jio.allDocs({limit: [1, 3], query: "b:2"});
}) })
.then(function (result) { .then(function (result) {
equal(result.data.total_rows, 4); equal(result.data.total_rows, 2);
}) })
.fail(function (error) { .fail(function (error) {
console.log(error); console.log(error);
...@@ -790,7 +790,13 @@ ...@@ -790,7 +790,13 @@
}); });
test("Repair fails", function () { test("Repair fails", function () {
var context = this; var context = this, chrome_error, firefox_error;
chrome_error = "Connection to: jio:index2_test failed: Version change " +
"transaction was aborted in upgradeneeded event handler. " +
"Error: Capacity 'buildQuery' is not implemented on 'dummystorage3'";
firefox_error = "Connection to: jio:index2_test failed: A request was " +
"aborted, for example through a call to IDBTransaction.abort. " +
"Error: Capacity 'buildQuery' is not implemented on 'dummystorage3'";
context.jio = jIO.createJIO({ context.jio = jIO.createJIO({
type: "index2", type: "index2",
database: "index2_test", database: "index2_test",
...@@ -806,9 +812,7 @@ ...@@ -806,9 +812,7 @@
context.jio.allDocs({query: "c: 'control'"}) context.jio.allDocs({query: "c: 'control'"})
.fail(function (error) { .fail(function (error) {
equal(error, "Connection to: jio:index2_test failed: Version change " + ok(error === chrome_error || error === firefox_error);
"transaction was aborted in upgradeneeded event handler. " +
"Error: Capacity 'buildQuery' is not implemented on 'dummystorage3'");
}) })
.always(function () { .always(function () {
start(); start();
......
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