Remove legacy iterator protocol use in Zotero.DB
This commit is contained in:
parent
9737a4d974
commit
dfe412d448
2 changed files with 12 additions and 16 deletions
|
@ -584,22 +584,18 @@ Zotero.DBConnection.prototype.queryAsync = Zotero.Promise.coroutine(function* (s
|
||||||
}
|
}
|
||||||
var failed = false;
|
var failed = false;
|
||||||
if (options && options.onRow) {
|
if (options && options.onRow) {
|
||||||
// Errors in onRow don't stop the query unless StopIteration is thrown
|
// Wrap onRow to cancel iteration by default and reject the promise
|
||||||
onRow = function (row) {
|
// on any error, which Sqlite.jsm does not do.
|
||||||
|
onRow = function (row, cancel) {
|
||||||
try {
|
try {
|
||||||
options.onRow(row);
|
options.onRow(row, () => {
|
||||||
|
Zotero.debug("Query cancelled", 3);
|
||||||
|
cancel();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
// If the onRow throws a StopIteration, stop gracefully
|
failed = e;
|
||||||
if (e instanceof StopIteration) {
|
cancel();
|
||||||
Zotero.debug("Query cancelled", 3);
|
|
||||||
}
|
|
||||||
// Otherwise, mark the promise as rejected, which Sqlite.jsm doesn't do
|
|
||||||
// on a StopIteration by default
|
|
||||||
else {
|
|
||||||
failed = e;
|
|
||||||
}
|
|
||||||
throw StopIteration;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -151,16 +151,16 @@ describe("Zotero.DB", function() {
|
||||||
assert.equal(e.message, "Failed");
|
assert.equal(e.message, "Failed");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should stop gracefully if onRow throws a StopIteration", function* () {
|
it("should stop gracefully if onRow is cancelled", function* () {
|
||||||
var i = 0;
|
var i = 0;
|
||||||
var rows = [];
|
var rows = [];
|
||||||
yield Zotero.DB.queryAsync(
|
yield Zotero.DB.queryAsync(
|
||||||
"SELECT * FROM " + tmpTable,
|
"SELECT * FROM " + tmpTable,
|
||||||
false,
|
false,
|
||||||
{
|
{
|
||||||
onRow: function (row) {
|
onRow: function (row, cancel) {
|
||||||
if (i > 0) {
|
if (i > 0) {
|
||||||
throw StopIteration;
|
return cancel();
|
||||||
}
|
}
|
||||||
rows.push(row.getResultByIndex(0));
|
rows.push(row.getResultByIndex(0));
|
||||||
i++;
|
i++;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue