Skip to content

Commit

Permalink
[Chore] eslint & typings fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
DevYukine committed Jan 19, 2021
1 parent 756f1da commit 5f67c84
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 19 deletions.
8 changes: 4 additions & 4 deletions src/Cluster/Cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class Cluster extends EventEmitter {

private readonly _exitListenerFunction: (...args: any[]) => void;

constructor(options: ClusterOptions) {
public constructor(options: ClusterOptions) {
super();
this.id = options.id;
this.shards = options.shards;
Expand Down Expand Up @@ -58,7 +58,7 @@ export class Cluster extends EventEmitter {
await this.spawn();
}

public send(data: object) {
public send(data: unknown) {
return this.manager.ipc.node.sendTo(`Cluster ${this.id}`, data);
}

Expand All @@ -80,9 +80,9 @@ export class Cluster extends EventEmitter {
this.ready = false;
this.worker = undefined;

if (this.manager.respawn) this.respawn();

this.manager.emit('debug', `Worker exited with code ${code} and signal ${signal}${this.manager.respawn ? ', restarting...' : ''}`);

if (this.manager.respawn) return this.respawn();
}

private _waitReady(shardCount: number) {
Expand Down
1 change: 1 addition & 0 deletions src/IPC/ClusterIPC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class ClusterIPC extends EventEmitter {
.on('error', error => this.emit('error', error))
.on('disconnect', client => this.emit('warn', `[IPC] Disconnected from ${client.name}`))
.on('ready', client => this.emit('debug', `[IPC] Connected to: ${client.name}`))
// eslint-disable-next-line @typescript-eslint/no-misused-promises
.on('message', this._message.bind(this));
}

Expand Down
2 changes: 1 addition & 1 deletion src/IPC/MasterIPC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class MasterIPC extends EventEmitter {
.on('disconnect', client => this.emit('debug', `Client Disconnected: ${client.name}`))
.on('error', error => this.emit('error', error))
.on('message', this._incommingMessage.bind(this));
if (isMaster) this.server.listen(manager.ipcSocket);
if (isMaster) void this.server.listen(manager.ipcSocket);
}

public async broadcast(code: string) {
Expand Down
26 changes: 14 additions & 12 deletions src/Sharding/ShardingManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ export interface SharderOptions {
export interface SessionObject {
url: string;
shards: number;
// eslint-disable-next-line @typescript-eslint/naming-convention
session_start_limit: {
total: number;
remaining: number;
// eslint-disable-next-line @typescript-eslint/naming-convention
reset_after: number;
};
}
Expand Down Expand Up @@ -59,16 +61,16 @@ export class ShardingManager extends EventEmitter {

public constructor(public path: string, options: SharderOptions) {
super();
this.clusterCount = options.clusterCount || cpus().length;
this.guildsPerShard = options.guildsPerShard || 1000;
this.clientOptions = options.clientOptions || {};
this.development = options.development || false;
this.shardCount = options.shardCount || 'auto';
this.client = options.client || Client;
this.respawn = options.respawn || true;
this.ipcSocket = options.ipcSocket || 9999;
this.retry = options.retry || true;
this.timeout = options.timeout || 30000;
this.clusterCount = options.clusterCount ?? cpus().length;
this.guildsPerShard = options.guildsPerShard ?? 1000;
this.clientOptions = options.clientOptions ?? {};
this.development = options.development ?? false;
this.shardCount = options.shardCount ?? 'auto';
this.client = options.client ?? Client;
this.respawn = options.respawn ?? true;
this.ipcSocket = options.ipcSocket ?? 9999;
this.retry = options.retry ?? true;
this.timeout = options.timeout ?? 30000;
this.token = options.token;
this.nodeArgs = options.nodeArgs;
this.ipc = new MasterIPC(this);
Expand Down Expand Up @@ -201,10 +203,10 @@ export class ShardingManager extends EventEmitter {
if (!this.token) throw new Error('No token was provided!');
const res = await fetch(`${http.api}/v${http.version}/gateway/bot`, {
method: 'GET',
headers: { Authorization: `Bot ${this.token.replace(/^Bot\s*/i, '')}` }
headers: { authorization: `Bot ${this.token.replace(/^Bot\s*/i, '')}` }
});
if (res.ok) return res.json();
throw res;
throw new Error(`Invalid Session Endpoint response: ${res.status} ${res.statusText}`);
}

private _debug(message: string) {
Expand Down
6 changes: 4 additions & 2 deletions src/Util/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function deepClone(source: any): any {
return output;
}
if (isObject(source)) {
const output = {} as Record<string, any>;
const output: Record<string, any> = {};
for (const [key, value] of Object.entries(source)) output[key] = deepClone(value);
return output;
}
Expand All @@ -48,7 +48,7 @@ export function isPrimitive(value: any): value is string | bigint | number | boo
return PRIMITIVE_TYPES.includes(typeof value);
}

export function mergeDefault<T>(def: Record<string, any>, given: Record<string, any>): T {
export function mergeDefault<T>(def: Record<string, any>, given?: Record<string, any>): T {
if (!given) return deepClone(def);
for (const key in def) {
if (given[key] === undefined) given[key] = deepClone(def[key]);
Expand All @@ -71,7 +71,9 @@ export function calcShards(shards: number, guildsPerShard: number): number {
}

export async function startCluster(manager: ShardingManager) {
// eslint-disable-next-line @typescript-eslint/naming-convention
const ClusterClassRequire = await import(manager.path);
// eslint-disable-next-line @typescript-eslint/naming-convention
const ClusterClass = ClusterClassRequire.default ? ClusterClassRequire.default : ClusterClassRequire;
const cluster = new ClusterClass(manager) as BaseCluster;
return cluster.init();
Expand Down

0 comments on commit 5f67c84

Please sign in to comment.