mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
Reorganise some shard logic
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -10,6 +10,7 @@ class ShardClientUtil {
|
||||
*/
|
||||
constructor(client) {
|
||||
this.client = client;
|
||||
process.on('message', this._handleMessage.bind(this));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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) }));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user