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(Shard): add eval context #7011

Merged
merged 4 commits into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 3 additions & 2 deletions src/sharding/Shard.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,12 @@ class Shard extends EventEmitter {
/**
* Evaluates a script or function on the shard, in the context of the {@link Client}.
* @param {string|Function} script JavaScript to run on the shard
* @param {*} [context] The context for the eval
* @returns {Promise<*>} Result of the script execution
*/
eval(script) {
eval(script, context) {
// Stringify the script if it's a Function
const _eval = typeof script === 'function' ? `(${script})(this)` : script;
const _eval = typeof script === 'function' ? `(${script})(this, ${JSON.stringify(context)})` : script;

// Shard is dead (maybe respawning), don't cache anything and error immediately
if (!this.process && !this.worker) return Promise.reject(new Error('SHARDING_NO_CHILD_EXISTS', this.id));
Expand Down
2 changes: 1 addition & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1846,7 +1846,7 @@ export class Shard extends EventEmitter {
public ready: boolean;
public worker: Worker | null;
public eval(script: string): Promise<unknown>;
public eval<T>(fn: (client: Client) => T): Promise<T>;
public eval<T, C>(fn: (client: Client, context: C) => T, context: C): Promise<T>;
daimond113 marked this conversation as resolved.
Show resolved Hide resolved
public fetchClientValue(prop: string): Promise<unknown>;
public kill(): void;
public respawn(options?: { delay?: number; timeout?: number }): Promise<ChildProcess>;
Expand Down