From db427b6fe8e8d8f812884f346dacd6471566eda8 Mon Sep 17 00:00:00 2001 From: Nate Wang Date: Sun, 13 May 2018 14:56:42 +0800 Subject: [PATCH] Use _.throttle for socket output. --- .../rekit-studio/middleware/taskRunner.js | 29 +++++++++++++++---- packages/rekit-studio/package.json | 2 +- yarn.lock | 9 +++++- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/packages/rekit-studio/middleware/taskRunner.js b/packages/rekit-studio/middleware/taskRunner.js index a913839c..3f60f0a2 100644 --- a/packages/rekit-studio/middleware/taskRunner.js +++ b/packages/rekit-studio/middleware/taskRunner.js @@ -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), @@ -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(); }); diff --git a/packages/rekit-studio/package.json b/packages/rekit-studio/package.json index 30ba7ebc..c8cb907c 100644 --- a/packages/rekit-studio/package.json +++ b/packages/rekit-studio/package.json @@ -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", diff --git a/yarn.lock b/yarn.lock index eb314554..80855874 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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: