diff --git a/SQLitePlugin.coffee.md b/SQLitePlugin.coffee.md index 82c301785..896266741 100644 --- a/SQLitePlugin.coffee.md +++ b/SQLitePlugin.coffee.md @@ -15,6 +15,10 @@ License for common Javascript: MIT or Apache READ_ONLY_REGEX = /^\s*(?:drop|delete|insert|update|create)\s/i IOS_REGEX = /iP(?:ad|hone|od)/ + nextTick = window.setImmediate || (fun) -> + window.setTimeout(fun, 0) + return + - SQLitePlugin object is defined by a constructor function and prototype member functions: SQLitePlugin = (openargs, openSuccess, openError) -> @@ -59,8 +63,7 @@ License for common Javascript: MIT or Apache SQLitePlugin::addTransaction = (t) -> @txQ.push t - if @txQ.length is 1 - t.start() + @.startNextTransaction() return SQLitePlugin::transaction = (fn, error, success) -> @@ -72,9 +75,12 @@ License for common Javascript: MIT or Apache return SQLitePlugin::startNextTransaction = -> - @txQ.shift() - if @txQ[0] - @txQ[0].start() + tx = @ + + nextTick () -> + if tx.txQ.length > 0 + tx.txQ.shift().start() + return return SQLitePlugin::open = (success, error) -> diff --git a/test-www/www/index.html b/test-www/www/index.html index 2fc715196..2cd23219c 100755 --- a/test-www/www/index.html +++ b/test-www/www/index.html @@ -424,6 +424,23 @@ }); }); + test(suiteName + ' test callback order', function () { + stop(); + var db = openDatabase("Database-Callback-Order", "1.0", "Demo", DEFAULT_SIZE); + var blocked = true; + + db.transaction(function(tx) { + ok(!blocked, 'callback to the transaction shouldn\'t block (1)'); + tx.executeSql('SELECT 1 from sqlite_master', [], function () { + ok(!blocked, 'callback to the transaction shouldn\'t block (2)'); + }); + }, function(err) { ok(false, err.message) }, function() { + start(); + ok(!blocked, 'callback to the transaction shouldn\'t block (3)'); + }); + blocked = false; + }); + /** test(suiteName + "PRAGMA & multiple databases", function() { var db = openDatabase("DB1", "1.0", "Demo", DEFAULT_SIZE); diff --git a/www/SQLitePlugin.js b/www/SQLitePlugin.js index 29aa04ccb..fae2a9eef 100644 --- a/www/SQLitePlugin.js +++ b/www/SQLitePlugin.js @@ -1,6 +1,6 @@ // Generated by CoffeeScript 1.7.1 (function() { - var IOS_REGEX, READ_ONLY_REGEX, SQLiteFactory, SQLitePlugin, SQLitePluginCallback, SQLitePluginTransaction, pcb, root; + var IOS_REGEX, READ_ONLY_REGEX, SQLiteFactory, SQLitePlugin, SQLitePluginCallback, SQLitePluginTransaction, nextTick, pcb, root; root = this; @@ -8,6 +8,10 @@ IOS_REGEX = /iP(?:ad|hone|od)/; + nextTick = window.setImmediate || function(fun) { + window.setTimeout(fun, 0); + }; + SQLitePlugin = function(openargs, openSuccess, openError) { var dbname; console.log("SQLitePlugin openargs: " + (JSON.stringify(openargs))); @@ -39,9 +43,7 @@ SQLitePlugin.prototype.addTransaction = function(t) { this.txQ.push(t); - if (this.txQ.length === 1) { - t.start(); - } + this.startNextTransaction(); }; SQLitePlugin.prototype.transaction = function(fn, error, success) { @@ -53,10 +55,13 @@ }; SQLitePlugin.prototype.startNextTransaction = function() { - this.txQ.shift(); - if (this.txQ[0]) { - this.txQ[0].start(); - } + var tx; + tx = this; + nextTick(function() { + if (tx.txQ.length > 0) { + tx.txQ.shift().start(); + } + }); }; SQLitePlugin.prototype.open = function(success, error) {