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

View File

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

View File

@ -197,7 +197,7 @@ public class SQLitePlugin extends Plugin {
long insertId = myStatement.executeInsert();
String result = "{'insertId':'" + insertId + "'}";
this.sendJavascript("SQLitePluginTransaction.queryCompleteCallback('" +
this.sendJavascript("SQLitePluginTransactionCB.queryCompleteCallback('" +
tx_id + "','" + query_id + "', " + result + ");");
} else {
String[] params = null;
@ -226,16 +226,16 @@ public class SQLitePlugin extends Plugin {
catch (SQLiteException ex) {
ex.printStackTrace();
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) {
ex.printStackTrace();
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 {
myDb.endTransaction();
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);
this.sendJavascript("SQLitePluginTransaction.queryCompleteCallback('" +
this.sendJavascript("SQLitePluginTransactionCB.queryCompleteCallback('" +
tx_id + "','" + query_id + "', " + result + ");");
}