-
Notifications
You must be signed in to change notification settings - Fork 417
/
ShardManager.js
132 lines (123 loc) · 4.11 KB
/
ShardManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"use strict";
const Collection = require("../util/Collection");
const Shard = require("./Shard");
class ShardManager extends Collection {
constructor(client) {
super(Shard);
this._client = client;
this.connectQueue = [];
this.lastConnect = 0;
this.connectTimeout = null;
}
_readyPacketCB() {
this.lastConnect = Date.now();
this.tryConnect();
}
connect(shard) {
if(shard.sessionID || (this.lastConnect <= Date.now() - 5000 && !this.find((shard) => shard.connecting))) {
shard.connect();
this.lastConnect = Date.now() + 7500;
} else {
this.connectQueue.push(shard);
this.tryConnect();
}
}
tryConnect() {
if(this.connectQueue.length > 0) {
if(this.lastConnect <= Date.now() - 5000) {
const shard = this.connectQueue.shift();
shard.connect();
this.lastConnect = Date.now() + 7500;
} else if(!this.connectTimeout) {
this.connectTimeout = setTimeout(() => {
this.connectTimeout = null;
this.tryConnect();
}, 1000);
}
}
}
spawn(id) {
let shard = this.get(id);
if(!shard) {
shard = this.add(new Shard(id, this._client));
shard.on("ready", () => {
/**
* Fired when a shard turns ready
* @event Client#shardReady
* @prop {Number} id The ID of the shard
*/
this._client.emit("shardReady", shard.id);
if(this._client.ready) {
return;
}
for(const other of this.values()) {
if(!other.ready) {
return;
}
}
this._client.ready = true;
this._client.startTime = Date.now();
/**
* Fired when all shards turn ready
* @event Client#ready
*/
this._client.emit("ready");
}).on("resume", () => {
/**
* Fired when a shard resumes
* @event Client#shardResume
* @prop {Number} id The ID of the shard
*/
this._client.emit("shardResume", shard.id);
if(this._client.ready) {
return;
}
for(const other of this.values()) {
if(!other.ready) {
return;
}
}
this._client.ready = true;
this._client.startTime = Date.now();
this._client.emit("ready");
}).on("disconnect", (error) => {
/**
* Fired when a shard disconnects
* @event Client#shardDisconnect
* @prop {Error?} error The error, if any
* @prop {Number} id The ID of the shard
*/
this._client.emit("shardDisconnect", error, shard.id);
for(const other of this.values()) {
if(other.ready) {
return;
}
}
this._client.ready = false;
this._client.startTime = 0;
/**
* Fired when all shards disconnect
* @event Client#disconnect
*/
this._client.emit("disconnect");
});
}
if(shard.status === "disconnected") {
this.connect(shard);
}
}
toJSON() {
const base = {};
for(const key in this) {
if(this.hasOwnProperty(key) && !key.startsWith("_")) {
if(this[key] && typeof this[key].toJSON === "function") {
base[key] = this[key].toJSON();
} else {
base[key] = this[key];
}
}
}
return base;
}
}
module.exports = ShardManager;