Improve sharding some more

This commit is contained in:
Schuyler Cebulskie
2016-09-24 00:44:47 -04:00
parent fa18b0c6c9
commit 5ea62e63ab
2 changed files with 27 additions and 7 deletions

File diff suppressed because one or more lines are too long

View File

@@ -39,6 +39,7 @@ class ShardingManager extends EventEmitter {
throw new TypeError('Amount of shards must be a number.'); throw new TypeError('Amount of shards must be a number.');
} }
if (this.totalShards < 1) throw new RangeError('Amount of shards must be at least 1.'); if (this.totalShards < 1) throw new RangeError('Amount of shards must be at least 1.');
if (this.totalShards !== Math.floor(this.totalShards)) throw new RangeError('Amount of shards must be an integer.');
this.respawn = respawn; this.respawn = respawn;
@@ -52,6 +53,7 @@ class ShardingManager extends EventEmitter {
/** /**
* Spawns a single shard. * Spawns a single shard.
* @param {number} id The ID of the shard to spawn. THIS IS NOT NEEDED IN ANY NORMAL CASE! * @param {number} id The ID of the shard to spawn. THIS IS NOT NEEDED IN ANY NORMAL CASE!
* @returns {Promise<Shard>}
*/ */
createShard(id = this.shards.size) { createShard(id = this.shards.size) {
const shard = new Shard(this, id); const shard = new Shard(this, id);
@@ -62,20 +64,37 @@ class ShardingManager extends EventEmitter {
* @param {Shard} shard Shard that was launched * @param {Shard} shard Shard that was launched
*/ */
this.emit('launch', shard); this.emit('launch', shard);
return Promise.resolve(shard);
} }
/** /**
* Spawns multiple shards. * Spawns multiple shards.
* @param {number} [amount=this.totalShards] Number of shards to spawn * @param {number} [amount=this.totalShards] Number of shards to spawn
* @param {number} [delay=5500] How long to wait in between spawning each shard (in milliseconds) * @param {number} [delay=5500] How long to wait in between spawning each shard (in milliseconds)
* @returns {Promise<Collection<number, Shard>>}
*/ */
spawn(amount = this.totalShards, delay = 5500) { spawn(amount = this.totalShards, delay = 5500) {
this.totalShards = amount; if (typeof amount !== 'number' || isNaN(amount)) throw new TypeError('Amount of shards must be a number.');
this.createShard(); if (amount < 1) throw new RangeError('Amount of shards must be at least 1.');
const interval = setInterval(() => { if (amount !== Math.floor(amount)) throw new RangeError('Amount of shards must be an integer.');
if (this.shards.size === this.totalShards) clearInterval(interval);
else this.createShard(); return new Promise(resolve => {
}, delay); this.totalShards = amount;
this.createShard();
if (delay <= 0) {
while (this.shards.size < this.totalShards) this.createShard();
resolve(this.shards);
} else {
const interval = setInterval(() => {
this.createShard();
if (this.shards.size >= this.totalShards) {
clearInterval(interval);
resolve(this.shards);
}
}, delay);
}
});
} }
/** /**
@@ -95,6 +114,7 @@ class ShardingManager extends EventEmitter {
* @returns {Promise<number>} * @returns {Promise<number>}
*/ */
fetchGuildCount(timeout = 3000) { fetchGuildCount(timeout = 3000) {
if (this.shards.size === 0) return Promise.reject(new Error('No shards have been spawned.'));
if (this.shards.size !== this.totalShards) return Promise.reject(new Error('Still spawning shards.')); if (this.shards.size !== this.totalShards) return Promise.reject(new Error('Still spawning shards.'));
if (this._guildCountPromise) return this._guildCountPromise; if (this._guildCountPromise) return this._guildCountPromise;