Fix: rewrite filterUsercss

This commit is contained in:
eight 2017-09-12 18:49:17 +08:00
parent 33200dbde4
commit a395df18f9

View File

@ -372,8 +372,15 @@ function filterStylesInternal({
// Parse the source and find the duplication
// {id: int, style: object, source: string, checkDup: boolean}
function filterUsercss(req) {
return Promise.resolve().then(() => {
let style;
let pendingBuild;
return buildMeta()
.then(buildSection)
.then(decide)
.catch(err => ({status: 'error', error: err.message}));
function buildMeta() {
return new Promise(resolve => {
if (req.source) {
style = usercss.buildMeta(req.source);
} else {
@ -382,18 +389,26 @@ function filterUsercss(req) {
if (!style.id && req.id) {
style.id = req.id;
}
let pending;
if (!style.sections || !style.sections.length) {
pending = usercss.buildCode(style);
} else {
pending = Promise.resolve(style);
resolve();
});
}
function buildSection() {
if (!style.sections || !style.sections.length) {
pendingBuild = usercss.buildCode(style);
} else {
pendingBuild = Promise.resolve(style);
}
}
function decide() {
// decide result
if (!style.id && req.checkDup) {
return Promise.all([pending, findDupUsercss(style)])
return Promise.all([pendingBuild, findDupUsercss(style)])
.then(([, dup]) => ({status: 'success', style, dup}));
}
return pending.then(() => ({status: 'success', style}));
}).catch(err => ({status: 'error', error: String(err)}));
return pendingBuild.then(() => ({status: 'success', style}));
}
}
function saveUsercss(style) {