off-load mozParser to a worker
This commit is contained in:
parent
9c3229717f
commit
f5516db8f4
|
@ -144,7 +144,7 @@ var mozParser = (() => {
|
||||||
return {
|
return {
|
||||||
// Parse mozilla-format userstyle into sections
|
// Parse mozilla-format userstyle into sections
|
||||||
parse(text) {
|
parse(text) {
|
||||||
return loadScript('/vendor-overwrites/csslint/csslint-worker.js')
|
return Promise.resolve(self.CSSLint || loadScript('/vendor-overwrites/csslint/csslint-worker.js'))
|
||||||
.then(() => parseMozFormat(text));
|
.then(() => parseMozFormat(text));
|
||||||
},
|
},
|
||||||
format(style) {
|
format(style) {
|
||||||
|
|
|
@ -102,6 +102,8 @@ var usercss = (() => {
|
||||||
const RX_STRING_BACKTICK = /(`(?:\\`|[\s\S])*?`)\s*/y;
|
const RX_STRING_BACKTICK = /(`(?:\\`|[\s\S])*?`)\s*/y;
|
||||||
const RX_STRING_QUOTED = /((['"])(?:\\\2|[^\n])*?\2|\w+)\s*/y;
|
const RX_STRING_QUOTED = /((['"])(?:\\\2|[^\n])*?\2|\w+)\s*/y;
|
||||||
|
|
||||||
|
const worker = {};
|
||||||
|
|
||||||
function getMetaSource(source) {
|
function getMetaSource(source) {
|
||||||
const commentRe = /\/\*[\s\S]*?\*\//g;
|
const commentRe = /\/\*[\s\S]*?\*\//g;
|
||||||
const metaRe = /==userstyle==[\s\S]*?==\/userstyle==/i;
|
const metaRe = /==userstyle==[\s\S]*?==\/userstyle==/i;
|
||||||
|
@ -474,25 +476,11 @@ var usercss = (() => {
|
||||||
|
|
||||||
const sVars = simpleVars(vars);
|
const sVars = simpleVars(vars);
|
||||||
|
|
||||||
return Promise.resolve().then(() => {
|
return Promise.resolve(builder.preprocess && builder.preprocess(sourceCode, sVars) || sourceCode)
|
||||||
// preprocess
|
.then(mozStyle => invokeWorker({action: 'parse', code: mozStyle}))
|
||||||
if (builder.preprocess) {
|
.then(sections => (style.sections = sections))
|
||||||
return builder.preprocess(sourceCode, sVars);
|
.then(() => builder.postprocess && builder.postprocess(style.sections, sVars))
|
||||||
}
|
.then(() => style);
|
||||||
return sourceCode;
|
|
||||||
}).then(mozStyle =>
|
|
||||||
// moz-parser
|
|
||||||
loadScript('/js/moz-parser.js').then(() =>
|
|
||||||
mozParser.parse(mozStyle).then(sections => {
|
|
||||||
style.sections = sections;
|
|
||||||
})
|
|
||||||
)
|
|
||||||
).then(() => {
|
|
||||||
// postprocess
|
|
||||||
if (builder.postprocess) {
|
|
||||||
return builder.postprocess(style.sections, sVars);
|
|
||||||
}
|
|
||||||
}).then(() => style);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function simpleVars(vars) {
|
function simpleVars(vars) {
|
||||||
|
@ -579,5 +567,24 @@ var usercss = (() => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function invokeWorker(message) {
|
||||||
|
if (!worker.queue) {
|
||||||
|
worker.instance = new Worker('/vendor-overwrites/csslint/csslint-worker.js');
|
||||||
|
worker.queue = [];
|
||||||
|
worker.instance.onmessage = ({data}) => {
|
||||||
|
worker.queue.shift().resolve(data);
|
||||||
|
if (worker.queue.length) {
|
||||||
|
worker.instance.postMessage(worker.queue[0].message);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return new Promise(resolve => {
|
||||||
|
worker.queue.push({message, resolve});
|
||||||
|
if (worker.queue.length === 1) {
|
||||||
|
worker.instance.postMessage(message);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return {buildMeta, buildCode, assignVars};
|
return {buildMeta, buildCode, assignVars};
|
||||||
})();
|
})();
|
||||||
|
|
|
@ -10967,5 +10967,12 @@ self.onmessage = ({data: {action = 'run', code, config}}) => {
|
||||||
return m;
|
return m;
|
||||||
}));
|
}));
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
case 'parse':
|
||||||
|
if (!self.mozParser) {
|
||||||
|
self.importScripts('/js/moz-parser.js');
|
||||||
|
}
|
||||||
|
mozParser.parse(code)
|
||||||
|
.then(sections => self.postMessage(sections));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue
Block a user