Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Sharding*): contexts for broadcastEval #5756

Merged
merged 14 commits into from Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/sharding/Shard.js
Expand Up @@ -348,7 +348,7 @@ class Shard extends EventEmitter {
// Shard is requesting an eval broadcast
if (message._sEval) {
const resp = { _sEval: message._sEval, _sEvalShard: message._sEvalShard };
this.manager.broadcastEval(message._sEval, message._sEvalShard).then(
this.manager.broadcastEval(message._sEval, { shard: message._sEvalShard }).then(
results => this.send({ ...resp, _result: results }),
err => this.send({ ...resp, _error: Util.makePlainError(err) }),
);
Expand Down
10 changes: 5 additions & 5 deletions src/sharding/ShardClientUtil.js
Expand Up @@ -128,28 +128,28 @@ class ShardClientUtil {
/**
* Evaluates a script or function on all shards, or a given shard, in the context of the {@link Client}s.
* @param {string|Function} script JavaScript to run on each shard
* @param {number} [shard] Shard to run script on, all if undefined
* @param {BroadcastEvalOptions} [options={}] The options for the broadcast
* @returns {Promise<*>|Promise<Array<*>>} Results of the script execution
* @example
* client.shard.broadcastEval('this.guilds.cache.size')
* .then(results => console.log(`${results.reduce((prev, val) => prev + val, 0)} total guilds`))
* .catch(console.error);
* @see {@link ShardingManager#broadcastEval}
*/
broadcastEval(script, shard) {
broadcastEval(script, options = {}) {
SpaceEEC marked this conversation as resolved.
Show resolved Hide resolved
return new Promise((resolve, reject) => {
const parent = this.parentPort || process;
script = typeof script === 'function' ? `(${script})(this)` : script;
script = typeof script === 'function' ? `(${script})(this, ${JSON.stringify(options.context)})` : script;

const listener = message => {
if (!message || message._sEval !== script || message._sEvalShard !== shard) return;
if (!message || message._sEval !== script || message._sEvalShard !== options.shard) return;
parent.removeListener('message', listener);
if (!message._error) resolve(message._result);
else reject(Util.makeError(message._error));
};
parent.on('message', listener);

this.send({ _sEval: script, _sEvalShard: shard }).catch(err => {
this.send({ _sEval: script, _sEvalShard: options.shard }).catch(err => {
parent.removeListener('message', listener);
reject(err);
});
Expand Down
19 changes: 15 additions & 4 deletions src/sharding/ShardingManager.js
Expand Up @@ -226,14 +226,25 @@ class ShardingManager extends EventEmitter {
return Promise.all(promises);
}

/**
* Options for {@link ShardingManager#broadcastEval} and {@link ShardClientUtil#broadcastEval}.
* @typedef {Object} BroadcastEvalOptions
* @property {number} [shard] Shard to run script on, all if undefined
* @property {*} [context] The JSON-serializable values to call the script with
*/

/**
* Evaluates a script on all shards, or a given shard, in the context of the {@link Client}s.
* @param {string} script JavaScript to run on each shard
* @param {number} [shard] Shard to run on, all if undefined
* @param {string|Function} script JavaScript to run on each shard
* @param {BroadcastEvalOptions} [options={}] The options for the broadcast
* @returns {Promise<*>|Promise<Array<*>>} Results of the script execution
*/
broadcastEval(script, shard) {
return this._performOnShards('eval', [script], shard);
broadcastEval(script, options = {}) {
SpaceEEC marked this conversation as resolved.
Show resolved Hide resolved
return this._performOnShards(
'eval',
[typeof script === 'function' ? `(${script})(this, ${JSON.stringify(options.context)})` : script],
options.shard,
);
}

/**
Expand Down
19 changes: 13 additions & 6 deletions typings/index.d.ts
Expand Up @@ -1695,10 +1695,10 @@ declare module 'discord.js' {
public readonly ids: number[];
public mode: ShardingManagerMode;
public parentPort: any | null;
public broadcastEval(script: string): Promise<any[]>;
public broadcastEval(script: string, shard: number): Promise<any>;
public broadcastEval<T>(fn: (client: Client) => T): Promise<T[]>;
public broadcastEval<T>(fn: (client: Client) => T, shard: number): Promise<T>;
public broadcastEval(script: string, { shard: undefined }: BroadcastEvalOptions): Promise<any[]>;
public broadcastEval(script: string, { shard: number }: BroadcastEvalOptions): Promise<any>;
public broadcastEval<T, P>(fn: (client: Client, context: P) => T, { shard: undefined, context: P }: BroadcastEvalOptions): Promise<T[]>;
amishshah marked this conversation as resolved.
Show resolved Hide resolved
public broadcastEval<T, P>(fn: (client: Client, context: P) => T, { shard: number, context: P }: BroadcastEvalOptions): Promise<T>;
public fetchClientValues(prop: string): Promise<any[]>;
public fetchClientValues(prop: string, shard: number): Promise<any>;
public respawnAll(options?: { shardDelay?: number; respawnDelay?: number; timeout?: number }): Promise<void>;
Expand All @@ -1721,8 +1721,10 @@ declare module 'discord.js' {
public totalShards: number | 'auto';
public shardList: number[] | 'auto';
public broadcast(message: any): Promise<Shard[]>;
public broadcastEval(script: string): Promise<any[]>;
public broadcastEval(script: string, shard: number): Promise<any>;
public broadcastEval(script: string, { shard: undefined }: BroadcastEvalOptions): Promise<any[]>;
public broadcastEval(script: string, { shard: number }: BroadcastEvalOptions): Promise<any>;
public broadcastEval<T, P>(fn: (client: Client, context: P) => T, { shard: undefined, context: P }: BroadcastEvalOptions): Promise<T[]>;
amishshah marked this conversation as resolved.
Show resolved Hide resolved
public broadcastEval<T, P>(fn: (client: Client, context: P) => T, { shard: number, context: P }: BroadcastEvalOptions): Promise<T>;
public createShard(id: number): Shard;
public fetchClientValues(prop: string): Promise<any[]>;
public fetchClientValues(prop: string, shard: number): Promise<any>;
Expand Down Expand Up @@ -2729,6 +2731,11 @@ declare module 'discord.js' {
| N
| Readonly<BitField<T, N>>;

interface BroadcastEvalOptions<T = unknown> {
shard?: number;
context?: T;
}

type BufferResolvable = Buffer | string;

interface ChannelCreationOverwrites {
Expand Down