JSFiddle - React, Tailwind, and code Playground
JavaScript
(function(win){
function Database(){
//public vars
// hold the database name
this.name;
// private vars
// holds the Database connection
this.connection;
// holds the result set for the last query
var lastResultSet;
// holds the row count for last query
var lastResultCount;
// holds the last inserted id
var lastInsertId;
// holds if a query has been run or not
var used = false;
// holds if we're blocking for thread exicution
var blocking = false;
// private methods
var blocked = function(){ throw "Thread is currently blocked can't access data."; };
var notUsed = function(){ throw "There has been no query on this database to have results from."; };
// public methods
// opens the database for use
this.open = function(name){
this.name = name;
this.connection = win.openDatabase(this.name, '1.0', this.name, 2 * 1024 * 1024);
};
// allows query with results and var replace
this.query = function(sql, success, vars){
used = blocking = true;
if(typeof success === "undefined"){ success = function(rs){}; }
if(typeof vars === "undefined"){ vars = []; }
this.connection.transaction(function(t){
t.executeSql(sql, vars, function(tx, results){
lastResultSet = results.rows;
lastResultCount = results.rows.length;
try{
lastInsertId = results.insertId;
}catch(ex){}
blocking = false;
success(results);
}, function(t, err){
console.log(t, err);
throw "Unhandled SQL Error please check console.";
});
});
};
// enable support for monitoring the thread locking
this.isBlocked = function(){ return blocking; };
this.waitForBlockLift = function(){
var test = this.isBlocked();
while(test){
test = this.isBlocked();
}
return true;
}
// returns the count of returned rows from last query
this.count = function(){
if(blocking){ blocked(); }
if(!used){ notUsed();...