Fix: handle prefs change

This commit is contained in:
eight 2019-10-01 19:39:03 +08:00
parent 938757b782
commit f6567eef12
2 changed files with 35 additions and 43 deletions

View File

@ -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,15 +71,13 @@ 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())
.then(() => {
currentDrive = getDrive(name); currentDrive = getDrive(name);
ctrl.use(currentDrive); ctrl.use(currentDrive);
prefs.set('sync.enabled', name);
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)) {
@ -83,7 +85,6 @@ const sync = (() => {
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');
}); });
} }
})(); })();

View File

@ -81,15 +81,12 @@ 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');
cloud.value = name;
state = name === 'none' ? 'disconnected' : 'connected';
updateButtons(); updateButtons();
}); });
@ -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();
}); });
} }