Skip to content

Commit

Permalink
Fixed bug where checking for updates whilst syncs are being processed…
Browse files Browse the repository at this point in the history
… can trigger a local refresh whilst event listeners are enabled (resolves #133).

Fixed bug where startup process can be triggered twice in some instances.
Added a delay on checking for sync updates on startup to allow browser to init connection.
Changed check for updates period back to 15 minutes.
  • Loading branch information
nero120 committed Feb 21, 2020
1 parent 7ce26aa commit 024e32e
Show file tree
Hide file tree
Showing 11 changed files with 305 additions and 299 deletions.
11 changes: 3 additions & 8 deletions js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -779,11 +779,8 @@ xBrowserSync.App.Controller = function ($q, $timeout, platform, globals, api, ut
vm.search.results = null;
vm.sync.enabled = false;

// Disable sync and event listeners
return $q.all([
bookmarks.DisableSync(),
!utility.IsMobilePlatform(vm.platformName) ? platform.EventListeners.Disable() : $q.resolve()
]);
// Disable sync
return bookmarks.DisableSync();
};

var displayAlert = function (title, message, alertType) {
Expand Down Expand Up @@ -980,9 +977,7 @@ xBrowserSync.App.Controller = function ($q, $timeout, platform, globals, api, ut
.then(function () {
return platform.Sync.Await(currentSync.uniqueId);
})
.then(function () {
return vm.view.change(vm.view.views.search);
});
.then(syncBookmarksSuccess);
}

// Return here if view has already been set
Expand Down
89 changes: 60 additions & 29 deletions js/bookmarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ xBrowserSync.App.Bookmarks = function ($q, $timeout, platform, globals, api, uti
return;
}

// Disable checking for sync updates
// Disable event listeners and checking for sync updates
platform.EventListeners.Disable();
platform.AutomaticUpdates.Stop();

// Clear sync queue
Expand All @@ -160,7 +161,7 @@ xBrowserSync.App.Bookmarks = function ($q, $timeout, platform, globals, api, uti
.then(function () {
utility.LogInfo('Sync disabled');

// Refresh interface/icon
// Update browser action icon
platform.Interface.Refresh();
});
});
Expand All @@ -180,6 +181,14 @@ xBrowserSync.App.Bookmarks = function ($q, $timeout, platform, globals, api, uti
})(bookmarks);
};

var enableSync = function () {
return $q.all([
platform.LocalStorage.Set(globals.CacheKeys.SyncEnabled, true),
platform.EventListeners.Enable(),
platform.AutomaticUpdates.Start()
]);
};

