Change: only modify pref if the initialization success?
This commit is contained in:
parent
809b6ff2fb
commit
6ef85820f1
|
@ -11,7 +11,8 @@ const sync = (() => {
|
||||||
state: 'disconnected',
|
state: 'disconnected',
|
||||||
syncing: false,
|
syncing: false,
|
||||||
progress: null,
|
progress: null,
|
||||||
currentDriveName: null
|
currentDriveName: null,
|
||||||
|
errorMessage: null
|
||||||
};
|
};
|
||||||
let currentDrive;
|
let currentDrive;
|
||||||
const ctrl = dbToCloud.dbToCloud({
|
const ctrl = dbToCloud.dbToCloud({
|
||||||
|
@ -52,9 +53,7 @@ const sync = (() => {
|
||||||
|
|
||||||
chrome.alarms.onAlarm.addListener(info => {
|
chrome.alarms.onAlarm.addListener(info => {
|
||||||
if (info.name === 'syncNow') {
|
if (info.name === 'syncNow') {
|
||||||
ctrl.syncNow()
|
syncNow().catch(console.error);
|
||||||
.catch(handle401Error)
|
|
||||||
.catch(console.error);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -103,18 +102,25 @@ const sync = (() => {
|
||||||
function withFinally(p, cleanup) {
|
function withFinally(p, cleanup) {
|
||||||
return p.then(
|
return p.then(
|
||||||
result => {
|
result => {
|
||||||
cleanup();
|
cleanup(undefined, result);
|
||||||
return result;
|
return result;
|
||||||
},
|
},
|
||||||
err => {
|
err => {
|
||||||
cleanup();
|
cleanup(err);
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function syncNow() {
|
function syncNow() {
|
||||||
return ctrl.syncNow().catch(handle401Error);
|
return withFinally(
|
||||||
|
(ctrl.isInit() ? ctrl.syncNow() : ctrl.start())
|
||||||
|
.catch(handle401Error),
|
||||||
|
err => {
|
||||||
|
status.errorMessage = err ? err.message : null;
|
||||||
|
emitStatusChange();
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function handle401Error(err) {
|
function handle401Error(err) {
|
||||||
|
@ -137,26 +143,28 @@ const sync = (() => {
|
||||||
}
|
}
|
||||||
currentDrive = getDrive(name);
|
currentDrive = getDrive(name);
|
||||||
ctrl.use(currentDrive);
|
ctrl.use(currentDrive);
|
||||||
prefs.set('sync.enabled', name);
|
|
||||||
status.state = 'connecting';
|
status.state = 'connecting';
|
||||||
status.currentDriveName = currentDrive.name;
|
status.currentDriveName = currentDrive.name;
|
||||||
emitStatusChange();
|
emitStatusChange();
|
||||||
return withFinally(
|
return tokenManager.getToken(name)
|
||||||
ctrl.start()
|
.catch(err => {
|
||||||
.catch(err => {
|
if (/Authorization page could not be loaded/i.test(err.message)) {
|
||||||
if (/Authorization page could not be loaded/i.test(err.message)) {
|
// FIXME: Chrome always fail at the first login so we try again
|
||||||
// FIXME: Chrome always fail at the first login so we try again
|
return tokenManager.getToken(name);
|
||||||
return ctrl.syncNow();
|
}
|
||||||
}
|
throw err;
|
||||||
throw err;
|
})
|
||||||
})
|
.catch(handle401Error)
|
||||||
.catch(handle401Error),
|
.then(() => syncNow())
|
||||||
() => {
|
.then(() => {
|
||||||
|
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) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user