addColumn('reason', lf.Type.STRING).
addPrimaryKey(['barcode']);
-lf.connecting = true;
-osb.connect().then(function (db) {
- lf.offlineDB = db;
- lf.connecting = false;
-});
-
/**
* Core Service - egLovefield
*
var service = {};
- function connectOrGo (resolver) {
- if (lf.offlineDB) {
- return resolver();
+ function connectOrGo() {
+
+ if (lf.offlineDB) { // offline DB connected
+ return $q.when();
}
- // apparently, this might take a while...
- if (lf.connecting) return $timeout(function() {
- return connectOrGo(resolver);
- });
+ if (service.cannotConnect) { // connection will never happen
+ return $q.reject();
+ }
- console.log('egLovefield connecting to offline DB');
+ if (service.connectPromise) { // connection in progress
+ return service.connectPromise;
+ }
- try {
- return osb.connect().then(function (db) {
+ // start a new connection attempt
+
+ var deferred = $q.defer();
+
+ console.debug('attempting offline DB connection');
+ osb.connect().then(
+ function(db) {
+ console.debug('successfully connected to offline DB');
+ service.connectPromise = null;
lf.offlineDB = db;
- return resolver();
- });
- } catch (err) {
- alert('attempted reconnect failure: ' + err.toString());
- }
+ deferred.resolve();
+ },
+ function(err) {
+ // assumes that a single connection failure means
+ // a connection will never succeed.
+ service.cannotConnect = true;
+ console.error('Cannot connect to offline DB: ' + err);
+ }
+ );
+
+ service.connectPromise = deferred.promise;
+ return service.connectPromise;
}
service.isCacheGood = function (type) {
- return connectOrGo(function() {
+ return connectOrGo().then(function() {
var cacheDate = lf.offlineDB.getSchema().table('CacheDate');
return lf.offlineDB.
}
service.destroyPendingOfflineXacts = function () {
- return connectOrGo(function() {
+ return connectOrGo().then(function() {
var table = lf.offlineDB.getSchema().table('OfflineXact');
return lf.offlineDB.
delete().
}
service.havePendingOfflineXacts = function () {
- return connectOrGo(function() {
+ return connectOrGo().then(function() {
var table = lf.offlineDB.getSchema().table('OfflineXact');
return lf.offlineDB.
select(table.reason).
}
service.retrievePendingOfflineXacts = function () {
- return connectOrGo(function() {
+ return connectOrGo().then(function() {
var table = lf.offlineDB.getSchema().table('OfflineXact');
return lf.offlineDB.
select(table.value).
}
service.destroyOfflineBlocks = function () {
- return connectOrGo(function() {
+ return connectOrGo().then(function() {
var table = lf.offlineDB.getSchema().table('OfflineBlocks');
return $q.when(
lf.offlineDB.
}
service.addOfflineBlock = function (barcode, reason) {
- return connectOrGo(function() {
+ return connectOrGo().then(function() {
var table = lf.offlineDB.getSchema().table('OfflineBlocks');
return $q.when(
lf.offlineDB.
// Returns a promise with true for blocked, false for not blocked
service.testOfflineBlock = function (barcode) {
- return connectOrGo(function() {
+ return connectOrGo().then(function() {
var table = lf.offlineDB.getSchema().table('OfflineBlocks');
return lf.offlineDB.
select(table.reason).
}
service.addOfflineXact = function (obj) {
- return connectOrGo(function() {
+ return connectOrGo().then(function() {
var table = lf.offlineDB.getSchema().table('OfflineXact');
return $q.when(
lf.offlineDB.
service.setStatCatsCache = function (statcats) {
if (lf.isOffline) return $q.when();
- return connectOrGo(function() {
+ return connectOrGo().then(function() {
var table = lf.offlineDB.getSchema().table('StatCat');
var rlist = [];
}
service.getStatCatsCache = function () {
- return connectOrGo(function() {
+ return connectOrGo().then(function() {
var table = lf.offlineDB.getSchema().table('StatCat');
var result = [];
service.setSettingsCache = function (settings) {
if (lf.isOffline) return $q.when();
- return connectOrGo(function() {
+ return connectOrGo().then(function() {
var table = lf.offlineDB.getSchema().table('Setting');
var rlist = [];
}
service.getSettingsCache = function (settings) {
- return connectOrGo(function() {
+ return connectOrGo().then(function() {
var table = lf.offlineDB.getSchema().table('Setting');
service.setListInOfflineCache = function (type, list) {
if (lf.isOffline) return $q.when();
- return connectOrGo(function() {
+ return connectOrGo().then(function() {
service.isCacheGood(type).then(function(good) {
if (!good) {
}
service.getListFromOfflineCache = function(type) {
- return connectOrGo(function() {
+ return connectOrGo().then(function() {
var object = lf.offlineDB.getSchema().table('Object');