Reorganise some shard logic

This commit is contained in:
Schuyler Cebulskie
2016-09-27 17:22:56 -04:00
parent 0ee3d6bb8a
commit 49d905a7b1
4 changed files with 30 additions and 16 deletions

View File

@@ -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

View File

@@ -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;

View File

@@ -10,6 +10,7 @@ class ShardClientUtil {
*/
constructor(client) {
this.client = client;
process.on('message', this._handleMessage.bind(this));
}
/**

View File

@@ -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<number, Shard>}
*/
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) }));
}
});
}
/**