debounce autosave more, prevent re-entry

see #298
This commit is contained in:
tophf 2017-12-10 06:02:25 +03:00
parent 4d4247f0d8
commit 30fa9671c8

View File

@ -3,6 +3,8 @@
function configDialog(style) { function configDialog(style) {
const AUTOSAVE_DELAY = 500; const AUTOSAVE_DELAY = 500;
let saving = false;
const data = style.usercssData; const data = style.usercssData;
const varsHash = deepCopy(data.vars) || {}; const varsHash = deepCopy(data.vars) || {};
const varNames = Object.keys(varsHash); const varNames = Object.keys(varsHash);
@ -88,7 +90,7 @@ function configDialog(style) {
if (va) { if (va) {
va.dirty = varsInitial[va.name] !== (isDefault(va) ? va.default : va.value); va.dirty = varsInitial[va.name] !== (isDefault(va) ? va.default : va.value);
if (prefs.get('config.autosave') && !justSaved) { if (prefs.get('config.autosave') && !justSaved) {
debounce(save, 0, {anyChangeIsDirty: true}); debounce(save, 100, {anyChangeIsDirty: true});
return; return;
} }
renderValueState(va); renderValueState(va);
@ -106,6 +108,10 @@ function configDialog(style) {
} }
function save({anyChangeIsDirty = false} = {}) { function save({anyChangeIsDirty = false} = {}) {
if (saving) {
debounce(save, 0, ...arguments);
return;
}
if (!vars.length || if (!vars.length ||
!vars.some(va => va.dirty || anyChangeIsDirty && va.value !== va.savedValue)) { !vars.some(va => va.dirty || anyChangeIsDirty && va.value !== va.savedValue)) {
return; return;
@ -157,6 +163,7 @@ function configDialog(style) {
if (!numValid) { if (!numValid) {
return; return;
} }
saving = true;
return BG.usercssHelper.save(style) return BG.usercssHelper.save(style)
.then(saved => { .then(saved => {
varsInitial = getInitialValues(deepCopy(saved.usercssData.vars)); varsInitial = getInitialValues(deepCopy(saved.usercssData.vars));
@ -169,6 +176,9 @@ function configDialog(style) {
const el = $('.config-error', messageBox.element) || const el = $('.config-error', messageBox.element) ||
$('#message-box-buttons').insertAdjacentElement('afterbegin', $create('.config-error')); $('#message-box-buttons').insertAdjacentElement('afterbegin', $create('.config-error'));
el.textContent = el.title = Array.isArray(errors) ? errors.join('\n') : errors; el.textContent = el.title = Array.isArray(errors) ? errors.join('\n') : errors;
})
.then(() => {
saving = false;
}); });
} }