Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions lib/sqlite.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ SQLitePluginTransaction.prototype.handleStatementFailure = function(handler, res
};

SQLitePluginTransaction.prototype.run = function() {
var batchExecutes, handlerFor, i, mycbmap, request, tropts, tx, txFailure, waiting;
var batchExecutes, handlerFor, i, callbacks, request, tropts, tx, txFailure, waiting;
txFailure = null;
tropts = [];
batchExecutes = this.executes;
Expand Down Expand Up @@ -588,39 +588,42 @@ SQLitePluginTransaction.prototype.run = function() {
}
};
};

i = 0;
mycbmap = {};
callbacks = [];
while (i < batchExecutes.length) {
request = batchExecutes[i];
mycbmap[i] = {
callbacks.push({
success: handlerFor(i, true),
error: handlerFor(i, false)
};
});
tropts.push({
qid: 1111,
sql: request.sql,
params: request.params
});
i++;
}

let mysuccess = function(result) {
var j, last, q, r, ref, res, type;
var j, last, q, r, res, type;
if (result.length == 0){
return;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, I don't understand the reason behind such complexity.
Though I'm not a js developer so I might be missing something.

for (i = j = 0, ref = last; 0 <= ref ? j <= ref : j >= ref; i = 0 <= ref ? ++j : --j) {

the ref could be == -1 only in case the map is empty.
I don't think we need to handle this case there.

In all other cases, the ref is >= 0.
So 0 <= ref is always true and we never will use --j and j >= ref.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the reason for this is that it is translated from a coffee script. I keep it in sync with Cordova plugin.

last = result.length - 1;
for (i = j = 0, ref = last; 0 <= ref ? j <= ref : j >= ref; i = 0 <= ref ? ++j : --j) {
r = result[i];
for (j = 0; j <= last; ++j) {
r = result[j];
type = r.type;
res = r.result;
q = mycbmap[i];
q = callbacks[j];
if (q) {
if (q[type]) {
q[type](res);
}
}
}
};

var myerror = function(error) {
console.log("batch execution error: ",error);
};
Expand All @@ -630,7 +633,7 @@ SQLitePluginTransaction.prototype.run = function() {
dbname: this.db.dbname
},
executes: tropts
},mysuccess, myerror);
}, mysuccess, myerror);
};

SQLitePluginTransaction.prototype.abort = function(txFailure) {
Expand Down