Skip to content

Commit

Permalink
cache_or_http promise cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pliablepixels committed Oct 23, 2020
1 parent 08857ae commit b24af50
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 26 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "zmninjapro",
"description": "Home security mobile app for ZoneMinder",
"version": "1.6.000",
"version": "1.6.002",
"displayName": "zmNinja",
"author": "Pliable Pixels",
"license": "custom see LICENSE.md",
Expand Down Expand Up @@ -70,7 +70,9 @@
"ANDROID_GSON_VERSION": "2.8.6"
},
"cordova-plugin-advanced-http": {},
"cordova-plugin-ionic-webview": {},
"cordova-plugin-ionic-webview": {
"ANDROID_SUPPORT_ANNOTATIONS_VERSION": "27.+"
},
"cordova-plugin-media-pp-fork": {}
}
},
Expand Down
47 changes: 23 additions & 24 deletions www/js/NVR.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ angular.module('zmApp.controllers')
DO NOT TOUCH zmAppVersion
It is changed by sync_version.sh
*/
var zmAppVersion = "1.6.000";
var zmAppVersion = "1.6.002";

var isBackground = false;
var justResumed = false;
Expand Down Expand Up @@ -431,7 +431,7 @@ angular.module('zmApp.controllers')

function cache_or_http(url,key,doCrypt, expiry) {


if (!loginData.useAPICaching) {
debug ('CACHE: Not being used, as it is disabled');
return $http.get(url);
Expand All @@ -443,19 +443,20 @@ angular.module('zmApp.controllers')
if (!expiry) expiry = 3600;
if (!doCrypt) doCrypt = false;

localforage.getItem(key)
return localforage.getItem(key)
.then (function (cache_data) {
if (cache_data) {
debug ('CACHE: found for key: '+key+' with expiry of:'+cache_data.expiry+'s');

data = cache_data.data;
t = moment(cache_data.time);
diff = moment().diff(t,'seconds');

if (diff >=cache_data.expiry) {
diff = moment().diff(t,'seconds');
if (diff >=cache_data.expiry ) {
debug ('CACHE: cached value for key:'+key+' has expired as '+diff+' >='+cache_data.expiry);
localforage.removeItem (key)
.then (function() {return cache_or_http(url, key, doCrypt, expiry);})
return localforage.removeItem (key)
.then (function() {
return cache_or_http(url, key, doCrypt, expiry);

})
.catch (function(err) {
debug ('CACHE: error deleting key, err:'+JSON.stringify(err)+' but still proceeding with another call to cache_or_http');
return cache_or_http(url, key, doCrypt, expiry);
Expand All @@ -469,12 +470,11 @@ angular.module('zmApp.controllers')
if (doCrypt) {
debug ('CACHE: decryption requested');
data = decrypt(data);
}
else
} else {
data = JSON.parse(data);

d.resolve(data);
return (d.promise);
}
return (data);

} else {
debug ('CACHE: NOT found for:'+key+ ' reverting to HTTP');
return $http.get(url)
Expand All @@ -494,27 +494,27 @@ angular.module('zmApp.controllers')
cache_entry.data = JSON.stringify(data);
}
cache_entry.time = moment().toString();
//debug ('Setting key:'+key+' data value to:'+cache_entry.data);
localforage.setItem(key, cache_entry);
d.resolve(data);
return d.promise;
//debug ('CACHE: Setting key:'+key+' data value to:'+cache_entry.data);
return localforage.setItem(key, cache_entry)
.then (function() { return (data);});

})
.catch ( function (err) {
log ('CACHE: error with http get '+JSON.stringify(err));
d.reject(err);
return d.promise;
return (err);

});
}

})
.catch ( function (err) {
//debug ('cache_or_http error:'+JSON.stringify(err));
d.reject(err);
return d.promise;
return (err);
//return $http.get(url);
}) ;
//debug ('returning promise');
return d.promise;
//return d.promise;

}

function getZMGroups() {
Expand Down Expand Up @@ -2701,7 +2701,6 @@ angular.module('zmApp.controllers')
if (success.data.version) {
//console.log("API VERSION RETURNED: " + JSON.stringify(success));
$rootScope.apiValid = true;

if (versionCompare(success.data.version, '1.32.0') != -1) {
debug("snapshot supported in image.php");
snapshotFrame = 'snapshot';
Expand Down

0 comments on commit b24af50

Please sign in to comment.