Cleanups in CoffeeScript & JS callbacks

This commit is contained in:
Chris Brody 2012-12-05 22:05:48 +01:00
parent bdf5ba7cb0
commit ae86164ec6
3 changed files with 67 additions and 60 deletions

View File

@ -3,41 +3,50 @@ do ->
SQLitePlugin = (dbPath, openSuccess, openError) -> SQLitePlugin = (dbPath, openSuccess, openError) ->
console.log "SQLitePlugin" console.log "SQLitePlugin"
throw new Error("Cannot create a SQLitePlugin instance without a dbPath") unless dbPath
@dbPath = dbPath @dbPath = dbPath
@openSuccess = openSuccess @openSuccess = openSuccess
@openError = openError @openError = openError
throw new Error("Cannot create a SQLitePlugin instance without a dbPath") unless dbPath
@openSuccess or (@openSuccess = -> @openSuccess or
console.log "DB opened: " + dbPath @openSuccess = ->
) console.log "DB opened: " + dbPath
@openError or (@openError = (e) ->
console.log e.message @openError or
) @openError = (e) ->
console.log e.message
@open @openSuccess, @openError @open @openSuccess, @openError
return return
SQLitePlugin::openDBs = {} SQLitePlugin::openDBs = {}
SQLitePlugin::transaction = (fn, error, success) -> SQLitePlugin::transaction = (fn, error, success) ->
t = new SQLitePluginTransaction(@dbPath) t = new SQLitePluginTransaction(@dbPath)
fn t fn t
t.complete success, error t.complete success, error
return
SQLitePlugin::open = (success, error) -> SQLitePlugin::open = (success, error) ->
console.log "SQLitePlugin.prototype.open" console.log "SQLitePlugin.prototype.open"
opts = undefined
unless @dbPath of @openDBs unless @dbPath of @openDBs
@openDBs[@dbPath] = true @openDBs[@dbPath] = true
cordova.exec success, error, "SQLitePlugin", "open", [ @dbPath ] cordova.exec success, error, "SQLitePlugin", "open", [ @dbPath ]
return
SQLitePlugin::close = (success, error) -> SQLitePlugin::close = (success, error) ->
console.log "SQLitePlugin.prototype.close" console.log "SQLitePlugin.prototype.close"
opts = undefined
if @dbPath of @openDBs if @dbPath of @openDBs
delete @openDBs[@dbPath] delete @openDBs[@dbPath]
cordova.exec null, null, "SQLitePlugin", "close", [ @dbPath ] cordova.exec null, null, "SQLitePlugin", "close", [ @dbPath ]
return
pcb = -> 1 pcb = -> 1
SQLitePlugin::executePragmaStatement = (statement, success, error) -> SQLitePlugin::executePragmaStatement = (statement, success, error) ->
@ -81,8 +90,9 @@ do ->
transaction_callback_queue[@trans_id] = new Object() transaction_callback_queue[@trans_id] = new Object()
return return
# XXX FUTURE handle tx CBs under SQLitePluginCallback object: SQLitePluginTransactionCB = {}
SQLitePluginTransaction.queryCompleteCallback = (transId, queryId, result) ->
SQLitePluginTransactionCB.queryCompleteCallback = (transId, queryId, result) ->
console.log "SQLitePluginTransaction.queryCompleteCallback" console.log "SQLitePluginTransaction.queryCompleteCallback"
query = null query = null
for x of transaction_queue[transId] for x of transaction_queue[transId]
@ -95,7 +105,7 @@ do ->
break break
query["callback"] result if query and query["callback"] query["callback"] result if query and query["callback"]
SQLitePluginTransaction.queryErrorCallback = (transId, queryId, result) -> SQLitePluginTransactionCB.queryErrorCallback = (transId, queryId, result) ->
query = null query = null
for x of transaction_queue[transId] for x of transaction_queue[transId]
if transaction_queue[transId][x]["query_id"] is queryId if transaction_queue[transId][x]["query_id"] is queryId
@ -107,13 +117,13 @@ do ->
break break
query["err_callback"] result if query and query["err_callback"] query["err_callback"] result if query and query["err_callback"]
SQLitePluginTransaction.txCompleteCallback = (transId) -> SQLitePluginTransactionCB.txCompleteCallback = (transId) ->
unless typeof transId is "undefined" unless typeof transId is "undefined"
transaction_callback_queue[transId]["success"]() if transId and transaction_callback_queue[transId] and transaction_callback_queue[transId]["success"] transaction_callback_queue[transId]["success"]() if transId and transaction_callback_queue[transId] and transaction_callback_queue[transId]["success"]
else else
console.log "SQLitePluginTransaction.txCompleteCallback---transId = NULL" console.log "SQLitePluginTransaction.txCompleteCallback---transId = NULL"
SQLitePluginTransaction.txErrorCallback = (transId, error) -> SQLitePluginTransactionCB.txErrorCallback = (transId, error) ->
unless typeof transId is "undefined" unless typeof transId is "undefined"
console.log "SQLitePluginTransaction.txErrorCallback---transId:" + transId console.log "SQLitePluginTransaction.txErrorCallback---transId:" + transId
transaction_callback_queue[transId]["error"] error if transId and transaction_callback_queue[transId]["error"] transaction_callback_queue[transId]["error"] error if transId and transaction_callback_queue[transId]["error"]
@ -126,18 +136,24 @@ do ->
SQLitePluginTransaction::add_to_transaction = (trans_id, query, params, callback, err_callback) -> SQLitePluginTransaction::add_to_transaction = (trans_id, query, params, callback, err_callback) ->
new_query = new Object() new_query = new Object()
new_query["trans_id"] = trans_id new_query["trans_id"] = trans_id
if callback or not @optimization_no_nested_callbacks if callback or not @optimization_no_nested_callbacks
new_query["query_id"] = get_unique_id() new_query["query_id"] = get_unique_id()
else new_query["query_id"] = "" if @optimization_no_nested_callbacks else new_query["query_id"] = "" if @optimization_no_nested_callbacks
new_query["query"] = query new_query["query"] = query
if params if params
new_query["params"] = params new_query["params"] = params
else else
new_query["params"] = [] new_query["params"] = []
new_query["callback"] = callback new_query["callback"] = callback
new_query["err_callback"] = err_callback new_query["err_callback"] = err_callback
transaction_queue[trans_id] = [] unless transaction_queue[trans_id] transaction_queue[trans_id] = [] unless transaction_queue[trans_id]
transaction_queue[trans_id].push new_query transaction_queue[trans_id].push new_query
return
SQLitePluginTransaction::executeSql = (sql, values, success, error) -> SQLitePluginTransaction::executeSql = (sql, values, success, error) ->
console.log "SQLitePluginTransaction.prototype.executeSql" console.log "SQLitePluginTransaction.prototype.executeSql"
@ -170,22 +186,21 @@ do ->
if error if error
errorcb = (res) -> errorcb = (res) ->
error txself, res error txself, res
@add_to_transaction @trans_id, sql, values, successcb, errorcb
console.log "executeSql - add_to_transaction" + sql console.log "executeSql - add_to_transaction" + sql
@add_to_transaction @trans_id, sql, values, successcb, errorcb
return
SQLitePluginTransaction::complete = (success, error) -> SQLitePluginTransaction::complete = (success, error) ->
console.log "SQLitePluginTransaction.prototype.complete" console.log "SQLitePluginTransaction.prototype.complete"
begin_opts = undefined
commit_opts = undefined
errorcb = undefined
executes = undefined
opts = undefined
successcb = undefined
txself = undefined
throw new Error("Transaction already run") if @__completed throw new Error("Transaction already run") if @__completed
throw new Error("Transaction already submitted") if @__submitted throw new Error("Transaction already submitted") if @__submitted
@__submitted = true @__submitted = true
txself = this txself = this
successcb = -> successcb = ->
if transaction_queue[txself.trans_id].length > 0 and not txself.optimization_no_nested_callbacks if transaction_queue[txself.trans_id].length > 0 and not txself.optimization_no_nested_callbacks
txself.__submitted = false txself.__submitted = false
@ -194,7 +209,7 @@ do ->
@__completed = true @__completed = true
success txself if success success txself if success
errorcb = (res) -> errorcb = (res) -> null
if error if error
errorcb = (res) -> errorcb = (res) ->
@ -204,11 +219,11 @@ do ->
transaction_callback_queue[@trans_id]["error"] = errorcb transaction_callback_queue[@trans_id]["error"] = errorcb
cordova.exec null, null, "SQLitePlugin", "executeSqlBatch", [ @dbPath, transaction_queue[@trans_id] ] cordova.exec null, null, "SQLitePlugin", "executeSqlBatch", [ @dbPath, transaction_queue[@trans_id] ]
return
# XXX FUTURE all CBs under SQLitePluginCallback # Required for callbacks:
# required for callbacks:
root.SQLitePluginTransaction = SQLitePluginTransaction
root.SQLitePluginCallback = SQLitePluginCallback root.SQLitePluginCallback = SQLitePluginCallback
root.SQLitePluginTransactionCB = SQLitePluginTransactionCB
root.sqlitePlugin = root.sqlitePlugin =
openDatabase: (dbPath, version, displayName, estimatedSize, creationCallback, errorCallback) -> openDatabase: (dbPath, version, displayName, estimatedSize, creationCallback, errorCallback) ->

