poll new revision date instead of full sync each time

This commit is contained in:
Kyle Spearrin 2017-01-14 12:47:11 -05:00
parent c18cdd6206
commit bb56f9ee47
4 changed files with 63 additions and 13 deletions

View File

@ -618,10 +618,11 @@ setInterval(fullSync, 5 * 60 * 1000); // check every 5 minutes
var syncInternal = 6 * 60 * 60 * 1000; // 6 hours var syncInternal = 6 * 60 * 60 * 1000; // 6 hours
function fullSync(override) { function fullSync(override) {
override = override || false;
syncService.getLastSync(function (lastSync) { syncService.getLastSync(function (lastSync) {
var now = new Date(); var now = new Date();
if (override || !lastSync || (now - lastSync) >= syncInternal) { if (override || !lastSync || (now - lastSync) >= syncInternal) {
syncService.fullSync(function () { }); syncService.fullSync(override, function () { });
} }
}); });
} }

View File

@ -9,7 +9,7 @@
$scope.sync = function () { $scope.sync = function () {
$scope.loading = true; $scope.loading = true;
syncService.fullSync(function () { syncService.fullSync(true, function () {
$analytics.eventTrack('Synced Full'); $analytics.eventTrack('Synced Full');
$scope.loading = false; $scope.loading = false;
toastr.success(i18nService.syncingComplete); toastr.success(i18nService.syncingComplete);

View File

@ -46,6 +46,23 @@ function initApiService() {
// Account APIs // Account APIs
ApiService.prototype.getAccountRevisionDate = function (success, error) {
var self = this;
this.tokenService.getToken(function (token) {
$.ajax({
type: 'GET',
url: self.baseUrl + '/accounts/revision-date?access_token=' + token,
dataType: 'json',
success: function (response) {
success(response);
},
error: function (jqXHR, textStatus, errorThrown) {
handleError(error, jqXHR, textStatus, errorThrown);
}
});
});
};
ApiService.prototype.getProfile = function (success, error) { ApiService.prototype.getProfile = function (success, error) {
var self = this; var self = this;
this.tokenService.getToken(function (token) { this.tokenService.getToken(function (token) {

View File

@ -10,7 +10,7 @@
}; };
function initSyncService() { function initSyncService() {
SyncService.prototype.fullSync = function (callback) { SyncService.prototype.fullSync = function (forceSync, callback) {
if (!callback || typeof callback !== 'function') { if (!callback || typeof callback !== 'function') {
throw 'callback function required'; throw 'callback function required';
} }
@ -27,24 +27,56 @@ function initSyncService() {
self.userService.getUserId(function (userId) { self.userService.getUserId(function (userId) {
var now = new Date(); var now = new Date();
needsSyncing(self, forceSync, function (needsSync) {
if (!needsSync) {
self.syncCompleted(false);
callback(false);
return;
}
var promises = []; var promises = [];
promises.push(syncVault(userId)); promises.push(syncVault(userId));
promises.push(syncSettings(userId)); promises.push(syncSettings(userId));
Q.all(promises).then(function () { Q.all(promises).then(function () {
self.setLastSync(now, function () { self.setLastSync(now, function () {
self.syncCompleted(true); self.syncCompleted(true);
callback(true); callback(true);
});
}, function () {
self.syncCompleted(false);
callback(false);
}); });
}, function () {
self.syncCompleted(false);
callback(false);
}); });
}); });
}); });
}; };
function needsSyncing(self, forceSync, callback) {
if (!callback || typeof callback !== 'function') {
throw 'callback function required';
}
if (forceSync) {
callback(true);
return;
}
self.getLastSync(function (lastSync) {
var now = new Date();
self.apiService.getAccountRevisionDate(function (response) {
var accountRevisionDate = new Date(response);
if (accountRevisionDate <= lastSync) {
callback(false);
return;
}
callback(true);
});
});
}
function syncVault(userId) { function syncVault(userId) {
var deferred = Q.defer(); var deferred = Q.defer();
var self = this; var self = this;