Don't stop the sync if the first sync is not triggered by the user

This commit is contained in:
eight 2019-10-12 18:15:15 +08:00
parent 6ef85820f1
commit 785dcfb619

View File

@ -137,7 +137,7 @@ const sync = (() => {
msg.broadcastExtension({method: 'syncStatusUpdate', status}); msg.broadcastExtension({method: 'syncStatusUpdate', status});
} }
function start(name) { function start(name, fromPref = false) {
if (currentDrive) { if (currentDrive) {
return Promise.resolve(); return Promise.resolve();
} }
@ -146,25 +146,29 @@ const sync = (() => {
status.state = 'connecting'; status.state = 'connecting';
status.currentDriveName = currentDrive.name; status.currentDriveName = currentDrive.name;
emitStatusChange(); emitStatusChange();
return tokenManager.getToken(name) return withFinally(
.catch(err => { tokenManager.getToken(name)
if (/Authorization page could not be loaded/i.test(err.message)) { .catch(err => {
// FIXME: Chrome always fail at the first login so we try again if (/Authorization page could not be loaded/i.test(err.message)) {
return tokenManager.getToken(name); // FIXME: Chrome always fails at the first login so we try again
return tokenManager.getToken(name);
}
throw err;
})
.catch(handle401Error)
.then(() => syncNow()),
err => {
// FIXME: should we move this logic to options.js?
if (err && !fromPref) {
console.error(err);
return stop();
} }
throw err;
})
.catch(handle401Error)
.then(() => syncNow())
.then(() => {
prefs.set('sync.enabled', name); prefs.set('sync.enabled', name);
chrome.alarms.create('syncNow', {periodInMinutes: SYNC_INTERVAL}); chrome.alarms.create('syncNow', {periodInMinutes: SYNC_INTERVAL});
status.state = 'connected'; status.state = 'connected';
emitStatusChange(); emitStatusChange();
}, err => { }
console.error(err); );
return stop();
});
} }
function getDrive(name) { function getDrive(name) {
@ -173,8 +177,6 @@ const sync = (() => {
getAccessToken: () => tokenManager.getToken(name) getAccessToken: () => tokenManager.getToken(name)
}); });
} }
throw new Error(`unknown cloud name: ${name}`); throw new Error(`unknown cloud name: ${name}`);
} }