View File

@ -1,14 +1,14 @@
(function() { (function() {
var SQLitePlugin, SQLitePluginCallback, SQLitePluginTransaction, get_unique_id, pcb, root, transaction_callback_queue, transaction_queue; var SQLitePlugin, SQLitePluginCallback, SQLitePluginTransaction, SQLitePluginTransactionCB, get_unique_id, pcb, root, transaction_callback_queue, transaction_queue;
root = this; root = this;
SQLitePlugin = function(dbPath, openSuccess, openError) { SQLitePlugin = function(dbPath, openSuccess, openError) {
console.log("SQLitePlugin"); console.log("SQLitePlugin");
this.dbPath = dbPath;
this.openSuccess = openSuccess;
this.openError = openError;
if (!dbPath) { if (!dbPath) {
throw new Error("Cannot create a SQLitePlugin instance without a dbPath"); throw new Error("Cannot create a SQLitePlugin instance without a dbPath");
} }
this.dbPath = dbPath;
this.openSuccess = openSuccess;
this.openError = openError;
this.openSuccess || (this.openSuccess = function() { this.openSuccess || (this.openSuccess = function() {
return console.log("DB opened: " + dbPath); return console.log("DB opened: " + dbPath);
}); });
@ -22,24 +22,20 @@
var t; var t;
t = new SQLitePluginTransaction(this.dbPath); t = new SQLitePluginTransaction(this.dbPath);
fn(t); fn(t);
return t.complete(success, error); t.complete(success, error);
}; };
SQLitePlugin.prototype.open = function(success, error) { SQLitePlugin.prototype.open = function(success, error) {
var opts;
console.log("SQLitePlugin.prototype.open"); console.log("SQLitePlugin.prototype.open");
opts = void 0;
if (!(this.dbPath in this.openDBs)) { if (!(this.dbPath in this.openDBs)) {
this.openDBs[this.dbPath] = true; this.openDBs[this.dbPath] = true;
return cordova.exec(success, error, "SQLitePlugin", "open", [this.dbPath]); cordova.exec(success, error, "SQLitePlugin", "open", [this.dbPath]);
} }
}; };
SQLitePlugin.prototype.close = function(success, error) { SQLitePlugin.prototype.close = function(success, error) {
var opts;
console.log("SQLitePlugin.prototype.close"); console.log("SQLitePlugin.prototype.close");
opts = void 0;
if (this.dbPath in this.openDBs) { if (this.dbPath in this.openDBs) {
delete this.openDBs[this.dbPath]; delete this.openDBs[this.dbPath];
return cordova.exec(null, null, "SQLitePlugin", "close", [this.dbPath]); cordova.exec(null, null, "SQLitePlugin", "close", [this.dbPath]);
} }
}; };
pcb = function() { pcb = function() {
@ -85,7 +81,8 @@
transaction_queue[this.trans_id] = []; transaction_queue[this.trans_id] = [];
transaction_callback_queue[this.trans_id] = new Object(); transaction_callback_queue[this.trans_id] = new Object();
}; };
SQLitePluginTransaction.queryCompleteCallback = function(transId, queryId, result) { SQLitePluginTransactionCB = {};
SQLitePluginTransactionCB.queryCompleteCallback = function(transId, queryId, result) {
var query, x; var query, x;
console.log("SQLitePluginTransaction.queryCompleteCallback"); console.log("SQLitePluginTransaction.queryCompleteCallback");
query = null; query = null;
@ -102,7 +99,7 @@
} }
if (query && query["callback"]) return query["callback"](result); if (query && query["callback"]) return query["callback"](result);
}; };
SQLitePluginTransaction.queryErrorCallback = function(transId, queryId, result) { SQLitePluginTransactionCB.queryErrorCallback = function(transId, queryId, result) {
var query, x; var query, x;
query = null; query = null;
for (x in transaction_queue[transId]) { for (x in transaction_queue[transId]) {
@ -118,7 +115,7 @@
} }
if (query && query["err_callback"]) return query["err_callback"](result); if (query && query["err_callback"]) return query["err_callback"](result);
}; };
SQLitePluginTransaction.txCompleteCallback = function(transId) { SQLitePluginTransactionCB.txCompleteCallback = function(transId) {
if (typeof transId !== "undefined") { if (typeof transId !== "undefined") {
if (transId && transaction_callback_queue[transId] && transaction_callback_queue[transId]["success"]) { if (transId && transaction_callback_queue[transId] && transaction_callback_queue[transId]["success"]) {
return transaction_callback_queue[transId]["success"](); return transaction_callback_queue[transId]["success"]();
@ -127,7 +124,7 @@
return console.log("SQLitePluginTransaction.txCompleteCallback---transId = NULL"); return console.log("SQLitePluginTransaction.txCompleteCallback---transId = NULL");
} }
}; };
SQLitePluginTransaction.txErrorCallback = function(transId, error) { SQLitePluginTransactionCB.txErrorCallback = function(transId, error) {
if (typeof transId !== "undefined") { if (typeof transId !== "undefined") {
console.log("SQLitePluginTransaction.txErrorCallback---transId:" + transId); console.log("SQLitePluginTransaction.txErrorCallback---transId:" + transId);
if (transId && transaction_callback_queue[transId]["error"]) { if (transId && transaction_callback_queue[transId]["error"]) {
@ -157,7 +154,7 @@
new_query["callback"] = callback; new_query["callback"] = callback;
new_query["err_callback"] = err_callback; new_query["err_callback"] = err_callback;
if (!transaction_queue[trans_id]) transaction_queue[trans_id] = []; if (!transaction_queue[trans_id]) transaction_queue[trans_id] = [];
return transaction_queue[trans_id].push(new_query); transaction_queue[trans_id].push(new_query);
}; };
SQLitePluginTransaction.prototype.executeSql = function(sql, values, success, error) { SQLitePluginTransaction.prototype.executeSql = function(sql, values, success, error) {
var errorcb, successcb, txself; var errorcb, successcb, txself;
@ -196,19 +193,12 @@
return error(txself, res); return error(txself, res);
}; };
} }
console.log("executeSql - add_to_transaction" + sql);
this.add_to_transaction(this.trans_id, sql, values, successcb, errorcb); this.add_to_transaction(this.trans_id, sql, values, successcb, errorcb);
return console.log("executeSql - add_to_transaction" + sql);
}; };
SQLitePluginTransaction.prototype.complete = function(success, error) { SQLitePluginTransaction.prototype.complete = function(success, error) {
var begin_opts, commit_opts, errorcb, executes, opts, successcb, txself; var errorcb, successcb, txself;
console.log("SQLitePluginTransaction.prototype.complete"); console.log("SQLitePluginTransaction.prototype.complete");
begin_opts = void 0;
commit_opts = void 0;
errorcb = void 0;
executes = void 0;
opts = void 0;
successcb = void 0;
txself = void 0;
if (this.__completed) throw new Error("Transaction already run"); if (this.__completed) throw new Error("Transaction already run");
if (this.__submitted) throw new Error("Transaction already submitted"); if (this.__submitted) throw new Error("Transaction already submitted");
this.__submitted = true; this.__submitted = true;
@ -222,7 +212,9 @@
if (success) return success(txself); if (success) return success(txself);
} }
}; };
errorcb = function(res) {}; errorcb = function(res) {
return null;
};
if (error) { if (error) {
errorcb = function(res) { errorcb = function(res) {
return error(txself, res); return error(txself, res);
@ -230,10 +222,10 @@
} }
transaction_callback_queue[this.trans_id]["success"] = successcb; transaction_callback_queue[this.trans_id]["success"] = successcb;
transaction_callback_queue[this.trans_id]["error"] = errorcb; transaction_callback_queue[this.trans_id]["error"] = errorcb;
return cordova.exec(null, null, "SQLitePlugin", "executeSqlBatch", [this.dbPath, transaction_queue[this.trans_id]]); cordova.exec(null, null, "SQLitePlugin", "executeSqlBatch", [this.dbPath, transaction_queue[this.trans_id]]);
}; };
root.SQLitePluginTransaction = SQLitePluginTransaction;
root.SQLitePluginCallback = SQLitePluginCallback; root.SQLitePluginCallback = SQLitePluginCallback;
root.SQLitePluginTransactionCB = SQLitePluginTransactionCB;
return root.sqlitePlugin = { return root.sqlitePlugin = {
openDatabase: function(dbPath, version, displayName, estimatedSize, creationCallback, errorCallback) { openDatabase: function(dbPath, version, displayName, estimatedSize, creationCallback, errorCallback) {
return new SQLitePlugin(dbPath, creationCallback, errorCallback); return new SQLitePlugin(dbPath, creationCallback, errorCallback);

View File

@ -197,7 +197,7 @@ public class SQLitePlugin extends Plugin {
long insertId = myStatement.executeInsert(); long insertId = myStatement.executeInsert();
String result = "{'insertId':'" + insertId + "'}"; String result = "{'insertId':'" + insertId + "'}";
this.sendJavascript("SQLitePluginTransaction.queryCompleteCallback('" + this.sendJavascript("SQLitePluginTransactionCB.queryCompleteCallback('" +
tx_id + "','" + query_id + "', " + result + ");"); tx_id + "','" + query_id + "', " + result + ");");
} else { } else {
String[] params = null; String[] params = null;
@ -226,16 +226,16 @@ public class SQLitePlugin extends Plugin {
catch (SQLiteException ex) { catch (SQLiteException ex) {
ex.printStackTrace(); ex.printStackTrace();
Log.v("executeSqlBatch", "SQLitePlugin.executeSql(): Error=" + ex.getMessage()); Log.v("executeSqlBatch", "SQLitePlugin.executeSql(): Error=" + ex.getMessage());
this.sendJavascript("SQLitePluginTransaction.txErrorCallback('" + tx_id + "', '"+ex.getMessage()+"');"); this.sendJavascript("SQLitePluginTransactionCB.txErrorCallback('" + tx_id + "', '"+ex.getMessage()+"');");
} catch (JSONException ex) { } catch (JSONException ex) {
ex.printStackTrace(); ex.printStackTrace();
Log.v("executeSqlBatch", "SQLitePlugin.executeSql(): Error=" + ex.getMessage()); Log.v("executeSqlBatch", "SQLitePlugin.executeSql(): Error=" + ex.getMessage());
this.sendJavascript("SQLitePluginTransaction.txErrorCallback('" + tx_id + "', '"+ex.getMessage()+"');"); this.sendJavascript("SQLitePluginTransactionCB.txErrorCallback('" + tx_id + "', '"+ex.getMessage()+"');");
} }
finally { finally {
myDb.endTransaction(); myDb.endTransaction();
Log.v("executeSqlBatch", tx_id); Log.v("executeSqlBatch", tx_id);
this.sendJavascript("SQLitePluginTransaction.txCompleteCallback('" + tx_id + "');"); this.sendJavascript("SQLitePluginTransactionCB.txCompleteCallback('" + tx_id + "');");
} }
} }
@ -253,7 +253,7 @@ public class SQLitePlugin extends Plugin {
{ {
String result = this.results2string(cur); String result = this.results2string(cur);
this.sendJavascript("SQLitePluginTransaction.queryCompleteCallback('" + this.sendJavascript("SQLitePluginTransactionCB.queryCompleteCallback('" +
tx_id + "','" + query_id + "', " + result + ");"); tx_id + "','" + query_id + "', " + result + ");");
} }