Skip to content

Commit

Permalink
Modernise the spawn loop (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Frangu Vlad authored and FireController1847 committed Feb 9, 2018
1 parent d78e674 commit 23acc3c
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/client/websocket/WebSocketManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,8 @@ class WebSocketManager extends EventEmitter {
* @param {Function} reject Function to run when connection fails
*/
spawn(gateway, resolve, reject) {
this.debug(`SHARD EXISTS: ${typeof this.client.shard}`);
this.debug(`SHARD ID: ${(this.client.shard ? this.client.shard.id : 0)}`);
this.gateway = gateway;
(function spawnLoop(id) {
const spawnShard = id => {
if (this.client.options.internalSharding && id >= this.client.options.shardCount) return;
this.debug(`Spawning shard ${id}`);
const shard = this.createShard(id);
Expand All @@ -110,8 +108,8 @@ class WebSocketManager extends EventEmitter {
if (event === 4011) reject(new Error('SHARDING_REQUIRED'));
});
shard.once('ready', () => {
this.debug(`Shard ready ${id}`);
if (this.client.options.internalSharding) this.client.setTimeout(spawnLoop.bind(this, id + 1), 5500);
this.debug(`Shard ${id} is ready`);
if (this.client.options.internalSharding) this.client.setTimeout(newId => spawnShard(newId), 5500, id + 1);
/**
* Emitted when a shard becomes ready to start working.
* @event Client#shardReady
Expand All @@ -127,7 +125,8 @@ class WebSocketManager extends EventEmitter {
this.packetManager.handleQueue();
}
});
}.bind(this)(this.client.options.internalSharding ? 0 : this.client.options.shardId));
};
spawnShard(this.client.options.internalSharding ? 0 : this.client.options.shardId);
}

/**
Expand Down

0 comments on commit 23acc3c

Please sign in to comment.