Fix: update progress

This commit is contained in:
eight 2017-09-12 01:23:32 +08:00
parent a15493bfb9
commit f305719db3
5 changed files with 41 additions and 27 deletions

View File

@ -19,10 +19,18 @@ var updater = {
SAME_VERSION: 'up-to-date: version is unchanged', SAME_VERSION: 'up-to-date: version is unchanged',
ERROR_MD5: 'error: MD5 is invalid', ERROR_MD5: 'error: MD5 is invalid',
ERROR_JSON: 'error: JSON is invalid', ERROR_JSON: 'error: JSON is invalid',
ERROR_VERSION: 'error: version is invalid', ERROR_VERSION: 'error: version is older than installed style',
lastUpdateTime: parseInt(localStorage.lastUpdateTime) || Date.now(), lastUpdateTime: parseInt(localStorage.lastUpdateTime) || Date.now(),
isSame(code) {
return code === updater.SAME_MD5 || code === updater.SAME_CODE || code === updater.SAME_VERSION;
},
isEdited(code) {
return code === updater.EDITED || code === updater.MAYBE_EDITED;
},
checkAllStyles({observer = () => {}, save = true, ignoreDigest} = {}) { checkAllStyles({observer = () => {}, save = true, ignoreDigest} = {}) {
updater.resetInterval(); updater.resetInterval();
updater.checkAllStyles.running = true; updater.checkAllStyles.running = true;
@ -72,11 +80,12 @@ var updater = {
}); });
function checkIfEdited(digest) { function checkIfEdited(digest) {
if (style.usercss) { if (ignoreDigest) {
// FIXME: remove this after we can calculate digest from style.source
return; return;
} }
if (!ignoreDigest && style.originalDigest && style.originalDigest !== digest) { if (style.usercss && style.edited) {
return Promise.reject(updater.EDITED);
} else if (style.originalDigest && style.originalDigest !== digest) {
return Promise.reject(updater.EDITED); return Promise.reject(updater.EDITED);
} }
} }
@ -97,34 +106,33 @@ var updater = {
function maybeUpdateUsercss() { function maybeUpdateUsercss() {
return download(style.updateUrl).then(text => { return download(style.updateUrl).then(text => {
const json = usercss.buildMeta(text); const json = usercss.buildMeta(text);
if (!json.version) { // re-install is invalid in a soft upgrade
if (semverCompare(style.version, json.version) === 0 && !ignoreDigest) {
return Promise.reject(updater.SAME_VERSION);
}
// downgrade is always invalid
if (semverCompare(style.version, json.version) > 0) {
return Promise.reject(updater.ERROR_VERSION); return Promise.reject(updater.ERROR_VERSION);
} }
if (style.version) {
if (semverCompare(style.version, json.version) === 0) {
return Promise.reject(updater.SAME_VERSION);
}
if (semverCompare(style.version, json.version) > 0) {
return Promise.reject(updater.ERROR_VERSION);
}
}
json.id = style.id;
return json; return json;
}); });
} }
function maybeSave(json) { function maybeSave(json) {
if (!styleJSONseemsValid(json)) {
return Promise.reject(updater.ERROR_JSON);
}
json.id = style.id; json.id = style.id;
if (styleSectionsEqual(json, style)) { // no need to compare section code for usercss, they are built dynamically
// JSONs may have different order of items even if sections are effectively equal if (!json.usercss) {
// so we'll update the digest anyway if (!styleJSONseemsValid(json)) {
saveStyle(Object.assign(json, {reason: 'update-digest'})); return Promise.reject(updater.ERROR_JSON);
return Promise.reject(updater.SAME_CODE); }
} else if (!style.originalDigest && !ignoreDigest) { if (styleSectionsEqual(json, style)) {
return Promise.reject(updater.MAYBE_EDITED); // JSONs may have different order of items even if sections are effectively equal
// so we'll update the digest anyway
saveStyle(Object.assign(json, {reason: 'update-digest'}));
return Promise.reject(updater.SAME_CODE);
} else if (!style.originalDigest && !ignoreDigest) {
return Promise.reject(updater.MAYBE_EDITED);
}
} }
return !save ? json : return !save ? json :
saveStyle(Object.assign(json, { saveStyle(Object.assign(json, {

View File

@ -111,6 +111,7 @@ function createSourceEditor(style) {
reason: 'editSave', reason: 'editSave',
id: style.id, id: style.id,
enabled: style.enabled, enabled: style.enabled,
edited: dirty.has('source'),
source: style.source source: style.source
}; };
return onBackgroundReady().then(() => BG.saveUsercss(req)) return onBackgroundReady().then(() => BG.saveUsercss(req))

View File

@ -86,5 +86,9 @@ function dirtyReporter() {
}; };
} }
return wrap({add, remove, modify, clear, isDirty, onChange}); function has(key) {
return dirty.has(key);
}
return wrap({add, remove, modify, clear, isDirty, onChange, has});
} }

View File

@ -173,6 +173,7 @@ var usercss = (function () {
usercss: true, usercss: true,
version: null, version: null,
source: source, source: source,
edited: false,
enabled: true, enabled: true,
sections: [], sections: [],
vars: {}, vars: {},

View File

@ -114,8 +114,8 @@ function reportUpdateState(state, style, details) {
if (entry.classList.contains('can-update')) { if (entry.classList.contains('can-update')) {
break; break;
} }
const same = details === BG.updater.SAME_MD5 || details === BG.updater.SAME_CODE; const same = BG.updater.isSame(details);
const edited = details === BG.updater.EDITED || details === BG.updater.MAYBE_EDITED; const edited = BG.updater.isEdited(details);
entry.dataset.details = details; entry.dataset.details = details;
if (!details) { if (!details) {
details = t('updateCheckFailServerUnreachable'); details = t('updateCheckFailServerUnreachable');