Comparing version 0.4.1 to 0.4.2
@@ -9,47 +9,63 @@ 'use strict'; | ||
function runTransaction(self, task) { | ||
function onTransactionComplete(err) { | ||
function done() { | ||
self._running = false; | ||
var ROLLBACK = [ | ||
{sql: 'ROLLBACK;', args: []}, | ||
{sql: 'END;', args: []} | ||
]; | ||
immediate(function () { | ||
immediate(function () { | ||
runNextTransaction(self); | ||
}); | ||
if (err) { | ||
task.errorCallback(err); | ||
} else { | ||
task.successCallback(); | ||
} | ||
}); | ||
} | ||
var COMMIT = [ | ||
{sql: 'END;', args: []} | ||
]; | ||
// v8 likes predictable objects | ||
function TransactionTask(readOnly, txnCallback, errorCallback, successCallback) { | ||
this.readOnly = readOnly; | ||
this.txnCallback = txnCallback; | ||
this.errorCallback = errorCallback; | ||
this.successCallback = successCallback; | ||
} | ||
function WebSQLDatabase(dbVersion, db) { | ||
this.version = dbVersion; | ||
this._db = db; | ||
this._txnQueue = new Queue(); | ||
this._running = false; | ||
this._currentTask = null; | ||
} | ||
WebSQLDatabase.prototype._onTransactionComplete = function(err) { | ||
var self = this; | ||
function done() { | ||
if (err) { | ||
self._db.exec([ | ||
{sql: 'ROLLBACK;', args: []}, | ||
{sql: 'END TRANSACTION;', args: []} | ||
], false, done); | ||
self._currentTask.errorCallback(err); | ||
} else { | ||
self._db.exec([ | ||
{sql: 'END TRANSACTION;', args: []} | ||
], false, done); | ||
self._currentTask.successCallback(); | ||
} | ||
self._running = false; | ||
self._currentTask = null; | ||
self._runNextTransaction(); | ||
} | ||
var txn = new WebSQLTransaction(self, task.readOnly, onTransactionComplete); | ||
if (err) { | ||
self._db.exec(ROLLBACK, false, done); | ||
} else { | ||
self._db.exec(COMMIT, false, done); | ||
} | ||
}; | ||
function execTransaction() { | ||
task.txnCallback(txn); | ||
WebSQLDatabase.prototype._runTransaction = function () { | ||
var self = this; | ||
var txn = new WebSQLTransaction(self); | ||
immediate(function () { | ||
self._currentTask.txnCallback(txn); | ||
txn._checkDone(); | ||
} | ||
self._db.exec([{sql: 'BEGIN TRANSACTION;', args: []}], false, function () { | ||
immediate(execTransaction); | ||
}); | ||
} | ||
}; | ||
function runNextTransaction(self) { | ||
if (self._running) { | ||
WebSQLDatabase.prototype._runNextTransaction = function() { | ||
if (this._running) { | ||
return; | ||
} | ||
var task = self._txnQueue.shift(); | ||
var task = this._txnQueue.shift(); | ||
@@ -60,15 +76,9 @@ if (!task) { | ||
self._running = true; | ||
runTransaction(self, task); | ||
} | ||
this._currentTask = task; | ||
this._running = true; | ||
this._runTransaction(); | ||
}; | ||
function TransactionTask(readOnly, txnCallback, errorCallback, successCallback) { | ||
this.readOnly = readOnly; | ||
this.txnCallback = txnCallback; | ||
this.errorCallback = errorCallback; | ||
this.successCallback = successCallback; | ||
} | ||
function createTransaction(self, readOnly, txnCallback, errorCallback, successCallback) { | ||
txnCallback = txnCallback; | ||
WebSQLDatabase.prototype._createTransaction = function( | ||
readOnly, txnCallback, errorCallback, successCallback) { | ||
errorCallback = errorCallback || noop; | ||
@@ -81,21 +91,14 @@ successCallback = successCallback || noop; | ||
self._txnQueue.push(new TransactionTask(readOnly, txnCallback, errorCallback, successCallback)); | ||
runNextTransaction(self); | ||
} | ||
this._txnQueue.push(new TransactionTask(readOnly, txnCallback, errorCallback, successCallback)); | ||
this._runNextTransaction(); | ||
}; | ||
function WebSQLDatabase(dbVersion, db) { | ||
this.version = dbVersion; | ||
this._db = db; | ||
this._txnQueue = new Queue(); | ||
this._running = false; | ||
} | ||
WebSQLDatabase.prototype.transaction = function (txnCallback, errorCallback, successCallback) { | ||
createTransaction(this, false, txnCallback, errorCallback, successCallback); | ||
this._createTransaction(false, txnCallback, errorCallback, successCallback); | ||
}; | ||
WebSQLDatabase.prototype.readTransaction = function (txnCallback, errorCallback, successCallback) { | ||
createTransaction(this, true, txnCallback, errorCallback, successCallback); | ||
this._createTransaction(true, txnCallback, errorCallback, successCallback); | ||
}; | ||
module.exports = WebSQLDatabase; |
@@ -52,3 +52,5 @@ 'use strict'; | ||
self._websqlDatabase._db.exec(batch, self._readOnly, function (err, results) { | ||
var readOnly = self._websqlDatabase._currentTask.readOnly; | ||
self._websqlDatabase._db.exec(batch, readOnly, function (err, results) { | ||
/* istanbul ignore next */ | ||
@@ -83,7 +85,7 @@ if (err) { | ||
self._complete = true; | ||
return self._completeCallback(self._error); | ||
return self._websqlDatabase._onTransactionComplete(self._error); | ||
} | ||
if (!self._sqlQueue.length) { | ||
self._complete = true; | ||
return self._completeCallback(); | ||
return self._websqlDatabase._onTransactionComplete(); | ||
} | ||
@@ -111,10 +113,9 @@ self._running = true; | ||
function WebSQLTransaction(websqlDatabase, readOnly, completeCallback) { | ||
function WebSQLTransaction(websqlDatabase) { | ||
this._websqlDatabase = websqlDatabase; | ||
this._readOnly = readOnly; | ||
this._completeCallback = completeCallback; | ||
this._sqlQueue = new Queue(); | ||
this._error = null; | ||
this._complete = false; | ||
this._runningTimeout = false; | ||
this._sqlQueue = new Queue(); | ||
this._sqlQueue.push(new SQLTask('BEGIN;', [], noop, noop)); | ||
} | ||
@@ -121,0 +122,0 @@ |
{ | ||
"name": "websql", | ||
"version": "0.4.1", | ||
"version": "0.4.2", | ||
"description": "WebSQL Database API, implemented for Node using sqlite3", | ||
@@ -5,0 +5,0 @@ "repository": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29306
320