mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
Rip out client sharding logic into its own class
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -10,6 +10,7 @@ const WebSocketManager = require('./websocket/WebSocketManager');
|
||||
const ActionsManager = require('./actions/ActionsManager');
|
||||
const Collection = require('../util/Collection');
|
||||
const Presence = require('../structures/Presence').Presence;
|
||||
const ShardUtil = require('../sharding/ShardUtil');
|
||||
|
||||
/**
|
||||
* The starting point for making a Discord Bot.
|
||||
@@ -85,6 +86,13 @@ class Client extends EventEmitter {
|
||||
*/
|
||||
this.voice = new ClientVoiceManager(this);
|
||||
|
||||
/**
|
||||
* The shard helpers for the client (only if the process was spawned as a child, such as from a ShardingManager)
|
||||
* @type {?ShardUtil}
|
||||
*/
|
||||
this.shard = process.send ? ShardUtil.singleton(this) : null;
|
||||
if (this.shard) process.on('message', this.shard._handleMessage.bind(this.shard));
|
||||
|
||||
/**
|
||||
* A Collection of the Client's stored users
|
||||
* @type {Collection<string, User>}
|
||||
@@ -146,24 +154,6 @@ class Client extends EventEmitter {
|
||||
if (this.options.message_sweep_interval > 0) {
|
||||
this.setInterval(this.sweepMessages.bind(this), this.options.message_sweep_interval * 1000);
|
||||
}
|
||||
|
||||
if (process.send) {
|
||||
process.on('message', message => {
|
||||
if (!message) return;
|
||||
if (message._eval) {
|
||||
try {
|
||||
process.send({ _evalResult: eval(message._eval) });
|
||||
} catch (err) {
|
||||
process.send({ _evalError: err });
|
||||
}
|
||||
} else if (message._fetchProp) {
|
||||
const props = message._fetchProp.split('.');
|
||||
let value = this; // eslint-disable-line consistent-this
|
||||
for (const prop of props) value = value[prop];
|
||||
process.send({ _fetchProp: message._fetchProp, _fetchPropValue: value });
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
47
src/sharding/ShardUtil.js
Normal file
47
src/sharding/ShardUtil.js
Normal file
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* Helper class for sharded clients
|
||||
*/
|
||||
class ShardUtil {
|
||||
/**
|
||||
* @param {Client} client Client of the current shard
|
||||
*/
|
||||
constructor(client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles an IPC message
|
||||
* @param {*} message Message received
|
||||
*/
|
||||
_handleMessage(message) {
|
||||
if (!message) return;
|
||||
if (message._eval) {
|
||||
try {
|
||||
process.send({ _evalResult: eval(message._eval) });
|
||||
} catch (err) {
|
||||
process.send({ _evalError: err });
|
||||
}
|
||||
} else if (message._fetchProp) {
|
||||
const props = message._fetchProp.split('.');
|
||||
let value = this; // eslint-disable-line consistent-this
|
||||
for (const prop of props) value = value[prop];
|
||||
process.send({ _fetchProp: message._fetchProp, _fetchPropValue: value });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates/gets the singleton of this class
|
||||
* @param {Client} client Client to use
|
||||
* @returns {ShardUtil}
|
||||
*/
|
||||
static singleton(client) {
|
||||
if (!this._singleton) {
|
||||
this._singleton = new this(client);
|
||||
} else {
|
||||
client.emit('error', 'Multiple clients created in child process; only the first will handle sharding helpers.');
|
||||
}
|
||||
return this._singleton;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ShardUtil;
|
||||
Reference in New Issue
Block a user