Fix: handle prefs change
This commit is contained in:
parent
938757b782
commit
f6567eef12
|
@ -37,9 +37,13 @@ const sync = (() => {
|
|||
}
|
||||
});
|
||||
|
||||
prefs.initializing
|
||||
.then(start)
|
||||
.catch(console.error);
|
||||
prefs.subscribe(['sync.enabled'], (key, value) => {
|
||||
if (value === 'none') {
|
||||
stop().catch(console.error);
|
||||
} else {
|
||||
start(value).catch(console.error);
|
||||
}
|
||||
});
|
||||
|
||||
chrome.alarms.onAlarm.addListener(info => {
|
||||
if (info.name === 'syncNow') {
|
||||
|
@ -67,23 +71,20 @@ const sync = (() => {
|
|||
throw err;
|
||||
}
|
||||
|
||||
function start() {
|
||||
const name = prefs.get('sync.enabled');
|
||||
if (name === 'none') {
|
||||
function start(name) {
|
||||
if (currentDrive) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
return (currentDrive ? stop() : Promise.resolve())
|
||||
.then(() => {
|
||||
currentDrive = getDrive(name);
|
||||
ctrl.use(currentDrive);
|
||||
return ctrl.start()
|
||||
.catch(err => {
|
||||
if (/Authorization page could not be loaded/i.test(err.message)) {
|
||||
// FIXME: Chrome always fail at the first login so we try again
|
||||
return ctrl.syncNow();
|
||||
}
|
||||
throw err;
|
||||
});
|
||||
currentDrive = getDrive(name);
|
||||
ctrl.use(currentDrive);
|
||||
prefs.set('sync.enabled', name);
|
||||
return ctrl.start()
|
||||
.catch(err => {
|
||||
if (/Authorization page could not be loaded/i.test(err.message)) {
|
||||
// FIXME: Chrome always fail at the first login so we try again
|
||||
return ctrl.syncNow();
|
||||
}
|
||||
throw err;
|
||||
})
|
||||
.catch(handle401Error)
|
||||
.then(() => {
|
||||
|
@ -102,15 +103,16 @@ const sync = (() => {
|
|||
}
|
||||
|
||||
function stop() {
|
||||
chrome.alarms.clear('syncNow');
|
||||
if (!currentDrive) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
chrome.alarms.clear('syncNow');
|
||||
return ctrl.stop()
|
||||
.then(() => tokenManager.revokeToken(currentDrive.name))
|
||||
.then(() => chromeLocal.remove(`sync/state/${currentDrive.name}`))
|
||||
.then(() => {
|
||||
currentDrive = null;
|
||||
prefs.set('sync.enabled', 'none');
|
||||
});
|
||||
}
|
||||
})();
|
||||
|
|
|
@ -81,17 +81,14 @@ document.onclick = e => {
|
|||
const disconnectButton = document.querySelector('.sync-options .disconnect');
|
||||
const syncButton = document.querySelector('.sync-options .sync-now');
|
||||
|
||||
let state;
|
||||
let connected = false;
|
||||
let syncing = false;
|
||||
|
||||
// init button state
|
||||
prefs.initializing
|
||||
.then(() => {
|
||||
const name = prefs.get('sync.enabled');
|
||||
cloud.value = name;
|
||||
state = name === 'none' ? 'disconnected' : 'connected';
|
||||
updateButtons();
|
||||
});
|
||||
prefs.subscribe(['sync.enabled'], (key, value) => {
|
||||
cloud.value = value;
|
||||
connected = value !== 'none';
|
||||
updateButtons();
|
||||
});
|
||||
|
||||
function validClick(e) {
|
||||
return e.button === 0 && !e.ctrl && !e.alt && !e.shift;
|
||||
|
@ -100,25 +97,19 @@ document.onclick = e => {
|
|||
cloud.addEventListener('change', updateButtons);
|
||||
|
||||
function updateButtons() {
|
||||
cloud.disabled = state !== 'disconnected';
|
||||
connectButton.disabled = state !== 'disconnected' || cloud.value === 'none';
|
||||
disconnectButton.disabled = state !== 'connected';
|
||||
syncButton.disabled = state !== 'connected' || syncing;
|
||||
cloud.disabled = connected;
|
||||
connectButton.disabled = connected || cloud.value === 'none';
|
||||
disconnectButton.disabled = !connected || syncing;
|
||||
syncButton.disabled = !connected || syncing;
|
||||
}
|
||||
|
||||
connectButton.addEventListener('click', e => {
|
||||
if (validClick(e)) {
|
||||
if (cloud.value === 'none') {
|
||||
return;
|
||||
}
|
||||
state = 'connecting';
|
||||
syncing = true;
|
||||
updateButtons();
|
||||
prefs.set('sync.enabled', cloud.value)
|
||||
.then(() => API.syncStart())
|
||||
API.syncStart(cloud.value)
|
||||
.catch(console.error)
|
||||
.then(() => {
|
||||
state = 'connected';
|
||||
syncing = false;
|
||||
updateButtons();
|
||||
});
|
||||
|
@ -127,13 +118,12 @@ document.onclick = e => {
|
|||
|
||||
disconnectButton.addEventListener('click', e => {
|
||||
if (validClick(e)) {
|
||||
state = 'disconnecting';
|
||||
syncing = true;
|
||||
updateButtons();
|
||||
prefs.set('sync.enabled', 'none')
|
||||
.then(() => API.syncStop())
|
||||
API.syncStop()
|
||||
.catch(console.error)
|
||||
.then(() => {
|
||||
state = 'disconnected';
|
||||
syncing = false;
|
||||
updateButtons();
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user