var executeSync = function (isBackgroundSync) {
// Check if sync enabled before running sync
return platform.LocalStorage.Get(globals.CacheKeys.SyncEnabled)
Expand Down Expand Up @@ -760,14 +769,6 @@ xBrowserSync.App.Bookmarks = function ($q, $timeout, platform, globals, api, uti
return cleanWordsArr;
};

var enableSync = function () {
utility.LogInfo('Sync enabled');
return $q.all([
platform.LocalStorage.Set(globals.CacheKeys.SyncEnabled, true),
platform.AutomaticUpdates.Start()
]);
};

var findBookmarkInTree = function (id, tree, index) {
if (Array.isArray(tree)) {
tree = {
Expand Down Expand Up @@ -825,6 +826,9 @@ xBrowserSync.App.Bookmarks = function ($q, $timeout, platform, globals, api, uti
};

var handleFailedSync = function (failedSync, err) {
// Update browser action icon
platform.Interface.Refresh();

// If offline swallow error and place failed sync back on the queue
if (err.code === globals.ErrorCodes.NetworkOffline) {
utility.LogInfo('Sync ' + failedSync.uniqueId + ' not committed, network offline (' + syncQueue.length + ' waiting to sync)');
Expand Down Expand Up @@ -972,7 +976,10 @@ xBrowserSync.App.Bookmarks = function ($q, $timeout, platform, globals, api, uti

// If syncing for the first time or re-syncing, set sync as enabled
if (!syncEnabled && currentSync.command !== globals.Commands.RestoreBookmarks) {
return enableSync();
return enableSync()
.then(function () {
utility.LogInfo('Sync enabled');
});
}
})
.then(function () {
Expand All @@ -993,7 +1000,24 @@ xBrowserSync.App.Bookmarks = function ($q, $timeout, platform, globals, api, uti
});
};

return utility.PromiseWhile(syncQueue, condition, action);
// Disable automatic updates whilst processing syncs
return platform.LocalStorage.Get(globals.CacheKeys.SyncEnabled)
.then(function (cachedSyncEnabled) {
if (cachedSyncEnabled) {
return platform.AutomaticUpdates.Stop();
}
})
.then(function () {
return utility.PromiseWhile(syncQueue, condition, action);
})
.finally(function () {
return platform.LocalStorage.Get(globals.CacheKeys.SyncEnabled)
.then(function (cachedSyncEnabled) {
if (cachedSyncEnabled) {
return platform.AutomaticUpdates.Start();
}
});
});
};

var recursiveDelete = function (bookmarks, id) {
Expand Down Expand Up @@ -1225,12 +1249,12 @@ xBrowserSync.App.Bookmarks = function ($q, $timeout, platform, globals, api, uti
};

var setIsSyncing = function (syncType) {
// Refresh interface with current sync type
// Update browser action icon with current sync type
if (syncType != null) {
return platform.Interface.Refresh(null, syncType);
}

// Get cached sync enabled value and refresh interface
// Get cached sync enabled value and update browser action icon
return platform.LocalStorage.Get(globals.CacheKeys.SyncEnabled)
.then(platform.Interface.Refresh);
};
Expand Down Expand Up @@ -1279,9 +1303,17 @@ xBrowserSync.App.Bookmarks = function ($q, $timeout, platform, globals, api, uti
return utility.EncryptData(JSON.stringify(bookmarks))
.then(function (encryptedBookmarks) {
// Update local bookmarks
return (syncData.command === globals.Commands.RestoreBookmarks ?
refreshLocalBookmarks(bookmarks) :
updateLocalBookmarks(updateLocalBookmarksInfo))
return $q(function (resolve, reject) {
platform.EventListeners.Disable()
.then(function () {
return (syncData.command === globals.Commands.RestoreBookmarks ?
refreshLocalBookmarks(bookmarks) :
updateLocalBookmarks(updateLocalBookmarksInfo));
})
.then(resolve)
.catch(reject)
.finally(platform.EventListeners.Enable);
})
.then(function () {
// Commit update to service
return api.UpdateBookmarks(encryptedBookmarks, null, backgroundUpdate)
Expand Down Expand Up @@ -1355,23 +1387,17 @@ xBrowserSync.App.Bookmarks = function ($q, $timeout, platform, globals, api, uti
encryptedBookmarks = encryptedBookmarksWithNewIds;
});
}

/*return !validateBookmarkIds(bookmarks) && repairBookmarkIds(bookmarks)
.then(function (bookmarksWithValidIds) {
// Encrypt bookmarks with new ids
bookmarks = bookmarksWithValidIds;
return utility.EncryptData(JSON.stringify(bookmarks));
})
.then(function (encryptedBookmarksWithNewIds) {
encryptedBookmarks = encryptedBookmarksWithNewIds;
});*/
})
.then(function () {
return updateCachedBookmarks(bookmarks, encryptedBookmarks);
})
.then(function () {
// Update browser bookmarks
return refreshLocalBookmarks(bookmarks);
return platform.EventListeners.Disable()
.then(function () {
return refreshLocalBookmarks(bookmarks);
})
.finally(platform.EventListeners.Enable);
})
.then(function () {
// Update cached last updated date
Expand Down Expand Up @@ -1531,7 +1557,11 @@ xBrowserSync.App.Bookmarks = function ($q, $timeout, platform, globals, api, uti
// Sync provided bookmarks and set local bookmarks
return $q.all([
api.UpdateBookmarks(encryptedBookmarks, true),
refreshLocalBookmarks(bookmarks)
platform.EventListeners.Disable()
.then(function () {
return refreshLocalBookmarks(bookmarks);
})
.finally(platform.EventListeners.Enable)
])
.then(function (data) {
// Update cached last updated date and return decrypted bookmarks
Expand Down Expand Up @@ -1623,6 +1653,7 @@ xBrowserSync.App.Bookmarks = function ($q, $timeout, platform, globals, api, uti
ConvertLocalBookmarkToXBookmark: convertLocalBookmarkToXBookmark,
DisableSync: disableSync,
Each: eachBookmark,
EnableSync: enableSync,
Export: exportBookmarks,
FindBookmarkById: findBookmarkById,
FindCurrentUrlInBookmarks: findCurrentUrlInBookmarks,
Expand Down
4 changes: 2 additions & 2 deletions js/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ xBrowserSync.App = xBrowserSync.App || {};
* Description: Defines global properties used across all platforms.
* ------------------------------------------------------------------------------------ */

xBrowserSync.App.Global = function (platform) {
xBrowserSync.App.Global = function () {
'use strict';

var Global = {
Alarm: {
Name: 'xBrowserSync-alarm',
Period: 1
Period: 15
},
AppVersion: '1.5.2',
Bookmarks: {
Expand Down
1 change: 1 addition & 0 deletions js/platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ xBrowserSync.App.Platform = function () {
ToggleLight: notImplemented
},
SelectFile: notImplemented,
SendMessage: notImplemented,
Sync: {
Await: notImplemented,
Current: notImplemented,
Expand Down
1 change: 0 additions & 1 deletion platform/android/js/app-initialise.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ xBrowserSync.App.Platform.$inject = ['$q'];
xBrowserSync.App.UI.factory('platform', xBrowserSync.App.Platform);

// Add global service
xBrowserSync.App.Global.$inject = ['platform'];
xBrowserSync.App.UI.factory('globals', xBrowserSync.App.Global);

// Add httpInterceptor service
Expand Down
1 change: 0 additions & 1 deletion platform/chrome/js/app-initialise.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ xBrowserSync.App.Platform.$inject = ['$q'];
xBrowserSync.App.UI.factory('platform', xBrowserSync.App.Platform);

// Add global service
xBrowserSync.App.Global.$inject = ['platform'];
xBrowserSync.App.UI.factory('globals', xBrowserSync.App.Global);

// Add httpInterceptor service
Expand Down
Loading

0 comments on commit 024e32e

Please sign in to comment.