From 12e9d713c50039e484a1c375163d93f665009ffe Mon Sep 17 00:00:00 2001 From: nuintun Date: Fri, 4 Dec 2015 17:58:01 +0800 Subject: [PATCH] update files --- bin/child-thread.js | 73 ++++++++++++++++++++++++++------------------- bin/emulator.js | 8 ++++- bin/thread.js | 17 +++++++---- 3 files changed, 62 insertions(+), 36 deletions(-) diff --git a/bin/child-thread.js b/bin/child-thread.js index e9e28db..ebc00ee 100644 --- a/bin/child-thread.js +++ b/bin/child-thread.js @@ -9,43 +9,56 @@ var Emulator = require('./emulator'); var emulator; // thread -process.on('message', function (project){ - var stream; +process.on('message', function (message){ + if (message.action === 'stop') { + if (emulator) { + emulator.stop(); + } - if (emulator) { - emulator.stop(); + process.exit(); + + return; } - emulator = new Emulator({ - cwd: project.path, - command: project.command.value - }); + if (message.action === 'start') { + var stream; + var project = message.project; - stream = emulator.start(); + if (emulator) { + emulator.stop(); + } - stream.stdout.on('data', function (data){ - process.send({ - event: 'data', - project: project, - data: data.toString() + emulator = new Emulator({ + cwd: project.path, + command: project.command.value }); - }); - stream.stderr.on('data', function (error){ - emulator.stop(); - process.send({ - event: 'error', - project: project, - data: error.toString() - }); - }); + stream = emulator.start(); - stream.on('close', function (signal){ - emulator.stop(); - process.send({ - event: 'close', - project: project, - data: signal.toString() + stream.stdout.on('data', function (data){ + process.send({ + event: 'data', + project: project, + data: data.toString() + }); }); - }); + + stream.stderr.on('data', function (error){ + emulator.stop(); + process.send({ + event: 'error', + project: project, + data: error.toString() + }); + }); + + stream.on('close', function (signal){ + emulator.stop(); + process.send({ + event: 'close', + project: project, + data: signal.toString() + }); + }); + } }); diff --git a/bin/emulator.js b/bin/emulator.js index 41c9402..65702e9 100644 --- a/bin/emulator.js +++ b/bin/emulator.js @@ -26,7 +26,13 @@ Emulator.prototype = { }, stop: function (){ if (this.thread) { - this.thread.kill('SIGTERM'); + this.thread.kill('SIGKILL'); + + ['stdin', 'stdout', 'stderr'].forEach(function (stream){ + this.thread[stream].removeAllListeners(); + }, this); + + this.thread = null; } }, exec: function (){ diff --git a/bin/thread.js b/bin/thread.js index e32b04f..6831613 100644 --- a/bin/thread.js +++ b/bin/thread.js @@ -19,7 +19,9 @@ function killThread(name){ var thread = threads[name]; if (thread && thread.connected) { - thread.kill('SIGTERM'); + thread.send({ + action: 'stop' + }); delete threads[name]; } @@ -28,8 +30,7 @@ function killThread(name){ module.exports = { start: function (){ ipc.on('emulator', function (event, project, action){ - var key = project.name + '-' + project.command.name; - var thread = threads[key]; + var thread = threads[project.name]; switch (action) { case 'start': @@ -58,13 +59,19 @@ module.exports = { delete project.env; - thread.send(project); + thread.send({ + action: 'start', + project: project + }); threads[project.name] = thread; } else { delete project.env; - thread.send(project); + thread.send({ + action: 'start', + project: project + }); } break; case 'stop':