Skip to content

Commit

Permalink
feat(Shard): add eval context (#7011)
Browse files Browse the repository at this point in the history
Co-authored-by: SpaceEEC <spaceeec@yahoo.com>
  • Loading branch information
daimond113 and SpaceEEC committed Nov 23, 2021
1 parent c1f2fe2 commit 77aff08
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/sharding/Shard.js
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
1 change: 1 addition & 0 deletions typings/index.d.ts
Expand Up @@ -1867,6 +1867,7 @@ export class Shard extends EventEmitter {
public worker: Worker | null;
public eval(script: string): Promise<unknown>;
public eval<T>(fn: (client: Client) => T): Promise<T>;
public eval<T, P>(fn: (client: Client, context: Serialized<P>) => T, context: P): Promise<T>;
public fetchClientValue(prop: string): Promise<unknown>;
public kill(): void;
public respawn(options?: { delay?: number; timeout?: number }): Promise<ChildProcess>;
Expand Down

0 comments on commit 77aff08

Please sign in to comment.