feature(user-menu) speed up user menu parsing with help of worker threads

This commit is contained in:
coderaiser 2019-08-16 12:13:50 +03:00
parent 9301506d82
commit 36b8e5903d
4 changed files with 23 additions and 9 deletions

View file

@ -154,6 +154,7 @@
"socket.io-client": "^2.1.1",
"squad": "^3.0.0",
"table": "^5.0.2",
"thread-it": "^1.0.1",
"try-catch": "^2.0.0",
"try-to-catch": "^1.0.2",
"tryrequire": "^2.0.1",

View file

@ -10,6 +10,7 @@ const squad = require('squad');
const tryToCatch = require('try-to-catch');
const wraptile = require('wraptile');
const compression = require('compression');
const threadIt = require('thread-it');
const two = currify((f, a, b) => f(a, b));
const exit = require(DIR_SERVER + 'exit');
@ -21,6 +22,7 @@ const promisifySelf = squad(promisify, bind);
const shutdown = wraptile(async (promises) => {
console.log('closing cloudcmd...');
await Promise.all(promises);
threadIt.terminate();
process.exit(0);
});

View file

@ -6,12 +6,17 @@ const fs = require('fs');
const {join} = require('path');
const {promisify} = require('util');
const tryCatch = require('try-catch');
const tryToCatch = require('try-to-catch');
const currify = require('currify');
const findUp = require('find-up');
const putout = require('putout');
const {codeframe} = putout;
const threadIt = require('thread-it');
const {codeframe} = require('putout');
const putout = threadIt('putout');
threadIt.init();
// warm up worker cache
transpile('');
const readFile = promisify(fs.readFile);
@ -62,12 +67,7 @@ async function onGET({req, res, menuName}) {
if (e)
return sendDefaultMenu(res);
const [parseError, result] = tryCatch(putout, source, {
plugins: [
'convert-esm-to-commonjs',
'strict-mode',
],
});
const [parseError, result] = await transpile(source);
if (parseError)
return res
@ -92,3 +92,11 @@ function sendDefaultMenu(res) {
});
}
function transpile(source) {
return tryToCatch(putout, source, {
plugins: [
'convert-esm-to-commonjs',
'strict-mode',
],
});
}

View file

@ -5,6 +5,7 @@ const {join} = require('path');
const test = require('supertape');
const serveOnce = require('serve-once');
const threadIt = require('thread-it');
const userMenu = require('./user-menu');
const {request} = serveOnce(userMenu);
@ -21,6 +22,8 @@ test('cloudcmd: user menu', async (t) => {
options,
});
threadIt.terminate();
t.equal(userMenuFile, body, 'should equal');
t.end();
});