workers: refactor.

This commit is contained in:
Christopher Jeffrey 2016-07-27 17:17:17 -07:00
parent 812b73ffbc
commit 6448cea1e7
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -306,9 +306,6 @@ Workers.prototype.scrypt = function scrypt(passwd, salt, N, r, p, len, callback)
*/ */
function Worker(id) { function Worker(id) {
var self = this;
var penv, cp;
if (!(this instanceof Worker)) if (!(this instanceof Worker))
return new Worker(); return new Worker();
@ -319,6 +316,19 @@ function Worker(id) {
this.setMaxListeners(utils.MAX_SAFE_INTEGER); this.setMaxListeners(utils.MAX_SAFE_INTEGER);
this.uid = 0; this.uid = 0;
this.id = id != null ? id : -1; this.id = id != null ? id : -1;
this.child = null;
this._init();
}
/**
* Initialize worker. Bind to events.
* @private
*/
Worker.prototype._init = function _init() {
var self = this;
var penv, cp;
penv = { penv = {
BCOIN_WORKER_NETWORK: bcoin.network.get().type BCOIN_WORKER_NETWORK: bcoin.network.get().type
@ -393,15 +403,15 @@ function Worker(id) {
self.emit('packet', packet); self.emit('packet', packet);
}); });
this._init(); this._bind();
} }
/** /**
* Initialize worker. * Initialize worker. Bind to more events.
* @private * @private
*/ */
Worker.prototype._init = function _init() { Worker.prototype._bind = function _bind() {
var self = this; var self = this;
this.on('worker error', function(err) { this.on('worker error', function(err) {
@ -569,8 +579,6 @@ utils.inherits(Worker, EventEmitter);
*/ */
function Master(options) { function Master(options) {
var self = this;
if (!(this instanceof Master)) if (!(this instanceof Master))
return new Master(); return new Master();
@ -580,6 +588,19 @@ function Master(options) {
this.parser = new Parser(); this.parser = new Parser();
this.options = options || {}; this.options = options || {};
this._init();
}
utils.inherits(Master, EventEmitter);
/**
* Initialize master. Bind events.
* @private
*/
Master.prototype._init = function _init() {
var self = this;
if (utils.isBrowser) { if (utils.isBrowser) {
global.onerror = function onerror(err) { global.onerror = function onerror(err) {
self.emit('error', err); self.emit('error', err);
@ -615,9 +636,7 @@ function Master(options) {
this.parser.on('packet', function(packet) { this.parser.on('packet', function(packet) {
self.emit('packet', packet); self.emit('packet', packet);
}); });
} };
utils.inherits(Master, EventEmitter);
/** /**
* Send data to worker. * Send data to worker.