-
Notifications
You must be signed in to change notification settings - Fork 18
/
IPC.ts
177 lines (143 loc) · 4.77 KB
/
IPC.ts
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import {EventEmitter} from "events";
import * as Admiral from "../sharding/Admiral";
export class IPC extends EventEmitter {
private events: Map<string | number, Array<(msg: unknown) => void>>;
public constructor() {
super();
this.events = new Map();
process.on("message", msg => {
const event = this.events.get(msg.op);
if (event) {
event.forEach(fn => {
fn(msg);
});
}
});
}
public register(event: string, callback: (msg: unknown) => void): void {
const existingEvent = this.events.get(event);
if (existingEvent) {
this.events.set(event, existingEvent.concat([callback]));
} else {
this.events.set(event, [callback]);
}
}
public unregister(event:string): void {
this.events.delete(event);
}
public broadcast(op: string, message?: unknown): void {
if (!message) message = null;
if (process.send) process.send({op: "broadcast", event: {op, msg: message}});
}
public admiralBroadcast(op: string, message?: unknown): void {
if (!message) message = null;
if (process.send) process.send({op: "admiralBroadcast", event: {op, msg: message}});
}
public sendTo(cluster: number, op: string, message?: unknown): void {
if (!message) message = null;
if (process.send) process.send({op: "sendTo", cluster: cluster, event: {msg: message, op}});
}
public async fetchUser(id: number): Promise<any> {
if (process.send) process.send({op: "fetchUser", id});
return new Promise((resolve, reject) => {
const callback = (r: unknown) => {
this.removeListener(id.toString(), callback);
resolve(r);
};
this.on(id.toString(), callback);
});
}
public async fetchGuild(id: number): Promise<any> {
if (process.send) process.send({op: "fetchGuild", id});
return new Promise((resolve, reject) => {
const callback = (r: unknown) => {
this.removeListener(id.toString(), callback);
resolve(r);
};
this.on(id.toString(), callback);
});
}
public async fetchChannel(id: number): Promise<any> {
if (process.send) process.send({op: "fetchChannel", id});
return new Promise((resolve, reject) => {
const callback = (r: unknown) => {
this.removeListener(id.toString(), callback);
resolve(r);
};
this.on(id.toString(), callback);
});
}
public async fetchMember(guildID: number, memberID: number): Promise<any> {
const UUID = JSON.stringify({guildID, memberID});
if (process.send) process.send({op: "fetchMember", id: UUID});
return new Promise((resolve, reject) => {
const callback = (r: any) => {
if (r) r.id = memberID;
this.removeListener(UUID, callback);
resolve(r);
};
this.on(UUID, callback);
});
}
public async command(service: string, message?: unknown, receptive?: boolean): Promise<unknown> {
if (!message) message = null;
if (!receptive) receptive = false;
const UUID = JSON.stringify({timestamp: Date.now(), message, service, receptive});
if (process.send) process.send({op: "serviceCommand",
command: {
service,
msg: message,
UUID,
receptive
}
});
if (receptive) {
return new Promise((resolve, reject) => {
const callback = (r: any) => {
this.removeListener(UUID, callback);
if (r.value.err) {
reject(r.value.err);
} else {
resolve(r.value);
}
};
this.on(UUID, callback);
});
}
}
public async getStats(): Promise<Admiral.Stats> {
if (process.send) process.send({op: "getStats"});
return new Promise((resolve, reject) => {
const callback = (r: Admiral.Stats) => {
this.removeListener("statsReturn", callback);
resolve(r);
};
if (process.send) this.on("statsReturn", callback);
});
}
public restartCluster(clusterID: number, hard?: boolean): void {
if (process.send) process.send({op: "restartCluster", clusterID, hard: hard ? true : false});
}
public restartAllClusters(hard?: boolean): void {
if (process.send) process.send({op: "restartAllClusters", hard: hard ? true : false});
}
public restartService(serviceName: string, hard?: boolean): void {
if (process.send) process.send({op: "restartService", serviceName, hard: hard ? true : false});
}
public restartAllServices(hard?: boolean): void {
if (process.send) process.send({op: "restartAllServices", hard: hard ? true : false});
}
public shutdownCluster(clusterID: number, hard?: boolean): void {
if (process.send) process.send({op: "shutdownCluster", clusterID, hard: hard ? true : false});
}
public shutdownService(serviceName: string, hard?: boolean): void {
if (process.send) process.send({op: "shutdownService", serviceName, hard: hard ? true : false});
}
/** Total shutdown of fleet */
public totalShutdown(hard?: boolean): void {
if (process.send) process.send({op: "totalShutdown", hard: hard ? true : false});
}
public reshard(): void {
if (process.send) process.send({op: "reshard"});
}
}