update files

This commit is contained in:
nuintun 2016-01-14 10:52:52 +08:00
parent 700c728b28
commit 72590c62b6
2 changed files with 40 additions and 32 deletions

View File

@ -4,7 +4,9 @@
'use strict'; 'use strict';
var util = require('util');
var spawn = require('./spawn'); var spawn = require('./spawn');
var EventEmitter = require('events');
var threadKill = require('./thread-kill'); var threadKill = require('./thread-kill');
/** /**
@ -16,10 +18,13 @@ function Emulator(task){
this.task = task; this.task = task;
this.thread = null; this.thread = null;
this.connected = false; this.connected = false;
EventEmitter.call(this);
} }
Emulator.prototype = { util.inherits(Emulator, EventEmitter);
start: function (){
Emulator.prototype.start = function (){
var context = this; var context = this;
this.thread = spawn(this.task.command, { this.thread = spawn(this.task.command, {
@ -27,31 +32,34 @@ Emulator.prototype = {
cwd: this.task.cwd cwd: this.task.cwd
}); });
this.thread.stderr.on('data', function (){ this.thread.stdout.on('data', function (chunk){
context.stop(); context.emit('data', chunk);
}); });
this.thread.on('close', function (){ this.thread.stderr.on('data', function (chunk){
context.stop(); context.stop();
context.emit('error', chunk);
}); });
this.connected = true; this.thread.on('close', function (signal){
return this.thread;
},
stop: function (){
if (this.thread) {
var context = this;
threadKill(this.thread.pid, function (){
['stdin', 'stdout', 'stderr'].forEach(function (stream){ ['stdin', 'stdout', 'stderr'].forEach(function (stream){
context.thread[stream].removeAllListeners(); context.thread[stream].removeAllListeners();
}); });
context.thread = null; context.thread = null;
context.connected = false; context.connected = false;
context.emit('close', signal);
}); });
}
this.connected = true;
return this;
};
Emulator.prototype.stop = function (){
if (this.thread) {
threadKill(this.thread.pid);
} }
}; };

View File

@ -35,22 +35,22 @@ module.exports = {
command: project.command.value command: project.command.value
}); });
var stream = thread.start(); thread.on('data', function (data){
stream.stdout.on('data', function (data){
event.sender.send('emulator', 'data', project, data.toString()); event.sender.send('emulator', 'data', project, data.toString());
}); });
stream.stderr.on('data', function (error){ thread.on('error', function (error){
event.sender.send('emulator', 'error', project, error.toString()); event.sender.send('emulator', 'error', project, error.toString());
}); });
stream.on('close', function (signal){ thread.on('close', function (signal){
event.sender.send('emulator', 'close', project, signal.toString()); event.sender.send('emulator', 'close', project, signal.toString());
delete threads[project.name]; delete threads[project.name];
}); });
thread.start();
threads[project.name] = thread; threads[project.name] = thread;
} else { } else {
thread.stop(); thread.stop();