From 49d905a7b12a790d7d79fdeda5d15113e3415913 Mon Sep 17 00:00:00 2001 From: Schuyler Cebulskie Date: Tue, 27 Sep 2016 17:22:56 -0400 Subject: [PATCH] Reorganise some shard logic --- src/client/Client.js | 1 - src/sharding/Shard.js | 30 +++++++++++++++++++++++++++++- src/sharding/ShardClientUtil.js | 1 + src/sharding/ShardingManager.js | 14 -------------- 4 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/client/Client.js b/src/client/Client.js index 84fa2cadf..a9a44b6de 100644 --- a/src/client/Client.js +++ b/src/client/Client.js @@ -91,7 +91,6 @@ class Client extends EventEmitter { * @type {?ShardUtil} */ this.shard = process.send ? ShardClientUtil.singleton(this) : null; - if (this.shard) process.on('message', this.shard._handleMessage.bind(this.shard)); /** * A Collection of the Client's stored users diff --git a/src/sharding/Shard.js b/src/sharding/Shard.js index 05d9dabfa..1c454d048 100644 --- a/src/sharding/Shard.js +++ b/src/sharding/Shard.js @@ -1,6 +1,7 @@ const childProcess = require('child_process'); const path = require('path'); const makeError = require('../util/MakeError'); +const makePlainError = require('../util/MakePlainError'); /** * Represents a Shard spawned by the ShardingManager. @@ -33,7 +34,7 @@ class Shard { SHARD_COUNT: this.manager.totalShards, }, }); - this.process.on('message', message => { this.manager.emit('message', this, message); }); + this.process.on('message', this._handleMessage.bind(this)); this.process.once('exit', () => { if (this.manager.respawn) this.manager.createShard(this.id); }); @@ -115,6 +116,33 @@ class Shard { this._evals.set(script, promise); return promise; } + + /** + * Handles an IPC message + * @param {*} message Message received + * @private + */ + _handleMessage(message) { + if (message) { + // Shard is requesting a property fetch + if (message._sFetchProp) { + this.manager.fetchClientValues(message._sFetchProp) + .then(results => this.send({ _sFetchProp: message._sFetchProp, _result: results })) + .catch(err => this.send({ _sFetchProp: message._sFetchProp, _error: makePlainError(err) })); + return; + } + + // Shard is requesting an eval broadcast + if (message._sEval) { + this.manager.broadcastEval(message._sEval) + .then(results => this.send({ _sEval: message._sEval, _result: results })) + .catch(err => this.send({ _sEval: message._sEval, _error: makePlainError(err) })); + return; + } + } + + this.manager.emit('message', this, message); + } } module.exports = Shard; diff --git a/src/sharding/ShardClientUtil.js b/src/sharding/ShardClientUtil.js index 37a58a6f7..5485cfa88 100644 --- a/src/sharding/ShardClientUtil.js +++ b/src/sharding/ShardClientUtil.js @@ -10,6 +10,7 @@ class ShardClientUtil { */ constructor(client) { this.client = client; + process.on('message', this._handleMessage.bind(this)); } /** diff --git a/src/sharding/ShardingManager.js b/src/sharding/ShardingManager.js index 66f5d0d4d..da4df2d56 100644 --- a/src/sharding/ShardingManager.js +++ b/src/sharding/ShardingManager.js @@ -4,7 +4,6 @@ const EventEmitter = require('events').EventEmitter; const Shard = require('./Shard'); const Collection = require('../util/Collection'); -const makePlainError = require('../util/MakePlainError'); /** * This is a utility class that can be used to help you spawn shards of your Client. Each shard is completely separate @@ -53,19 +52,6 @@ class ShardingManager extends EventEmitter { * @type {Collection} */ this.shards = new Collection(); - - this.on('message', (shard, message) => { - if (!message) return; - if (message._sFetchProp) { - this.fetchClientValues(message._sFetchProp) - .then(results => shard.send({ _sFetchProp: message._sFetchProp, _result: results })) - .catch(err => shard.send({ _sFetchProp: message._sFetchProp, _error: makePlainError(err) })); - } else if (message._sEval) { - this.broadcastEval(message._sEval) - .then(results => shard.send({ _sEval: message._sEval, _result: results })) - .catch(err => shard.send({ _sEval: message._sEval, _error: makePlainError(err) })); - } - }); } /**