Skip to content

Commit

Permalink
Use _.throttle for socket output.
Browse files Browse the repository at this point in the history
  • Loading branch information
supnate committed May 13, 2018
1 parent 0583cbf commit db427b6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
29 changes: 23 additions & 6 deletions packages/rekit-studio/middleware/taskRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@

const rekitCore = require('rekit-core');
const spawn = require('child_process').spawn;
const _ = require('lodash');

function runTask(io, cmd, type, params) {
console.log('running task: ', cmd);
const prjRoot = rekitCore.utils.getProjectRoot();
const args = cmd.split(' ');

// const doOutput = (data) => {

// }
return new Promise((resolve) => {
const child = spawn(args[0],
args.splice(1),
Expand All @@ -18,23 +23,35 @@ function runTask(io, cmd, type, params) {
}
);
child.stdout.pipe(process.stdout);
const handleOutput = (data) => {
// collect the data
const text = data.toString('utf8').replace(/ /g, ' ').split('\n');

const arr = [];
text.forEach(t => arr.push(t));
io.emit('output', {
const arr = [];
// Emit output at most every second
const emitOutput = _.throttle(() => {
if (arr.length > 0) io.emit('output', {
type,
id: params.id,
params,
output: arr,
});
arr.length = 0;
}, 1000);
const handleOutput = (data) => {
// collect the data
const text = data.toString('utf8').replace(/ /g, ' ').split('\n');
arr.push.apply(arr, text);
emitOutput();
};
child.stdout.on('data', handleOutput);
child.stderr.on('data', handleOutput);

child.on('close', () => {
io.emit('output', {
type,
id: params.id,
params,
output: arr,
});
arr.length = 0;
io.emit('task-finished', { type, params });
resolve();
});
Expand Down
2 changes: 1 addition & 1 deletion packages/rekit-studio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
"estraverse-fb": "^1.3.1",
"express": "^4.15.2",
"express-history-api-fallback": "^2.0.0",
"file-loader": "^1.1.6",
"file-loader": "^1.1.11",
"fuzzysort": "^1.1.1",
"highlight.js": "^9.11.0",
"immutability-helper": "^2.6.4",
Expand Down
9 changes: 8 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4431,6 +4431,13 @@ file-entry-cache@^2.0.0:
flat-cache "^1.2.1"
object-assign "^4.0.1"

file-loader@^1.1.11:
version "1.1.11"
resolved "https://registry.yarnpkg.com/file-loader/-/file-loader-1.1.11.tgz#6fe886449b0f2a936e43cabaac0cdbfb369506f8"
dependencies:
loader-utils "^1.0.2"
schema-utils "^0.4.5"

file-loader@^1.1.6:
version "1.1.6"
resolved "https://registry.yarnpkg.com/file-loader/-/file-loader-1.1.6.tgz#7b9a8f2c58f00a77fddf49e940f7ac978a3ea0e8"
Expand Down Expand Up @@ -9590,7 +9597,7 @@ schema-utils@^0.3.0:
dependencies:
ajv "^5.0.0"

schema-utils@^0.4.0:
schema-utils@^0.4.0, schema-utils@^0.4.5:
version "0.4.5"
resolved "https://registry.yarnpkg.com/schema-utils/-/schema-utils-0.4.5.tgz#21836f0608aac17b78f9e3e24daff14a5ca13a3e"
dependencies:
Expand Down

0 comments on commit db427b6

Please sign in to comment.