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